𝟮𝟳. 𝗦𝗤𝗟 𝗕𝗮𝘀𝗶𝗰𝘀

Learn SQL with this guide from Dr. Angela.

SQL manages data in tables. You use CRUD operations to handle information.

CRUD stands for:

  1. Managing Tables

Use CREATE TABLE to make a new table. Use a Primary Key to identify each row uniquely.

Example: CREATE TABLE products ( id INT NOT NULL, name STRING, price MONEY, PRIMARY KEY (id) );

Use INSERT to add data. To add data to all columns: INSERT INTO products VALUES (1, 'Pen', 1.20);

To add data to specific columns: INSERT INTO products (id, name) VALUES (2, 'Pencil');

  1. Reading Data

Use SELECT to see your data. To see everything: SELECT * FROM products;

Use WHERE to filter results. To see one item: SELECT * FROM products WHERE id = 1;

  1. Modifying Data and Tables

Use UPDATE to change existing values. The SET command picks the new value. The WHERE command picks the row.

Example: UPDATE products SET price = 1.00 WHERE id = 1;

Use ALTER TABLE to add a column. Example: ALTER TABLE products ADD stock INT;

  1. Deleting Data

Use DELETE to remove rows. Example: DELETE FROM products WHERE id = 2;

Note: Always use a WHERE clause. If you omit it, you delete every row in the table.

  1. Relationships and Joins

Foreign Keys link two tables together. This keeps your data organized and accurate.

Use INNER JOIN to combine data from different tables. It only shows rows where the data matches in both tables.

Example: SELECT orders.order_number, customers.first_name FROM orders INNER JOIN customers ON orders.customer_id = customers.id;

Resources:

Source: https://dev.to/avery_/27-sql-4ha6