MySQL-DDL-库和表的管理

库和表的管理

一、库的管理

创建、修改、删除

二、表的管理

创建、修改、删除

创建:create

修改:alter

删除:drop

一、库的管理

  1. 库的创建

    语法:

    create database [if not exists] 库名;

    create database if not exists books;

  2. 库的修改
    rename database books to 新库名(版本可能不支持)

    改变库的字符集

    alter database books character set gbk;

  3. 库的删除

    drop database if exists books

二、表的管理

  1. 表的创建
    语法:

    create table 表名(

    ​ 列名 列的类型【长度 约束】,

    ​ 列名 列的类型【长度 约束】,

    ​ 列名 列的类型【长度 约束】,

    ​ 列名 列的类型【长度 约束】

    )

  2. 表的修改

    alter table 表名 add|drop|modify|change column 列名 【列类型 约束】

    alter table 表名 rename to 新名称

    alter table 表名 add column 列名 【列类型 约束】 【first|after 列名】

  3. 表的删除

    drop table if exists book_author;

​ 通用写法:

​ drop database if exists 旧库名;

​ create database 新库名;

​ drop table if exists 旧表名;

​ create table 表名();

  1. 表的复制

    1. 仅仅复制表的结构
      create table 新表名 like 表名;

    2. 复制表的结构+数据
      create table 表名
      select * from author;

    3. 只复制部分数据
      create table 表名
      select id,au_name
      from author
      where nation='中国';

    4. 仅仅复制某些字段
      create table 表名

      select id,au_name

      from author

      where 0;

posted @ 2020-09-17 10:52  LongSL  阅读(148)  评论(0)    收藏  举报