<<SQL必知必会>> -- 笔记4

插入数据

INSERT

插入完整行
    INSERT INTO Customers 
    VALUES('1000000006',
            'Toy Land',
            '123 Any Street',
            'New York',
            'NY',
            '11111',
            'USA',
            NULL,
            NULL);

一一对应的属性

    INSERT INTO Customers(cust_id,
                          cust_name,
                          cust_city,
                          cust_address,
                          cust_state,
                          cust_zip,
                          cust_country,
                          cust_contact,
                          cust_email) 
    VALUES('1000000006',
            'Toy Land',
            '123 Any Street',
            'New York',
            'NY',
            '11111',
            'USA',
            NULL,
            NULL);
插入部分行
    INSERT INTO Customers(cust_id,
                          cust_name,
                          cust_city,
                          cust_address,
                          cust_state,
                          cust_zip,
                          cust_country) 
    VALUES('1000000006',
            'Toy Land',
            '123 Any Street',
            'New York',
            'NY',
            '11111',
            'USA');

部分属性允许为NULL,或给出默认值

插入检索出的数据

INSERT SELECT语句

    INSERT INTO Customers(.....)
    SELECT ..... FROM CustNew;
从一个表复制到另一个表
    CREATE TABLE CustCopy AS
    SELECT * FROM Customers;




更新和删除数据

UPDATE

    UPDATE Customers 
    SET cust_contact = 'Sam Roberts',
        cust_email = '[email protected]'
    WHERE cust_id = '1000000006';

DELETE

    DELETE FROM Customers 
    WHERE cust_id = '1000000006';

如果UPDATEDELETE不带WHERE的话就是更改/删除每一行



创建和操纵表

CREATE

    CREATE TABLE Orders
    (
        order_num   INTEGER     NOT NULL,
        order_date  DATETIME    NOT NULL,
        cust_id     CHAR(10)    NOT NULL 
    );
指定默认值

在后面加上DEFAULT

    CREATE TABLE OrderItems
    (
        order_num       INTEGER     NOT NULL,
        order_item      INTEGER     NOT NULL,
        prod_id         CHAR(10)    NOT NULL,
        quantity        INTEGER     NOT NULL    DEFAULT 1,
        item_price      DECIMAL(8, 2) NOT NULL 
    );

ALTER

增加列
    ALTER TABLE Vendors
    ADD vend_phone CHAR(20);
删除列
    ALTER TABLE Vendors 
    DROP COLUMN vend_phone;

删除表

    DROP TABLE CustCopy;




使用视图

VIEW

创建视图
    CREATE VIEW ProductCustomers AS
    SELECT cust_name, cust_contact, prod_id
    FROM Customers, Orders, OrderItems
    WHERE Customers.cust_id = Orders.cust_id 
    AND OrderItems.order_num = Orders.order_num;




存储过程

所有处理封装,简化操作

事务

游标

高级SQL特性

约束

主键

唯一标识

    CREATE TABLE Orders
    (
        order_num   INTEGER     NOT NULL,    PRIMARY KEY,
        order_date  DATETIME    NOT NULL,
        cust_id     CHAR(10)    NOT NULL 
    );

或者创建表后再添加

    ALTER TABLE Orders
    ADD CONSTRAINT PRIMARY KEY (order_num);
外键
    CREATE TABLE Orders
    (
        order_num   INTEGER     NOT NULL,    PRIMARY KEY,
        order_date  DATETIME    NOT NULL,
        cust_id     CHAR(10)    NOT NULL ,    REFERENCES Customers(cust_id)
    );

或者

    ALTER TABLE Orders
    ADD CONSTRAINT
    FOREIGN KEY (cust_id) REFERENCES Customers (cust_id);
检查约束
    CREATE TABLE OrderItems
    (
        order_num       INTEGER     NOT NULL,
        order_item      INTEGER     NOT NULL,
        prod_id         CHAR(10)    NOT NULL,
        quantity        INTEGER     NOT NULL    CHECK (quantity > 0),
        item_price      DECIMAL(8, 2) NOT NULL 
    );

或者

    ADD CONSTRAINT CHECK (gender LIKE '[MF]');

索引

触发器

猜你喜欢

转载自www.cnblogs.com/burymyname/p/11908213.html