第十五课 插入数据

第十五课 、插入数据

        预习与回顾:

        第十四课学习如何用UNION操作符来组合SELECT语句---利用UNION,可以把多条查询的结果作为一条组合查询返回,不管结果中有无重复;第十五课学习如何利用SQL的INSERT语句将数据插入表中。

        15.1、数据插入

        15.1.1、插入完整的行

        要求指定表名和插入到新行中的值。

输入▼
INSERT INTO Customers
VALUES('1000000006',
                 'Toy Land',
                 '123 Any Street',
                 'New York',
                 'NY',
                 '11111',
                 'USA',
                 NULL,
                 NULL);

          将一个新顾客插入到Customers表中。存储到表中每一列的数据在VALUES子句中给出,!!必须给每一列提供一个值。如果某列没有值,如上面的cust_contactcust_email列,则应该使用NULL值(假定表允许对该列指定空值)。各列必须以它们在表定义中出现的次序填充。!!

输入▼
INSERT INTO Customers(cust_id,
                                           cust_name,
                                           cust_address,
                                            cust_city,
                                           cust_state,
                                           cust_zip,
                                           cust_country,
                                           cust_contact,
                                           cust_email)
                          VALUES( '1000000006',
                                           'Toy Land',
                                           '123 Any Street',
                                           'New York',
                                           'NY',
                                           '11111',
                                            'USA',
                                           NULL,
                                           NULL);---在表名后的括号里明确给出了列名。

输入▼
INSERT INTO Customers(cust_id,
                                           cust_contact,
                                           cust_email,
                                           cust_name,
                                           cust_address,
                                            cust_city,
                                           cust_state,
                                           cust_zip)
VALUES('1000000006',
                 NULL,
                 NULL,
                 'Toy Land',
                 '123 Any Street',
                 'New York',
                 'NY',
                 '11111');--- 给出了列名,以一种不同的次序填充

         15.1.2、插入部分行

输入▼
INSERT INTO Customers(cust_id,
                                           cust_name,
                                           cust_address,
                                           cust_city,
                                           cust_state,
                                           cust_zip,
                                            cust_country)
VALUES('1000000006',
                 'Toy Land',
                 '123 Any Street',
                 'New York',
                 'NY',
                 '11111',
                 'USA');

        没有给cust_contact和cust_email这两列提供值,因此,这里的INSERT语句省略了这两列及其对应的值。

        省略列    该列定义为允许NULL值(无值或空值)。
        在表定义中给出默认值。这表示如果不给出值,将使用默认值。

        15.1.3插入检索出的数据

        利用它将SELECT语句的结果插入表中---INSERT SELECT。

输入
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;

        INSERT SELECT中的列名---DBMS一点儿也不关心SELECT返回的列名。它使用的是列的位置,因此SELECT中的第一列(不管其列名)将用来填充表列中指定的第一列,第二列将用来填充表列中指定的第二列,如此等等。

        15.2、从一个表复制到另一个表

SELECT INTO语句导入数据,而INSERT SELECT是导出数据。

输入▼
SELECT *
INTO CustCopy
FROM Customers;
分析▼---ELECT语句创建一个名为CustCopy的新表,并把Customers表的整个内容复制到新表中。

        任何SELECT选项和子句都可以使用,包括WHEREGROUP BY
        可利用联结从多个表插入数据;
        不管从多少个表中检索数据,数据都只能插入到一个表中。

你可能感兴趣的:(SQL基础学习)