③:MySQL中 DDL 常見操作命令匯總

2021-12-29 程式設計師路人

大家好,我是路人。

對 mysql 感興趣的,加一下我的微信 itsoku,拉你入群交流技術。

這是 Mysql 系列第 4 篇。

環境:mysql5.7.25,cmd 命令中進行演示。

DDL 介紹

DDL:Data Define Language 數據定義語言,主要用來對資料庫、表進行一些管理操作。

如:建庫、刪庫、建表、修改表、刪除表、對列的增刪改等等。

文中涉及到的語法用[]包含的內容屬於可選項,下面做詳細說明。

庫的管理創建庫
create database [if not exists] 庫名;

刪除庫
drop databases [if exists] 庫名;

建庫通用的寫法
drop database if exists 舊庫名;
create database 新庫名;

示例
mysql> show databases like 'javacode2018';
++
| Database (javacode2018) |
++
| javacode2018            |
++
1 row in set (0.00 sec)

mysql> drop database if exists javacode2018;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases like 'javacode2018';
Empty set (0.00 sec)

mysql> create database javacode2018;
Query OK, 1 row affected (0.00 sec)

show databases like 'javacode2018';列出javacode2018庫信息。

表管理創建表
create table 表名(
    欄位名1 類型[(寬度)] [約束條件] [comment '欄位說明'],
    欄位名2 類型[(寬度)] [約束條件] [comment '欄位說明'],
    欄位名3 類型[(寬度)] [約束條件] [comment '欄位說明']
)[表的一些設置];

注意:

類型是用來限制 欄位 必須以何種數據類型來存儲記錄類型其實也是對欄位的約束(約束欄位下的記錄必須為 XX 類型)類型後寫的 約束條件 是在類型之外的 額外添加的約束

約束說明

not null:標識該欄位不能為空

mysql> create table test1(a int not null comment '欄位a');
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test1 values (null);
ERROR 1048 (23000): Column 'a' cannot be null
mysql> insert into test1 values (1);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test1;
+---+
| a |
+---+
| 1 |
+---+
1 row in set (0.00 sec)

default value:為該欄位設置默認值,默認值為 value

mysql> drop table IF EXISTS test2;
Query OK, 0 rows affected (0.01 sec)

