美文网首页
mysql 创建表语句

mysql 创建表语句

作者: 苍老师的眼泪 | 来源:发表于2020-09-21 16:23 被阅读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不是约束,要跟在数据类型后面。

相关文章

网友评论

      本文标题:mysql 创建表语句

      本文链接:https://www.haomeiwen.com/subject/lgwvyktx.html