Mysql语法之插入数据

作者: etron_jay | 来源:发表于2019-08-10 16:25 被阅读2次

一、数据插入

INSERT是用来插入(或添加)行到数据库表的。插入可以用几种方式使用:

  • 插入完整的行;
  • 插入行的一部分;
  • 插入多行;
  • 插入某些查询的结果;

二、插入完整的行

insert into customers
values(NULL,
'Pep E. LaPew',
'100 Main Street',
'Los Angeles',
'CA',
'90046',
'USA',
'NULL',
NULL);

对每个列必须提供一个值。如果某个列没有值,应该使用NULL值

上面的语法很简单,但是高度依赖于表中列的定义次序。

INSERT语句一般不会产生输出

insert into customers(cust_name,
                      cust_address,
                      cust_City,
                      cust_state,
                      cust_zip,
                      cust_country,
                      cust_contact,
                      cust_email)
                    values(NULL,
                           'Pep E. LaPew',
                           '100 Main Street',
                           'Los Angeles',
                           'CA',
                           '90046',
                           'USA',
                           'NULL',
                           NULL);

此例子完成与前一个INSERT语句完全相同的工作,但在表名后的括号里明确地给出了列名。

三、插入多个行

insert into customers(cust_name,
                      cust_address,
                      cust_City,
                      cust_state,
                      cust_zip,
                      cust_country,
                      cust_contact,
                      cust_email)
                    values(NULL,
                           'Pep E. LaPew',
                           '100 Main Street',
                           'Los Angeles',
                           'CA',
                           '90046',
                           'USA',
                           'NULL',
                           NULL),
                           
                           (NULL,
                           'Pep E. LaPew',
                           '100 Main Street',
                           'Los Angeles',
                           'CA',
                           '90046',
                           'USA',
                           'NULL',
                           NULL);

四、插入检索出的数据

INSERT还存在一种形式,可以利用它将一条SELECT语句的结果插入表中。这就是所谓的INSERT SELECT。

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;

相关文章

网友评论

    本文标题:Mysql语法之插入数据

    本文链接:https://www.haomeiwen.com/subject/osvrjctx.html