mysql> create table test2(
    ->   a int not null comment '欄位a',
    ->   b int not null default 0 comment '欄位b'
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into test2(a) values (1);
Query OK, 1 row affected (0.00 sec)

mysql> select *from test2;
+---+---+
| a | b |
+---+---+
| 1 | 0 |
+---+---+
1 row in set (0.00 sec)

上面插入時未設置 b 的值,自動取默認值 0

primary key:標識該欄位為該表的主鍵,可以唯一的標識記錄,插入重複的會報錯

兩種寫法,如下:

方式 1:跟在列後,如下:

mysql> drop table IF EXISTS test3;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create table test3(
    ->   a int not null comment '欄位a' primary key
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test3 (a) values (1);
Query OK, 1 row affected (0.01 sec)

mysql> insert into test3 (a) values (1);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

方式 2:在所有列定義之後定義,如下:

mysql> drop table IF EXISTS test4;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create table test4(
    ->   a int not null comment '欄位a',
    ->   b int not null default 0 comment '欄位b',
    ->   primary key(a)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into test4(a,b) values (1,1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test4(a,b) values (1,2);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

插入重複的值,會報違法主鍵約束

方式 2 支持多欄位作為主鍵,多個之間用逗號隔開,語法:primary key(欄位 1,欄位 2,欄位 n),示例:

mysql> drop table IF EXISTS test7;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>
mysql> create table test7(
    ->    a int not null comment '欄位a',
    ->    b int not null comment '欄位b',
    ->   PRIMARY KEY (a,b)
    ->  );
Query OK, 0 rows affected (0.02 sec)

mysql>
mysql> insert into test7(a,b) VALUES (1,1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test7(a,b) VALUES (1,1);
ERROR 1062 (23000): Duplicate entry '1-1' for key 'PRIMARY'

foreign key:為表中的欄位設置外鍵

語法:foreign key(當前表的列名) references 引用的外鍵表(外鍵表中欄位名稱)

mysql> drop table IF EXISTS test6;
Query OK, 0 rows affected (0.01 sec)

mysql> drop table IF EXISTS test5;
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> create table test5(
    ->   a int not null comment '欄位a' primary key
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql>
mysql> create table test6(
    ->   b int not null comment '欄位b',
    ->   ts5_a int not null,
    ->   foreign key(ts5_a) references test5(a)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test5 (a) values (1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test6 (b,test6.ts5_a) values (1,1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test6 (b,test6.ts5_a) values (2,2);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`javacode2018`.`test6`, CONSTRAINT `test6_ibfk_1` FOREIGN KEY (`ts5_a`) REFERENCES `test5` (`a`))

說明:表示 test6 中 ts5_a 欄位的值來源於表 test5 中的欄位 a。

注意幾點:

被插入的值在外鍵表必須存在,如上面向 test6 中插入 ts5_a 為 2 的時候報錯了,原因:2 的值在 test5 表中不存在

unique key(uq):標識該欄位的值是唯一的

支持一個到多個欄位,插入重複的值會報違反唯一約束,會插入失敗。

定義有 2 種方式。

方式 1:跟在欄位後,如下:

mysql> drop table IF EXISTS test8;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>
mysql> create table test8(
    ->    a int not null comment '欄位a' unique key
    ->  );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> insert into test8(a) VALUES (1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test8(a) VALUES (1);
ERROR 1062 (23000): Duplicate entry '1' for key 'a'

方式 2:所有列定義之後定義,如下:

mysql> drop table IF EXISTS test9;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>
mysql> create table test9(
    ->    a int not null comment '欄位a',
    ->   unique key(a)
    ->  );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> insert into test9(a) VALUES (1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test9(a) VALUES (1);
ERROR 1062 (23000): Duplicate entry '1' for key 'a'

方式 2 支持多欄位,多個之間用逗號隔開,語法:primary key(欄位 1,欄位 2,欄位 n),示例:

mysql> drop table IF EXISTS test10;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>
mysql> create table test10(
    ->   a int not null comment '欄位a',
    ->   b int not null comment '欄位b',
    ->   unique key(a,b)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> insert into test10(a,b) VALUES (1,1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test10(a,b) VALUES (1,1);
ERROR 1062 (23000): Duplicate entry '1-1' for key 'a'

auto_increment:標識該欄位的值自動增長(整數類型,而且為主鍵)

mysql> drop table IF EXISTS test11;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>
mysql> create table test11(
    ->   a int not null AUTO_INCREMENT PRIMARY KEY comment '欄位a',
    ->   b int not null comment '欄位b'
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> insert into test11(b) VALUES (10);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test11(b) VALUES (20);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test11;
+---+----+
| a | b  |
+---+----+
| 1 | 10 |
| 2 | 20 |
+---+----+
2 rows in set (0.00 sec)

欄位 a 為自動增長,默認值從 1 開始,每次+1

關於自動增長欄位的初始值、步長可以在 mysql 中進行設置,比如設置初始值為 1 萬,每次增長 10

注意:

自增長列當前值存儲在內存中,資料庫每次重啟之後,會查詢當前表中自增列的最大值作為當前值,如果表數據被清空之後,資料庫重啟了,自增列的值將從初始值開始

我們來演示一下:

mysql> delete from test11;
Query OK, 2 rows affected (0.00 sec)

mysql> insert into test11(b) VALUES (10);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test11;
+---+----+
| a | b  |
+---+----+
| 3 | 10 |
+---+----+
1 row in set (0.00 sec)

上面刪除了 test11 數據,然後插入了一條,a 的值為 3,執行下面操作:

刪除 test11 數據,重啟 mysql,插入數據,然後看 a 的值是不是被初始化了?如下:

mysql> delete from test11;
Query OK, 1 row affected (0.00 sec)

mysql> select * from test11;
Empty set (0.00 sec)

mysql> exit
Bye

C:\Windows\system32>net stop mysql
mysql 服務正在停止..
mysql 服務已成功停止。


C:\Windows\system32>net start mysql
mysql 服務正在啟動 .
mysql 服務已經啟動成功。


C:\Windows\system32>mysql -uroot -p
Enter password: *******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use javacode2018;
Database changed
mysql> select * from test11;
Empty set (0.01 sec)

mysql> insert into test11 (b) value (100);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test11;
+---++
| a | b   |
+---++
| 1 | 100 |
+---++
1 row in set (0.00 sec)

刪除表
drop table [if exists] 表名;

修改表名
alter table 表名 rename [to] 新表名;

表設置備註
alter table 表名 comment '備註信息';

複製表只複製表結構
create table 表名 like 被複製的表名;

如:

mysql> create table test12 like test11;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from test12;
Empty set (0.00 sec)

mysql> show create table test12;
+---+--+
| Table  | Create Table
+---+--+
| test12 | CREATE TABLE `test12` (
  `a` int(11) NOT NULL AUTO_INCREMENT COMMENT '欄位a',
  `b` int(11) NOT NULL COMMENT '欄位b',
  PRIMARY KEY (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8     |
+---+--+
1 row in set (0.00 sec)

複製表結構+數據
create table 表名 [as] select 欄位,... from 被複製的表 [where 條件];

如:

mysql> create table test13 as select * from test11;
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from test13;
+---++
| a | b   |
+---++
| 1 | 100 |
+---++
1 row in set (0.00 sec)

表結構和數據都過來了。

表中列的管理添加列
alter table 表名 add column 列名 類型 [列約束];

示例:

mysql> drop table IF EXISTS test14;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>
mysql> create table test14(
    ->   a int not null AUTO_INCREMENT PRIMARY KEY comment '欄位a'
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> alter table test14 add column b int not null default 0 comment '欄位b';
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table test14 add column c int not null default 0 comment '欄位c';
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> insert into test14(b) values (10);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test14;                                                 c
+---+----+---+
| a | b  | c |
+---+----+---+
| 1 | 10 | 0 |
+---+----+---+
1 row in set (0.00 sec)

修改列
alter table 表名 modify column 列名 新類型 [約束];
或者
alter table 表名 change column 列名 新列名 新類型 [約束];

2 種方式區別:modify 不能修改列名,change 可以修改列名

我們看一下 test14 的表結構:

mysql> show create table test14;
+---+---+
| Table  | Create Table |
+---+---+
| test14 | CREATE TABLE `test14` (
  `a` int(11) NOT NULL AUTO_INCREMENT COMMENT '欄位a',
  `b` int(11) NOT NULL DEFAULT '0' COMMENT '欄位b',
  `c` int(11) NOT NULL DEFAULT '0' COMMENT '欄位c',
  PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8       |
+---+---+
1 row in set (0.00 sec)

我們將欄位 c 名字及類型修改一下,如下:

mysql> alter table test14 change column c d varchar(10) not null default '' comment '欄位d';
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table test14;                                                          ;;
+---+---+
| Table  | Create Table |
+---+---+
| test14 | CREATE TABLE `test14` (
  `a` int(11) NOT NULL AUTO_INCREMENT COMMENT '欄位a',
  `b` int(11) NOT NULL DEFAULT '0' COMMENT '欄位b',
  `d` varchar(10) NOT NULL DEFAULT '' COMMENT '欄位d',
  PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8       |
+---+---+
1 row in set (0.00 sec)

刪除列
alter table 表名 drop column 列名;

示例:

mysql> alter table test14 drop column d;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table test14;
+---+---+
| Table  | Create Table |
+---+---+
| test14 | CREATE TABLE `test14` (
  `a` int(11) NOT NULL AUTO_INCREMENT COMMENT '欄位a',
  `b` int(11) NOT NULL DEFAULT '0' COMMENT '欄位b',
  PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8     |
+---+---+
1 row in set (0.00 sec)

mysql 系列大概有 20 多篇,喜歡的請關注一下!

最新資料

相關焦點

  • MySQL基本操作命令(DDL、DML、DQL、DCL)
    一:資料庫基本操作命令1.1:mysql查看資料庫結構1.2:查看資料庫信息mysql> show databases;++| Database |++| information_schema || mysql || performance_schema
  • ③:MySQL管理員常用命令總結
    這是 mysql 系列第 3 篇文章。環境:mysql5.7.25,cmd 命令中進行演示。在玩 mysql 的過程中,經常遇到有很多朋友在雲上面玩 mysql 的時候,說我創建了一個用戶為什麼不能登錄?
  • Mysql 常用命令操作
    無論開發還是測試人員,在工作中經常涉及資料庫交互,需要去操作資料庫,這裡簡單的講下
  • MySQL 常用命令手冊
    45mysql> insert into MyClass values(1,』Tom』,96.45),(2,』Joan』,82.99), (2,』Wang』, 96.59);  5、查詢表中的數據   11)、查詢所有行   2 3命令:select <欄位,欄位,...
  • MySQL EXPLAIN 命令詳解
    查詢中總的讀操作數量是基於合併之前行的每一行的rows 值的連續積累而得出的。這是一種嵌套行算法。以連接兩個表的QEP 為例。通過id=1 這個條件找到的第一行的rows 值為1,這等於對第一個表做了一次讀操作。第二行是通過id=2 找到的,rows 的值為5。這等於有5 次讀操作符合當前1 的積累量。參考兩個表,讀操作的總數目是6。
  • Java從零開始學 - 第60天:DDL常見操作匯總
    環境:mysql5.7.25,cmd命令中進行演示。DDL:Data Define Language數據定義語言,主要用來對資料庫、表進行一些管理操作。如:建庫、刪庫、建表、修改表、刪除表、對列的增刪改等等。文中涉及到的語法用[]包含的內容屬於可選項,下面做詳細說明。
  • MySQL EXPLAIN 命令詳解學習
    查詢中總的讀操作數量是基於合併之前行的每一行的rows 值的連續積累而得出的。這是一種嵌套行算法。以連接兩個表的QEP 為例。通過id=1 這個條件找到的第一行的rows 值為1,這等於對第一個表做了一次讀操作。第二行是通過id=2 找到的,rows 的值為5。這等於有5 次讀操作符合當前1 的積累量。參考兩個表,讀操作的總數目是6。
  • MYSQL資料庫操作案例
    , 但是你再進行刪除表操作時要非常小心,因為執行刪除命令後所有數據都會消失。MySQL 中的數據,我們可以使用 SQL UPDATE 命令來操作。MySQL UNION 操作符用於連接兩個以上的 SELECT 語句的結果組合到一個結果集合中。
  • MySQL的Online DDL語句
    訴求背景我們平時所說的在線的 DDL操作一般情況是指以下操作:在表中增加欄位在表中增加索引在表中修改欄位類型刪除表中的欄位刪除表中的索引如果是一張小表,只有幾百行或者幾千行的數據表,我們要增加欄位或增加索引,基本不用考慮什麼,一個字:幹,就完了。但如果是一個大表呢?
  • MySQL資料庫常用命令詳解
    除了用第三方軟體管理MySQL資料庫外,MySQL本身也提供了管理資料庫的操作命令,可以在CentOS終端直接使用MySQL命令,用於MySQL資料庫的創建、表的管理、SQL查詢等管理操作。(1)登錄MySQL資料庫用SSH客戶端連接CentOS伺服器,打開終端命令輸入窗口,在終端輸入窗口輸入命令:mysql -uroot –p 該命令用root帳號以密碼方式登錄MySQL,回車後提示輸入密碼
  • 你可能不知道 Mysql的常用命令收集
    首先在打開DOS窗口,然後進入目錄 mysqlbin,再鍵入命令mysql -uroot -p,回車後提示你輸密碼,如果剛安裝好MYSQL,超級用戶root是沒有密碼的,故直接回車即可進入到MYSQL中了,MYSQL的提示符是:mysql> 2、例2:連接到遠程主機上的MYSQL。
  • python對mysql資料庫的操作(一)
    在自動化測試中,某些人認為是沒有必要操作資料庫的,理由是大多數的自動化測試都是UI的,非接口的自動化測試,其實,在一個項目的自動化測試中,這種定義很模糊,或者說很不明確,比如在自動化測試中,怎麼來驗證用戶登錄成功,用戶註冊成功?先來說登錄,用戶登錄成功後,驗證點首先是用戶的暱稱,再有麼?
  • MySQL/MsSQL/Oracle提權總結
    用戶可以通過自定義函數實現在mysql中無法方便實現的功能,其添加的新函數都可以在sql語句中調用,就像調用本機函數一樣。3.掌握mysql資料庫的帳戶,從擁有對mysql的insert和delete權限,以創建和拋棄函數。4.擁有可以將udf.dll寫入相應目錄的權限。③操作步驟1.最常見的是直接使用udf.php此類的工具來執行udf提權,具體如下:連接到mysql以後,先導出udf.dll到 c:\windows\system32 目錄下。
  • MySQL之Online DDL
    例如資料庫中的資料庫名、表名、 列名、用戶名、版本名以及從SQL語句得到的結果中的大部分字符串是元數據。舉例來看一下:存儲引擎為InnoDB的表,磁碟上會生成兩個物理文件,分別是.frm、.ibd,.frm就是表結構定義文件,我們通過MySQL官方提供的mysql-utilities工具包中的mysqlfrm來看一下.frm文件中存放的內容(因為.frm文件是二進位格式的,我們不能使用常規的Linux作業系統查看文本的方式來看,會出現亂碼):[172.xx.xx.xxx
  • mysql常用命令,保存一下,以後查詢方便了
    格式:grant select on 資料庫.* to 用戶名@登錄主機 identified by "密碼"grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";7、使用test1帳號從其他主機上登錄命令
  • 一看就會,MySQL資料庫的基本操作(四)
    本節繼續學習資料庫的有關操作。1、啟動或者關閉MySQL服務兩種方法:第一種方法:利用系統服務打開。具體是打開命令窗口(win+r),輸入services.msc,然後在右側服務列表中找到MSQL服務。可能有的後邊有版本號。比如MSQL56,雙擊打開或者關閉服務。
  • mysql 慢查詢命令
    如何查找MySQL中查詢慢的SQL語句更多如何在mysql查找效率慢的SQL語句呢?您也可以使用mysqladmin processlist語句得到此信息。各列的含義和用途:ID列一個標識,你要kill一個語句的時候很有用,用命令殺掉此查詢 /*/mysqladmin kill 進程號。
  • 詳解PHP操作MySQL資料庫
    安裝好這款軟體,網站的目錄在/Applications/MAMP/htdocs文件夾裡,只需將文件放入該文件夾中,就可以通過http://localhost:8888來訪問了,或者通過點擊如下紅色下劃線按鈕來快速訪問站點。
  • Python 操作MySQL資料庫
    Python 使用操作MySQL第一步:
  • MySQL 資料庫基本命令匯總整理,需要的趕緊學起來
    :(mysql_create_db(),mysql_query())$conn = mysql_connect(「localhost」,」username」,」password」) ordie ( 「could not connect to localhost」);1.