八 mysql語句的DDL語句

2022-07-09 20:24:11 字數 2668 閱讀 9242

ddl語句:資料定義語言

建立乙個不存在的資料庫

create database school;

create schema sch;

檢視支援的字符集

show charset;

檢視支援的校對規則

show collation;

完整建立乙個不存在的資料庫,建議的操作

create database test charset utf8; #建立乙個test庫,字符集為utf8

create database xyz charset utf8mb4 collate utf8mb4_bin; #建立乙個xyz庫,字符集為utf8mb4,校對規則為uft8mb4_bin(大小寫敏感,注意:校對規則必須與字符集一致)

建庫規範:

1.庫名不能有大寫字母

2.建庫要加字符集

3.庫名不能有數字開頭

4. 庫名要和業務相關

建庫標準語句

mysql>create database db charset utf8mb4;

mysql> show create database xuexiao; #檢視建立庫的語句

mysql>drop database ywx;

#物理刪除ywx庫及庫中的所有表

show create database school;

alter database school charset utf8;

注意:修改字符集,修改後的字符集一定是原字符集的嚴格超集(修改後的字符集一定要包含原字符集)

show databases;#檢視所有庫名

show create database ywx; #檢視建立ywx庫的建立語句

create table stu(

列1 屬性(資料型別、約束、其他屬性) ,

列2 屬性,

列3 屬性

);

use school;  #進入school資料庫

create table stu( #建立列及其屬性資訊

id int not null primary key auto_increment comment '學號'

,sname varchar(

255) not null comment '姓名'

,sage tinyint unsigned not null default

0 comment '年齡'

,sgender enum('m

','f

','n

') not null default '

n' comment '性別'

,sfz char(

18) not null unique comment '

身份證'

,intime timestamp not null default now() comment

'入學時間

') engine=innodb charset=utf8 comment '

學生表';

建表規範:

1

. 表名小寫

2. 不能是數字開頭

3. 注意字符集和儲存引擎

4. 表名和業務有關

5. 選擇合適的資料型別

6. 每個列都要有注釋

7. 每個列設定為非空,無法保證非空,用0來填充。

drop table t1;

物理刪除t1表的所有資訊

4.1新增列

在stu表中新增qq列

desc stu;

alter table stu add qq varchar(

20) not null unique comment '

qq號';

alter table stu add wechat varchar(64) not null unique  comment '

' after sname ;

在id列前加乙個新列num

alter table stu add num int not null comment '數字'

first;

desc stu;

4.2刪除列

把剛才新增的列都刪掉(危險)

alter table stu drop num;

alter table stu drop qq;

alter table stu drop wechat;

4.3修改列的屬性

修改sname資料型別的屬性

alter table stu modify sname varchar(128)  not null ;

將sgender 改為 sg 資料型別改為 char 型別

alter table stu change sgender sg char(1) not null default 'n'

;desc stu;

#change可以更改列的名稱,也可以更改列的屬性;modify只能更改列的屬性。

mysql的ddl的語句有 mysql語句之DDL

sql分類 ddl data definition languages 語句 資料定義語言,這些語句定義了不同的資料段 資料庫 表 列 索引等資料庫物件的定義。常用的語句關鍵字主要包括create drop alter等。dml data manipulation language 語句 資料操縱語...

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...