SQL四大主要功能
- Create
- Read
- Update
- Destroy
01. Create Table
CREATE TABLE Products (
id INT NOT NULL,
name STRING,
price MONEY,
PRIMARY KEY (id)
)
02. Create Data
have all values
INSERT INTO products
VALUES (1, "pen", 1.20)
miss some values
INSERT INTO products (id, name)
VALUES (2, "pencil")
03. Read Table
select everything
SELECT * FROM 'products';
select particular column
SELECT name, price FROM Products
select particular row
SELECT *
FROM Products
WHERE id = 1
05. Update Data
UPDATE products
SET price = 1.0
WHERE id = 2
06. Update by Alter Column
ALTER TABLE Product
ADD stock INT;
07. Delete Data
DELETE FROM Product
WHERE id = 2
08. Relationship built with other table
CREATE TABLE orders (
id int NOT NULL,
order_number Int,
customer_id INT,
product_id INT,
PRIMARY KEY (id),
FOREIGN KEY (customer_id) REFERENCES customers (id),
FOREIGN KEy (product_id) REFERENCES Product (id)
)
09. Relationship showed by table
SELECT orders.order_number, customers.first_name, customers.last_name, customers.address
FROM orders
INNER JOIN customers ON orders.customer_id = customers.id;