欢迎投稿

今日深度:

mysql中的表操作,mysql使用表

mysql中的表操作,mysql使用表


------------恢复内容开始------------

创建数据库

  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(约束条件)

  注:mysql不支持检查约束,但是写上检查约束不会报错

 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.默认约束

  1)直接在创建表的类型后加 default 默认值

  2)在表外修改 alter table 表名 add constraint 约束名 

删除约束

  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 表名

 

 查看当前数据库中所有表

  show tables

www.htsjk.Com true http://www.htsjk.com/Mysql/40433.html NewsArticle mysql中的表操作,mysql使用表 ------------恢复内容开始------------ 创建数据库 create database 数据库名 切换数据库 use 数据库名 建表: create table 表名 ( 字段名1,类型,约束 字段名2,类型,...
相关文章
    暂无相关文章
评论暂时关闭