SQL 常用語法


Posted by pei_______ on 2022-06-12

SQLite - 線上版


SQL四大主要功能

  1. Create
  2. Read
  3. Update
  4. 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;

#SQL #web系列







Related Posts

DNS, Lock, NoSQL vs SQL and ACID

DNS, Lock, NoSQL vs SQL and ACID

讀書筆記-JavaScript技術手冊1: Intro.

讀書筆記-JavaScript技術手冊1: Intro.

CSS:object-fit:none的表現

CSS:object-fit:none的表現


Comments