mysql 创建表语句

2020-09-21  本文已影响0人  苍老师的眼泪
create table `表名` (

第一列 数据类型[size] [约束1 约束2 约束3...],
第二列 数据类型[size] [约束1 约束2 约束3...] [DEFAULT 默认值],
PRIMARY KEY (列名),
FOREIGN KEY (充当外键的列) REFERENCES Persons(主键列),
CHECK (列明 比较运算符 条件值),
) engine = 数据库引擎;

示例:

create table student (
    id int unsigned not null auto_increment,
    school varchar(120),
    primary key (id)
) engine = InnoDB;

create table `fuck` (
    `id` int unsigned not null auto_increment,
    `name` varchar(40) not null DEFAULT 'edison',
    `stuID` int unsigned not null,
    PRIMARY KEY (id),
    foreign key (stuID) references student (id),
    check (stuID > 20)
) engine = InnoDB;

tips:

  1. auto_increment列必须是主键。
  2. unsigned 要放在not null前面。
  3. 外键必须和它引用的列有相同是数据类型。
  4. unsigned不是约束,要跟在数据类型后面。
上一篇 下一篇

猜你喜欢

热点阅读