文章目录
  1. 数据插入
  2. 更新数据
  3. 删除数据

数据插入

插入完整的行

1
2
3
INSERT INTO Customers(
cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
VALUES('1000000006', 'Tot Land', '123 Any Street', 'New York', 'NY', '11111', 'USA', NULL, NULL);

若只指定了部分列,则未指定的列的值为空,或者为默认值;这就要求省略的列必须允许NULL值,或者定义了默认值。

还能利用INSERT语句插入检索出的数据,这就是INSERT SELECT;从CustNew中将所有的数据导入Customers;

1
2
3
4
INSERT INTO Customers(
cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
SELECT cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email
FROM CustNew;

从一个表复制到另一个表;

1
CREATE TABLE CustCopy AS SELECT * FROM Customers;

更新数据

要更新表中的数据,可使用UPDATE语句。既能更新表中的特定行,也能更新表中的所有行。

UPDATE语句需指定表、列名与新值,以及过滤条件。如更新客户的电子邮件地址。

1
UPDATE Customers SET cust_email='kim@thetoystore.com' WHERE cust_id='1000000005';

要更新多个列时,只需要在每对”列=值”之间用逗号分开。

若要删除某列的值,可设置它为NULL;

删除数据

使用DELETE语句删除行,只需要指定该行即可。

1
DELETE FROM Customers WHERE cust_id='1000000006';

若要删除表中所有行,应使用TRUNCATE TABLE;