mysql数据库常用基本sql语句(库、表)

2023年 7月 30日 86.8k 0

show global variables like ‘port’;

查看mysql使用的端口是多少

库:

  • show databases;
  • 显现所有数据库

  • create database [if not exists] 数据库名;
  • 创建数据库

  • drop database [if exists] 数据库名;
  • 删除数据库

  • show create database 数据库名;
  • 显示创建数据库语句

  • select database();
  • 查看当前是连接哪个数据库

  • use 数据库名;
  • 使用数据库

    表:

  • show tables;
  • 查询当前数据库中的全部表

  • show create table 表名;
  • 显示创建表的语句

  • describe/desc 表名;
  • 查看表结构

  • drop table [if not exists] 表名;
  • 删除表

  • alter table 原表名 rename to 新表名;
  • 修改表名

  • alter table 表名 add 字段名 字段类型;
  • 添加字段

  • alter table 表名 change 原始字段名 新的字段名 新数据类型 [默认值] [注释];
  • 修改字段名、字段数据类型、【默认值】、【注释】

    例:alter table table01 change id id01 int default 10 comment ‘序号’;

    –将table01表中的字段id改名为id01,数据类型为int,默认值为10,注释为“序号”

  • alter table 表名 modeify [column] 字段名 字段类型 [默认值] [注释];
  • 修改字段的数据类型、【默认值】、【注释】

  • alter table 表名 drop [column] 字段名
  • 删除字段

    扩展:

    注释:

  • select table_name 表名,table_comment 表注释,table_schema
  • from information_schema.tables

    where table_schema = ‘数据库名’

    order by table_name;

    查看数据库中的所有表注释

  • select table_name 表名,table_comment 表注释,table_rows 数据量
  • from information_schema.tables

    where table_schema = ‘数据库名’

    order by table_name;

    查看数据库下的所有表的表名、表注释及其数据量

  • select column_name 字段名,column_comment 字段注释
  • from information_schema.columns

    where table_name=’表名’ and table_schema=’数据库名’;

    查看数据库下的表的表字段注释

  • select a.table_name 表名,a.table_comment 表注释,b.column_name
  • 表字段,b.column_type 字段类型,b.column_comment 字段注释

    from information_schema.tables a,information_schema.columns b

    where b.table_name=a.table_name and a.table_schema=’数据库名’;

    查看数据库下的所有表的表名、表注释以及对应表字段注释

  • alter table 表名 comment ‘注释内容’;
  • 修改表注释

  • alter table 表名 modify column 字段名 字段类型 comment ‘注释内容’;
  • 修改表的表字段注释

    注意:字段名和字段类型照写就行,如果之前有规定长度 这里也要指定一下

    相关文章

    Oracle如何使用授予和撤销权限的语法和示例
    Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
    下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
    社区版oceanbase安装
    Oracle 导出CSV工具-sqluldr2
    ETL数据集成丨快速将MySQL数据迁移至Doris数据库

    发布评论