1、数据插入
INSERT 用来插入行到数据库表。插入有多种形式:
- 插入完整的行
- 插入行的一部分
- 插入多行
- 插入某些查询的值
#缺点是不安全,必须按照表中定义列的顺序插入数据,如果表的结构发生改变,将会和预计的结果不一致甚至失败。
#有效的方法是插入列名。
insert into customers
values(
NULL,
'Pep E. LaPew',
'100 Main Street',
'Los Angeles',
'CA',
'90046',
'USA',
NULL,
NULL
);
插入列名:
insert into customers(
CUST_NAME,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
values(
'Pep E. LaPewG',
'100 Main Street',
'Los Angeles',
'CA',
'90046',
'USA',
NULL,
NULL);
INSERT LOW_PRIORITY INTO#降低插入的优先级,提高性能
2、插入多行数据
insert into customers(
CUST_NAME,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
values(
'Pep E. LaPewG',
'100 Main Street',
'Los Angeles',
'CA',
'90046',
'CHN',
NULL,
NULL),
(
'M.Martian',
'42 Galaxy Way',
'New Yotk',
'NY',
'11213',
'USA',
NULL,
NULL);
3、插入检索出的数据
insert into customers(
CUST_NAME,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
SELECT CUST_NAME,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email
from custnew;#先建立的一个表