MySQL常用DDL語句

2021-10-01 02:18:23 字數 1674 閱讀 8212

刪除表

-- 如果存在則刪除

drop table if exists t_user;

建立表 

-- 建立表

create table t_user(

id bigint(20) unsigned not null auto_increment,

user_code varchar(20) not null comment '系統號',

user_name varchar(50) default null comment '姓名',

*** varchar(2) default null comment '性別:m-男 f-女',

user_phone varchar(32) default null comment '**',

create_time timestamp not null default current_timestamp comment '記錄建立時間',

last_modify_time timestamp not null default current_timestamp on update current_timestamp comment '最後修改時間',

is_del tinyint(1) unsigned not null default '0' comment '邏輯刪除',

primary key (id),

key idx_t_user_user_code(user_code)

) comment='使用者基本資訊表';

重新命名表 

-- 重新命名表

rename table t to t_test;

新增字段

-- 在指定欄位後新增,如果新增在第一列可以用first

alter table t_user add address varchar(200) default null comment '位址' after user_phone;

修改字段

-- 可修改字段型別、長度、預設值以及注釋

alter table t_user modify address varchar(300) default '' comment '詳細位址';

-- 可修改欄位名、字段型別、長度、預設值以及注釋

alter table t_user change address full_address varchar(500) default 'a' comment '完整位址';

刪除字段

alter table t_user drop full_address;
新增索引

-- 新增唯一索引

alter table t_user add unique uk_t_user_user_code(user_code);

-- 新增普通索引

alter table t_user add key idx_t_user_user_code(user_code);

刪除索引

-- 刪除索引

alter table t_user drop index uk_t_user_user_code;

常用DDL語句

1 修改欄位名稱 alter table t user login log change device id session id varchar 256 2 修改注釋 alter table t user login log modify column session id varchar 256...

mysql優化 ddl語句

mysql優化 ddl語句 mysql優化 ddl語句 在drop table維護mysql資料庫時,在drop操作期間,整個系統會被hang住,這個hang的時間的長短與buffer pool的大小相關。主要原因在於innodb在drop table時,會連續兩次遍歷buf pool lru 鍊錶...

MySQL 基礎語句 DDL

ddl語言 邏輯庫 資料表 檢視 索引 dml語言 新增 修改 刪除 查詢 dcl語言 使用者 許可權 事務 以下是ddl命令 建立 修改 刪除資料庫 使用資料庫 use test01 建立表和字段 create table student id int unsigned primary key,n...