案例1:insert语句的使用
案例1:插入完整的一行数据
mysql> insert into customers values (null, 'abc', '453 Main street', 'los', 'ca', '99045', 'usa', null, null);
Query OK, 1 row affected (0.03 sec)
mysql> select * from customers;
+---------+----------------+---------------------+-----------+------------+----------+--------------+--------------+---------------------+
| cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email |
+---------+----------------+---------------------+-----------+------------+----------+--------------+--------------+---------------------+
| 10001 | Coyote Inc. | 200 Maple Lane | Detroit | MI | 44444 | USA | Y Lee | ylee@coyote.com |
| 10002 | Mouse House | 333 Fromage Lane | Columbus | OH | 43333 | USA | Jerry Mouse | NULL |
| 10003 | Wascals | 1 Sunny Place | Muncie | IN | 42222 | USA | Jim Jones | rabbit@wascally.com |
| 10004 | Yosemite Place | 829 Riverside Drive | Phoenix | AZ | 88888 | USA | Y Sam | sam@yosemite.com |
| 10005 | E Fudd | 4545 53rd Street | Chicago | IL | 54545 | USA | E Fudd | NULL |
| 10006 | abc | 453 Main street | los | ca | 99045 | usa | NULL | NULL |
+---------+----------------+---------------------+-----------+------------+----------+--------------+--------------+---------------------+
6 rows in set (0.09 sec)
插入数据的一部分
mysql> insert into customers (cust_name, cust_address, cust_zip) values ('abc', '453 Main street', '99045');
Query OK, 1 row affected (0.03 sec)
案例1:插入多个行
方法1:复制多行
方法2:简化方法1
案例1:插入检索出的数据,也可以理解为复制表或备份
mysql> create table customers2 as select * from customers;
Query OK, 7 rows affected (0.04 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> delete from customers2;
Query OK, 7 rows affected (0.03 sec)
mysql> insert into customers2 select * from customers;
Query OK, 7 rows affected (0.03 sec)
Records: 7 Duplicates: 0 Warnings: 0