mysql> create table tt10 ( -> name varchar(20) not null, -> age tinyint unsigned default -> sex char(2) default '男');
列描述
comment,没有实际含义,专门用来描述字段,在创建表时保存
1 2 3 4
mysql> create table tt12 ( -> name varchar(20) not null comment '姓名', -> age tinyint unsigned default 0 comment '年龄', -> sex char(2) default '男' comment '性别');
comment通过desc看不到注释信息,需要通过show关键字查询
1 2 3 4 5 6 7 8 9 10
mysql> show create table tt12/G; 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 '/G' at line 1 mysql> show create table tt12\G; *************************** 1. row *************************** Table: tt12 Create Table: CREATE TABLE `tt12` ( `name` varchar(20) NOT NULL COMMENT '姓名', `age` tinyint(3) unsigned DEFAULT '0' COMMENT '年龄', `sex` char(2) DEFAULT '男' COMMENT '性别' ) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql> create table tt13( -> id int unsigned primary key comment '学号不能为空', -> name varchar(20) not null); Query OK, 0 rows affected (0.04 sec)
mysql> desc tt13; 主键 +-------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | +-------+------------------+------+-----+---------+-------+ 2 rows in set (0.01 sec)
复合主键,可以在创建表的最后,使用复合主键来设置多个字段
1 2 3 4 5 6 7 8 9 10 11 12 13
mysql> create table tt14( -> id int unsigned,-> course char(10) comment '代码', -> score tinyint unsigned default 60 comment '成绩', -> primary key(id, course) -- id和course为复合 ->); mysql> desc tt14; +--------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------------+------+-----+---------+-------+ | id |int(10) unsigned | NO | PRI | 0 | | | course | char(10) | NO | PRI | | | | score | tinyint(3) unsigned | YES | | 60 | | +--------+---------------------+------+-----+---------+-------+