5.查看表中我们上面添加的记录: mysql> select * from tb1; +-----------+------+----------+ | username | age | salary | +-----------+------+----------+ | Tom | 25 | 8000.25 | | 杨海迎 | 23 | 10000.25 | | 李玉 | 21 | 10000.25 | | John | NULL | 4500.88 | +-----------+------+----------+ 4 rows in set (0.00 sec)
6.空值与非空值 NULL,字段值可以为空 NOT NULL,字段值禁止为空 我们新创建一张表指定表结构 mysql> CREATE TABLE tb2( -> username VARCHAR(20) NOT NULL, -> age TINYINT UNSIGNED NULL -> ); Query OK, 0 rows affected (0.13 sec) mysql> SHOW COLUMNS FROM tb2; +----------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+-------+ | username | varchar(20) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | +----------+---------------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> INSERT tb2 VALUES('TOM',NULL); Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM tb2; +----------+------+ | username | age | +----------+------+ | TOM | NULL | +----------+------+ 1 row in set (0.00 sec) 如果我们将username字段的值赋为NULL,系统会提示错误,记录不能被插入。
7.自动编号: AUTO_INCREMENT 自动编号,且必须与主键组合使用 默认情况下,起始值为1,每次的增量为1 mysql> CREATE TABLE tb3( -> id SMALLINT UNSIGNED AUTO_INCREMENT, -> username VARCHAR(30) NOT NULL, -> ); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 4 mysql> 输出上述错误,是因为我们没有定义主键,这里我们需要定义一个主键
mysql> SHOW COLUMNS FROM tb3; +----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(30) | NO | | NULL | | +----------+----------------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
mysql> SHOW COLUMNS FROM tb4; +----------+----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+-------+ | id | smallint(5) unsigned | NO | PRI | NULL | | | username | varchar(20) | NO | | NULL | | +----------+----------------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
mysql> SELECT * FROM tb4; +----+----------+ | id | username | +----+----------+ | 4 | TOM | | 22 | JOHN | | 23 | Rose | +----+----------+ 3 rows in set (0.00 sec)
mysql> SHOW COLUMNS FROM tb5; +----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | +----------+----------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
mysql> SHOW COLUMNS FROM tb6; +----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | sex | enum('1','2','3') | YES | | 3 | | +----------+----------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
mysql> SELECT * FROM tb6; +----+----------+------+ | id | username | sex | +----+----------+------+ | 1 | TOM | 3 | //我们可以看到我们的性别为默认值3 +----+----------+------+ 1 row in set (0.00 sec)