SQL笔记#数据更新

一、数据的插入(INSERT语句的使用方法)

1、什么是INSERT

        首先通过CREATE TABLE语句创建表,但创建的表中没有数据;再通过INSERT语句向表中插入数据。

--创建表ProductIns
CREATE TABLE ProductIns
(product_id      CHAR(4)      NOT NULL,
 product_name    VARCHAR(100) NOT NULL,
 product_type    VARCHAR(32)  NOT NULL,
 sale_price      INTEGER      DEFAULT 0,
 purchase_price  INTEGER      ,
 regist_date     DATE         ,
 PRIMARY KEY (product_id));

2、INSERT语句的基本语法

INSERT INTO <表名> (列1,列2,列3,……) VALUES (值1,值2,值3,……);

INSERT INTO ProductIns (product_id, product_name, product_type, sale_price, purchase_price, regist_date) VALUES ('0001', 'T恤衫' ,'衣服', 1000, 500, '2009-09-20');

列清单为(列1,列2,列3,……);

值清单为(值1,值2,值3,……)。 

        列清单与值清单的列数必须保持一致, 通常执行一次INSERT语句会插入一行数据;但也就二将多条VALUE子句通过都好进行分隔排列。

-- 多行INSERT(Oracle除外)
INSERT INTO ProductIns VALUES ('0002', '打孔器', '办公用品', 500, 320, '2009-09-11'),
                              ('0003', '运动T恤', '衣服', 4000, 2800, NULL),
                              ('0004', '菜刀', '厨房用具', 3000, 2800, '2009-09-20');

3、列清单的省略

-- 包含列清单
INSERT INTO ProductIns (product_id, product_name, product_type, sale_price, purchase_price, regist_date) VALUES ('0005', '高压锅', '厨房用具', 6800, 5000, '2009-01-15');

-- 省略列清单
INSERT INTO ProductIns VALUES ('0005', '高压锅', '厨房用具', 6800, 5000, '2009-01-15');

4、插入NULL

INSERT INTO ProductIns (product_id, product_name, product_type, sale_price, purchase_price, regist_date) VALUES ('0006', '叉子', '厨房用具', 500, NULL, '2009-09-20');

5、插入默认值

\blacksquare 通过显性的方式插入默认值

INSERT INTO ProductIns (product_id, product_name, product_type, sale_price, purchase_price, regist_date) VALUES ('0007', '擦菜板', '厨房用具', DEFAULT, 790, '2009-04-28');
postgres=# SELECT * FROM ProductIns WHERE product_id = '0007';
 product_id | product_name | product_type | sale_price | purchase_price | regist_date
------------+--------------+--------------+------------+----------------+-------------
 0007       | 

你可能感兴趣的:(SQL笔记,sql,笔记,数据库)