15插入数据

插入数据

插入行到数据库表中。

1.数据插入

插入有几种方式

1.插入完整的行

2.插入行的一部分

3.插入某些查询的结果

1.1插入完整的行

要求指定表名和插入行中的值。使用INSERT INTO指出表名,用values子句给出值。

INSERT INTO Customers(cust_id,
cust_contact,
cust_email,
cust_name,
cust_adress,
cust_city,
cust_state,
cust_zip)
Values('10000001',
NULL,
NULL,
'Toy Land',
'123 Any Street',
'New York',
'NY',
'1111');

可以省略Custmers中的列名,但那样必须按照表的顺序填写value值。而写明列名可以不按照表顺序来。

注:如果不给出列名,都必须给出所有列的值

(如果主键是自增的,则可以省略主键值)

1.2插入检索出的数据

INSERT INTO

SELECT关键字

INSERT INTO Customers(cust_id,
cust_contact,
cust_email,
cust_name,
cust_adress,
cust_city,
cust_state,
cust_zip)
SELECT cust_id,
cust_contact,
cust_email,
cust_name,
cust_adress,
cust_city,
cust_state,
cust_zip
From CustNew;

2.从一个表复制到另一个表

SELECT INTO语句

SELECT *
INTO CustCopy
From Customers;