打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Access sql语句创建表及字段类型
SQL初级----创建表和约束
MySQL常用基本操作
SQL的主键和外键的作用
Mysql基础02-约束
MySQL - DDL -常见约束
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服