mysql中的表操作

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

mysql中的表操作

RunWithYou   2019-12-24 我要评论

创建数据库

  create database 数据库名

切换数据库

  use 数据库名

建表:

  create table 表名 (

    字段名1,类型,约束

    字段名2,类型,约束

    ...

  )

约束:

 1.主键约束

  1)直接在建表时字段类型后加 primary key

  2)在表最后加 constraint 约束名 primary key(字段名)

  3)表外修改 alter table 表名 add constraint 约束名 primary key(字段名)

 2.检查约束

  1)直接在建表类型后加 check(约束条件)

  2)在表最后加 constraint 约束名 check(约束条件)

  3)表外修改 alter table 表名 add constraint 约束名 check(约束条件)

 3.非空约束

  1)直接在创建表的类型后加 not null

  2) 在表最后加入 constraint 约束名 check(字段名 is not null)

  3)在表外修改 alter table 表名 modify 字段名 字段类型 not null

 4.唯一约束

  1)直接在创建表的类型后加 unique

  2) 在表的最后加入 constraint 约束名 unqiue(字段名)

  3) 在表外修改 alter table 表名 add constraint 约束名 unique(字段名)

 5.外键约束

  1)直接在创建表的类型后加 references 父表名(父表主键名)

  2)在表的最后加入 constraint 约束名 foreign key(字段名) references 父表名(父表主键名)

  3)在表外修改 alter table 表名 add constraint 约束名 foreign key(字段名) references 父表名(父表主键名)on delete set null on updata cascade

 6.默认约束

  alter table 表名

删除约束

  alter table 表名 drop constraint 约束名

 

表的修改

  1)添加字段

    alter table 表名 add 字段名 字段类型

  2)删除字段

    alter table 表名 drop 字段名

  3)修改字段类型

    alter table 表名 modify 字段名 新字段类型

  4)修改字段名

    alter table 表名 change 字段名 新字段名 字段类型

  5)修改表名

    alter table 表名 rename as 新表名

  6)删除表

    drop table 表名

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们