常用mysql語句 常用MySql語句

2021-10-17 12:36:42 字數 1284 閱讀 4383

新建資料表

drop table if exists ga_game_way;

create table `ga_game_way` (

`id` int(11) unsigned not null auto_increment comment 'id',

primary key (`id`) using btree, -- 主鍵

unique index `uq_username`(`username`) using btree, -- 唯一索引

index `index_pid`(`pid`) using btree, -- 普通索引

constraint fk_bt_game_id

foreign key (game_id)

references ga_game_sys(id)

on delete cascade

on update cascade

-- 外來鍵索引

) engine=innodb auto_increment=1 default charset=utf8mb4 collate=utf8mb4_unicode_ci row_format=dynamic comment='表注釋';

no action , set null , set default ,cascade

no action (預設)表示 不做任何操作,不能刪除資料,提示錯誤

set null 表示在外鍵表中將相應字段設定為null

set default 表示設定為預設值(restrict)

cascade 表示級聯操作,就是說,如果主鍵表中被參考字段更新,外來鍵表中也更新,主鍵表中的記錄被刪除,外來鍵表中改行也相應刪除

索引新建索引

-- 唯一索引

alter table ga_user add unique uq_username(username)

-- 普通索引

alter table ga_user add index index_pid(pid)

刪除索引

alter table ga_user drop

啟用、禁用外來鍵約束

set names utf8mb4;

set foreign_key_checks = 0; -- 禁用外來鍵約束

set foreign_key_checks = 1; -- 啟用外來鍵約束

優化-- 檢視碎片

show table status like 'ga_admin';

-- 優化表:

optimize table ga_admin;

常用MySQL語句

1.庫的操作 2.表的操作 3.索引的操作 4.檢視的操作 5.資料的操作 建立資料庫 create database database name 檢視資料庫 show databases 選擇資料庫 use database name 刪除資料庫 drop database database na...

常用MySql語句

alter table rbac user add name varchar 20 not null 在乙個表中增加一條字段 alter table rbac user drop name 刪除乙個字段 insert into rbac user1 select from rbac user 插入多...

常用Mysql 語句

1 建立 資料庫 create database 資料庫名 建立資料庫 db database drop database ifexists db database create database db database default character set utf8 collate utf8...