结合上一节讲的,这一节我们将如何创建数据库中的表
创建数据库表
格式: create table 表名称(列声明);
举个栗子
mysql> create table phone_test(
-> id int primary key not null auto_increment,
-> name char(16) not null,
-> prince smallint not null default 2018,
-> information char(128)
-> );
- primary key:表的主键。本列的值不可以重复,必须唯一。
- auto_increment:需在整数列中使用,其作用是在插入数据时若该列为NULL,MySQL将自动产生一个比现存值更大的唯一标识符值。MySQL表中只能有一个auto_increment字段,而且这个字段必须被定义为键。
- not null:说明改列的值不能为空,必须要填。若不声明,则说明可以为null。
- default:当你忘记传入该字段的值时,MySQL会自动为你设置该字段的值。
提示
- 可以使用 show tables 来查看当前数据库中的表。
- 可以使用 desc 表名 来查看该表的详细信息。
> mysql> desc phone_test;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(16) | NO | | NULL | |
| prince | smallint(6) | NO | | 2018 | |
| infomation | char(128) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+