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)