插入数据
本章节引入新的语句 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_contact, cust_email, cust_address, cust_city, cust_state, cust_zip, cust_country) VALUES('Pep E. LaPew', NULL, NULL, '100 Main Street', 'Los os Angeles', 'CA', '90046', 'USA');
通过上面的这条命令指定数据表的列,以及值从而安全的进行数据插入,但是稍显繁琐;
插入多行数据
INSERT INTO customers(cust_name, cust_contact, cust_email, cust_address, cust_city, cust_state, cust_zip, cust_country) VALUES('Pep E. LaPew', NULL, NULL, '100 Main Street', 'Los Angeles', 'CA', '90046', 'USA');INSERT INTO customers(cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country) VALUES('M. Martian', '42 Galaxy Way', 'New York', 'NY', '11213', 'USA');
插入多行数据的方式,通过叠加多个INSERT语句可以实现;
插入检索出的数据
INSERT INTO customers (cust_id, cust_contact, cust_email, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country) SELECT cust_id, cust_contact, cust_email, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country FROM custnew;
执行命令后的生成的新的数据表信息;