小言_互联网的博客

MySql之DDL 数据库定义语言

379人阅读  评论(0)

DDL 数据库定义语言

前言:
DDL 数据库定义语言 操作库(删除和创建)、操作表(删除、创建、修改)

库的操作

1.show datebase; 查看当前数据库系统下所有的数据库
2.create database 库名; 创建数据库
3.use 库名; 选中数据库,对数据库的表进行操作前要先选中库
4.show tables; 查看当前库中的所有表
5.drop database 库名; 删除库

表的操作

  1. create table 表名(字段名1 类型 【约束】,字段名2 类型 【约束】,…); 创建表
  2. drop table 表名; 删除表
  3. desc 表名; 查看表结构
  4. 查看创建表的语句
    show create table 表名;

    5.修改表名
    alter table 原表名 rename 新表名;

    6.添加字段
    alter table 表名 add 字段名 类型 【约束】;

    7.更改字段
    alter table 表名 change 原字段名 新字段名 类型 【约束】;

    8.修改字段约束
    alter table 表名 modify 字段名 修改后类型 【约束】;

    9.约束的处理
    (1). 删除主键
    alter table 表名 drop primary key;

    (2). 添加主键
    alter table 表名 modify 字段名 类型 primary key;

    (3). 删除唯一键
    alter table 表名 drop index 字段名;

    (4).添加唯一约束
    alter table 表名 modify 字段名 类型 unique;

    (5). 删除外键
    alter table 表名 drop foreign key 外键名;

    注意:创建外键的时候mysql数据库 会自定创建一个同名索引,删除外键时要将索引一起删除
    show index from 表名; 查看当前表的所有索引: 主键索引 唯一索引
    alter table 表名 drop index 字段名;删除索引
  5. 添加外键
    alter table 表名 add constraint 外键名 foreign key(字段名) references 表名(字段名);

转载:https://blog.csdn.net/weixin_45373827/article/details/100935822
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场