網(wǎng)站后臺密碼錯誤今天重要新聞
一、數(shù)據(jù)的插入(INSERT語句的使用方法)
1、什么是INSERT
首先通過CREATE TABLE語句創(chuàng)建表,但創(chuàng)建的表中沒有數(shù)據(jù);再通過INSERT語句向表中插入數(shù)據(jù)。
--創(chuàng)建表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,……)。
列清單與值清單的列數(shù)必須保持一致, 通常執(zhí)行一次INSERT語句會插入一行數(shù)據(jù);但也就二將多條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、插入默認值
通過顯性的方式插入默認值
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 |