1.创建表
CREAT TABLE tabel_name
{
字段名称 数据类型 可选择的约束条件,
column dataType contrai,
--主键说明可以放在字段中单独说明 也可以放在最后统一说明
PRIMARY KEY(one or more columns)
};
提示 auto_incremert 表示自动增长
--查看当前数据库中所有表
show tables;
int unsigned 无符号整型
auto_increment 表示自动增长
not null 表示不能为空
primary key 表示主键
default 默认值
实例:
1.创建班级表:
mysql> create table classed(
-> id int unsigned primary key auto_increment,
-> name varchar(10) not null,
-> num int(4)
-> );
Query OK, 0 rows affected (0.00 sec)
2.创建学生表:
mysql> create table students(
-> id int unsigned primary key auto_increment,
-> name varchar(20) not null,
-> age tinyint(1),
-> high decimal(3,2),
-> gender enum("man","woman"),
-> cls_id int unsigned
-> );
Query OK, 0 rows affected (0.00 sec)
3.查看表结构:
mysql> desc students;
+--------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(1) | YES | | NULL | |
| high | decimal(3,2) | YES | | NULL | |
| gender | enum('man','woman') | YES | | NULL | |
| cls_id | int(10) unsigned | YES | | NULL | |
+--------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
4.查看创建的所有表:
mysql> show tables;
+-----------------------+
| Tables_in_school_data |
+-----------------------+
| classed |
| classes |
| students |
+-----------------------+
3 rows in set (0.00 sec)