MySQL的INSERT文法小結

來源:互聯網
上載者:User


mysql> create table addressbook(fname varchar(255) null,lname varchar(255) null,

phone varchar(255) null,fax varchar(255) null,email varchar(255) null);

Query OK, 0 rows affected (0.11 sec)

 

mysql> desc addressbook;

+-------+--------------+------+-----+---------+-------+

| Field | Type        | Null | Key | Default | Extra |

+-------+--------------+------+-----+---------+-------+

| fname | varchar(255) | YES |    | NULL   |      |

| lname | varchar(255) | YES |    | NULL   |      |

| phone | varchar(255) | YES |    | NULL   |      |

| fax  | varchar(255) | YES |    | NULL   |      |

| email | varchar(255) | YES |    | NULL   |      |

+-------+--------------+------+-----+---------+-------+

5 rows in set (0.06 sec)

 

當向表中插入字串值(以及一些資料值時)。必須使用引號,否則MySQL認為它們是欄位名:

mysql> insert into addressbook(fname,lname,phone,fax,email)

   -> values('Rob',Rabbit,'674 1536','382 8364','rob@some.domain');

ERROR 1054 (42S22): Unknown column 'Rabbit' in 'field list'

 

包含引用標誌的值需要在前面加上反斜線,不過數值不需要加引號:

mysql> insert into addressbook(fname,lname,phone,fax,email)

   -> values('France','D\'Souza','123 4567','000 7574','fdz@some.domain');

Query OK, 1 row affected (0.05 sec)

 

mysql> select * from addressbook;

+--------+---------+----------+----------+-----------------+

| fname | lname  | phone   | fax     | email          |

+--------+---------+----------+----------+-----------------+

|France| D'Souza | 123 4567 | 000 7574 | fdz@some.domain |

+--------+---------+----------+----------+-----------------+

1 row in set (0.00 sec)

 

在一個單獨的Insert語句中使用多個VALUES()子句可以插入多條記錄:

mysql> insert stocks (symbol,price,quantity) values('ABCD',100,4500),

   -> ('HYDH',2000,29),('UGTS',25,67);

 

也可以不使用INSERT。。。VALUES格式,而是使用類似的UPDATE語句,它使用SET語句分別為每一列設定值。故可以不進行下面的操作:

mysql> insert into stocks(symbol,price,quantity) values('HYDH',2000,29);

而是這樣做:

mysql> insert into stocks set symbol='HYDH',price=2000,quantity=29;

 

 

使用DEFAULT值

mysql> create table forums(name varchar(150) not null,category varchar(50) defau

lt 'UNIX' not null,postsperpage smallint default 15 not null,highlightcolor varc

har(10) default 'red' not null);

Query OK, 0 rows affected (0.11 sec)

 

mysql> insert into forums(name) values('Apache');

Query OK, 1 row affected (0.05 sec)

 

mysql> insert into forums(name,highlightcolor) values('Sendmail','green');

Query OK, 1 row affected (0.06 sec)

 

mysql> select * from forums;

+----------+----------+--------------+----------------+

| name    | category | postsperpage | highlightcolor |

+----------+----------+--------------+----------------+

| Apache  | UNIX    |          15 | red           |

| Sendmail | UNIX    |          15 | green         |

+----------+----------+--------------+----------------+

2 rows in set (0.00 sec)

 

MySQL4.0.3以及更高版本支援DEFAULT關鍵字,MySQL使用這個關鍵字為記錄設定列的預設值。

mysql> insert into forums(name,category,postsperpage,highlightcolor)

   -> values('MySQL',default,default,default);

Query OK, 1 row affected (0.06 sec)

 

使用AUTOINCREMENT欄位

當使用AUTOINCREMENT列建立序列時,在INSERT語句中不使用欄位名將導致MySQL自動產生序列的下一個序號。這個序號作為表的主鍵。

mysql> insert into users(uname,upass) values('jim','secret');

Query OK, 1 row affected (0.05 sec)

 

mysql> insert into users(uname,upass) values('sarah','opense');

Query OK, 1 row affected (0.06 sec)

 

mysql> insert into users(uname,upass) values('tim','whiteboard');

Query OK, 1 row affected (0.05 sec)

 

mysql> select * from users;

+-----+-------+------------+

| uid | uname | upass     |

+-----+-------+------------+

|  1 | jim  | secret    |

|  2 | sarah | opense    |

|  3 | tim  | whiteboard |

+-----+-------+------------+

3 rows in set (0.00 sec)

使用NULL值

我們可以在INSERT語句中使用NULL關鍵字來向一個欄位(只要沒有標記為NOT NULL)輸入NULL值。下面的例子通過向包含NULL值的email和fax欄位的addressbook表添加記錄進行說明:

mysql> insert into addressbook(fname,lname,phone,fax,email)

   -> values('Polly','Parrot','239 1828',NULL,NULL);

Query OK, 1 row affected (0.05 sec)

 

mysql> select * from addressbook where email is null or fax is null;

+-------+--------+----------+------+-------+

| fname | lname | phone   | fax | email |

+-------+--------+----------+------+-------+

| Polly | Parrot | 239 1828 | NULL | NULL |

+-------+--------+----------+------+-------+

1 row in set (0.02 sec)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.