最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mysql DDL常見操作匯總

 更新時間:2020年09月21日 11:06:28   作者:易兮科技  
這篇文章主要介紹了Mysql DDL常見操作匯總,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

庫的管理

創(chuàng)建庫

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庫信息。

表管理

創(chuàng)建表

create table 表名(
  字段名1 類型[(寬度)] [約束條件] [comment '字段說明'],
  字段名2 類型[(寬度)] [約束條件] [comment '字段說明'],
  字段名3 類型[(寬度)] [約束條件] [comment '字段說明']
)[表的一些設(shè)置];

注意:

  • 在同一張表中,字段名不能相同
  • 寬度和約束條件為可選參數(shù),字段名和類型是必須的
  • 最后一個字段后不能加逗號
  • 類型是用來限制 字段 必須以何種數(shù)據(jù)類型來存儲記錄
  • 類型其實也是對字段的約束(約束字段下的記錄必須為XX類型)
  • 類型后寫的 約束條件 是在類型之外的 額外添加的約束

約束說明

not null:標(biāo)識該字段不能為空

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:**為該字段設(shè)置默認值,默認值為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)

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

**primary key:**標(biāo)識該字段為該表的主鍵,可以唯一的標(biāo)識記錄,插入重復(fù)的會報錯

兩種寫法,如下:

方式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'

插入重復(fù)的值,會報違法主鍵約束

方式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:為表中的字段設(shè)置外鍵

語法:foreign key(當(dāng)前表的列名) 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。

注意幾點:

  • 兩張表中需要建立外鍵關(guān)系的字段類型需要一致
  • 要設(shè)置外鍵的字段不能為主鍵
  • 被引用的字段需要為主鍵
  • 被插入的值在外鍵表必須存在,如上面向test6中插入ts5_a為2的時候報錯了,原因:2的值在test5表中不存在

unique key(uq):標(biāo)識該字段的值是唯一的

支持一個到多個字段,插入重復(fù)的值會報違反唯一約束,會插入失敗。

定義有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:標(biāo)識該字段的值自動增長(整數(shù)類型,而且為主鍵)

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

關(guān)于自動增長字段的初始值、步長可以在mysql中進行設(shè)置,比如設(shè)置初始值為1萬,每次增長10

注意:
 自增長列當(dāng)前值存儲在內(nèi)存中,數(shù)據(jù)庫每次重啟之后,會查詢當(dāng)前表中自增列的最大值作為當(dāng)前值,如果表數(shù)據(jù)被清空之后,數(shù)據(jù)庫重啟了,自增列的值將從初始值開始

我們來演示一下:

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數(shù)據(jù),然后插入了一條,a的值為3,執(zhí)行下面操作:

刪除test11數(shù)據(jù),重啟mysql,插入數(shù)據(jù),然后看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 服務(wù)正在停止..
mysql 服務(wù)已成功停止。


C:\Windows\system32>net start mysql
mysql 服務(wù)正在啟動 .
mysql 服務(wù)已經(jīng)啟動成功。


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] 新表名;

表設(shè)置備注

alter table 表名 comment '備注信息';

復(fù)制表

create table 表名 like 被復(fù)制的表名;

如:

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)

復(fù)制表結(jié)構(gòu)+數(shù)據(jù)

create table 表名 [as] select 字段,... from 被復(fù)制的表 [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)

表結(jié)構(gòu)和數(shù)據(jù)都過來了。

表中列的管理

添加列

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種方式區(qū)別:modify不能修改列名,change可以修改列名

我們看一下test14的表結(jié)構(gòu):

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)

到此這篇關(guān)于Mysql DDL常見操作匯總的文章就介紹到這了,更多相關(guān)Mysql DDL操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MySQL InnoDB MRR優(yōu)化指南

    MySQL InnoDB MRR優(yōu)化指南

    這篇文章主要給大家介紹了關(guān)于MySQL InnoDB MRR優(yōu)化的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Linux系統(tǒng)下MySQL的初始化和配置指南

    Linux系統(tǒng)下MySQL的初始化和配置指南

    當(dāng)MySQL的系統(tǒng)庫(mysql系統(tǒng)庫)發(fā)生故障或需要新加一個mysql實例時,需要初始化mysql數(shù)據(jù)庫,這篇文章主要給大家介紹了關(guān)于Linux系統(tǒng)下MySQL的初始化和配置指南的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • MySQL如何支撐起億級流量

    MySQL如何支撐起億級流量

    當(dāng)每天新增數(shù)據(jù)上億級的時候,單表數(shù)據(jù)量在百萬級別,數(shù)據(jù)庫服務(wù)器的高峰期寫入壓力、查詢壓力在都很高的時候,該如何讓MySQL順利支撐起來呢?本片文章將教給你詳細的方案
    2021-09-09
  • windows下安裝mysql-8.0.18-winx64的教程(圖文詳解)

    windows下安裝mysql-8.0.18-winx64的教程(圖文詳解)

    這篇文章主要介紹了windows下安裝mysql-8.0.18-winx64,需要的朋友可以參考下
    2019-12-12
  • kali虛擬機mysql修改綁定ip的問題

    kali虛擬機mysql修改綁定ip的問題

    這篇文章主要介紹了kali虛擬機mysql修改綁定ip,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • MySQL?表分區(qū)步驟示例詳解

    MySQL?表分區(qū)步驟示例詳解

    MySQL表分區(qū)是一種數(shù)據(jù)庫管理技術(shù),用于將大型表拆分成更小、更可管理的分區(qū)(子表,這篇文章主要介紹了MySQL?表分區(qū)簡介,需要的朋友可以參考下
    2023-09-09
  • MySQL數(shù)據(jù)庫表的合并及分區(qū)方式

    MySQL數(shù)據(jù)庫表的合并及分區(qū)方式

    這篇文章主要介紹了MySQL數(shù)據(jù)庫表的合并及分區(qū)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 詳細聊一聊mysql的樹形結(jié)構(gòu)存儲以及查詢

    詳細聊一聊mysql的樹形結(jié)構(gòu)存儲以及查詢

    由于mysql是關(guān)系型數(shù)據(jù)庫,因此對于類似組織架構(gòu),子任務(wù)等相關(guān)的樹形結(jié)構(gòu)的處理不是很友好,下面這篇文章主要給大家介紹了關(guān)于mysql樹形結(jié)構(gòu)存儲以及查詢的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • MySQL一對多查詢的實現(xiàn)示例

    MySQL一對多查詢的實現(xiàn)示例

    一對多連接查詢就是其中一種常見的查詢方式,它可以將一張表中的一行記錄與多張表中的多行記錄關(guān)聯(lián)起來,并將其結(jié)果輸出,本文就來介紹一下如何使用,感興趣的可以了解一下
    2023-10-10
  • MySQL數(shù)值類型溢出的處理方法

    MySQL數(shù)值類型溢出的處理方法

    這篇文章主要給大家介紹了關(guān)于MySQL數(shù)值類型溢出的處理方法,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08

最新評論

衡阳市| 新化县| 仪陇县| 儋州市| 河池市| 永福县| 塘沽区| 长顺县| 丁青县| 南涧| 长沙市| 伊宁县| 江西省| 开阳县| 江西省| 绥芬河市| 合川市| 鸡泽县| 科技| 绥宁县| 蕲春县| 巫溪县| 克什克腾旗| 旬邑县| 高邑县| 泸定县| 吕梁市| 云霄县| 平南县| 五莲县| 平南县| 延寿县| 小金县| 名山县| 泰兴市| 积石山| 额尔古纳市| 师宗县| 石林| 琼中| 三河市|