mysql常用DDL基本语法

mysql表结构字段增删改操作和索引等,示例如下:

1、创建数据库语法,示例:
【1】
create database if not exists db_name character set = utf8mb4;
【2】create database if not exists db_name character set = 'gb2312' collate = 'gb2312_chinese_ci';
【3】推荐

方式一(首推):
create database 数据库名称 charset utf8mb4 collate utf8mb4_general_ci;


方式二(推荐2):
create database 数据库名称 charset utf8mb4 collate utf8mb4_bin;


方式三:
create database 数据库名称 character set utf8mb4 collate utf8mb4_general_ci;

2、创建表结构语法,示例:

//示例1
create table if not exists t_table
(
    id          bigint         not null primary key auto_increment comment '主键id',
    user_name   varchar(100)   not null comment '姓名',
    pwd         varchar(100)   not null comment '密码',
    email       varchar(100)   null comment '邮箱',
    price       decimal(10, 2) null comment '价格',
    is_enable   tinyint(2)     null comment '是否有效(0否,1是)',
    remark      text           null comment '备注',

    deleted     tinyint(2)              default 0 not null comment '是否已删除,0-未删除,1-已删除',
    create_by   bigint comment '创建人Id',
    create_date datetime       not null default now() comment '创建时间',
    update_by   bigint comment '修改人Id',
    update_date datetime       not null default now() on update now() comment '修改时间'
)
    engine = InnoDB
    charset = utf8mb4
    collate = utf8mb4_general_ci
    auto_increment = 1
    comment '表名';




//示例2,以及对应java代码说明
create table lifehall_db.t_shop_trend_content
(
    id          bigint(20)                            not null comment '主键id' primary key,
    str         varchar(100)                          null comment '对应java中(String)',
    txtStr      text                                  null comment '对应java中(String)',
    total       bigint(25)                            null comment '对应java中(Long、Integer)',
    flag        tinyint(2)                            null comment '对应java中(int、Integer)',
    intTest     int(10)                               null comment '对应java中(int、Integer、Long)',
    dcTest      decimal(12, 2)                        null comment '对应java中(BigDecimal)',
    dtTest      datetime                              null comment '对应java中(Date)',
    create_time datetime    default CURRENT_TIMESTAMP not null comment '创建时间',
    creator_id  varchar(32) default 'sys'             not null comment '创建人',
    update_time datetime    default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    updater_id  varchar(32) default 'sys'             not null comment '更新人'
)
    engine = InnoDB
    charset = utf8mb4
    collate = utf8mb4_general_ci
    comment '表名';

-- 创建索引
create index idx_shop_trend_content_grp_one on lifehall_db.t_shop_trend_content (l1, l2);

3、表结构增加字段
alter table 表名 add 要增加的字段名 字段类型 是否为空 default 默认值 comment 字段描述 after 加到哪个字段之后;

4、删除字段:
ALTER TABLE 表名 DROP 字段名;

5、修改字段名称、类型等:
alter table 表名 change 原字段名 改过后的字段名 字段类型 是否为空 default 默认值 comment 字段描述;

6、备份某张表结构和数据
create table 备份表名 select * from 待备份表名;
insert into 目标表 select * from 源表名;

7、创建索引语法
【1】alter table 表 add index 索引名(字段1,字段2);
【2】create index 索引名称(idx_字段名) on 库名.表名 (字段1,字段2...);

mysql脚本常用的日期时间相关函数:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值