日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

Mysql DDL常見操作匯總

瀏覽:44日期:2023-10-11 11:10:24

庫的管理

創(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ù),字段名和類型是必須的 最后一個(gè)字段后不能加逗號(hào) 類型是用來限制 字段 必須以何種數(shù)據(jù)類型來存儲(chǔ)記錄 類型其實(shí)也是對(duì)字段的約束(約束字段下的記錄必須為XX類型) 類型后寫的 約束條件 是在類型之外的 額外添加的約束

約束說明

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

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 nullmysql> 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è)置默認(rèn)值,默認(rèn)值為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í)未設(shè)置b的值,自動(dòng)取默認(rèn)值0

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

兩種寫法,如下:

方式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ù)的值,會(huì)報(bào)違法主鍵約束

方式2支持多字段作為主鍵,多個(gè)之間用逗號(hào)隔開,語法: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。

注意幾點(diǎn):

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

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

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

定義有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支持多字段,多個(gè)之間用逗號(hào)隔開,語法: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í)該字段的值自動(dòng)增長(整數(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為自動(dòng)增長,默認(rèn)值從1開始,每次+1

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

注意: 自增長列當(dāng)前值存儲(chǔ)在內(nèi)存中,數(shù)據(jù)庫每次重啟之后,會(huì)查詢當(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> exitByeC:Windowssystem32>net stop mysqlmysql 服務(wù)正在停止..mysql 服務(wù)已成功停止。C:Windowssystem32>net start mysqlmysql 服務(wù)正在啟動(dòng) .mysql 服務(wù)已經(jīng)啟動(dòng)成功。C:Windowssystem32>mysql -uroot -pEnter password: *******Welcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 2Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.Type ’help;’ or ’h’ for help. Type ’c’ to clear the current input statement.mysql> use javacode2018;Database changedmysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: MySQL 數(shù)據(jù)庫
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
老牛影视精品| 日韩精品一区第一页| 天海翼亚洲一区二区三区| 日韩一级网站| 亚洲69av| 精品日韩一区| 成人久久一区| 中文字幕亚洲精品乱码| 日本国产欧美| 精品日韩一区| 伊人久久亚洲美女图片| 日韩欧美中文在线观看| 欧美日本精品| 精品久久精品| 午夜视频精品| 国产亚洲人成a在线v网站| 国内精品亚洲| 免费视频久久| 久久99精品久久久野外观看| 日韩亚洲一区在线| 亚洲三级网站| av免费不卡国产观看| 老牛影视一区二区三区| 久久精品一区二区三区中文字幕| 亚洲特级毛片| 精品国产亚洲一区二区在线观看| 国产色综合网| 麻豆精品少妇| 中文亚洲欧美| 成人一区而且| 91嫩草精品| 亚洲激精日韩激精欧美精品| 久久久久伊人| 日本成人在线一区| 免费日韩一区二区| 精品成人免费一区二区在线播放| 青草久久视频| 一区二区三区四区精品视频| 日韩免费av| 国产精品久久久免费| 亚洲精品女人| 爽好久久久欧美精品| 亚洲激情偷拍| 黄色aa久久| 精品高清久久| 欧美激情一区| 国产欧美久久一区二区三区| 久久亚洲欧洲| 国产精品啊啊啊| 久久国产尿小便嘘嘘| 亚洲一区二区免费看| 精品欧美激情在线观看| 久久久精品网| 99精品视频在线| 日韩精品影视| 成人羞羞视频在线看网址| 久久精品国产99国产| 国产精品一级| 婷婷成人av| 日韩av黄色在线| 久久国产精品色av免费看| 国产视频一区二区在线播放| 国产精品分类| 国产一区二区精品久| 日韩大片在线| 亚洲精品在线观看91| 香蕉久久夜色精品国产| 亚洲精品三级| 鲁大师精品99久久久| 中文字幕在线视频久| 免费精品国产| 日韩欧美中文字幕电影| 欧美国产精品| 成人国产精品一区二区免费麻豆| 樱桃视频成人在线观看| re久久精品视频| 日韩一二三区在线观看| 久久国际精品| 99精品美女| 日本不卡不码高清免费观看 | 久久国产日韩| 视频在线观看91| 美女久久99| 亚洲精品在线观看91| 蜜臀av国产精品久久久久| 国产精品久av福利在线观看| 蜜桃精品在线| 日韩av电影一区| 亚洲一级网站| 美女毛片一区二区三区四区最新中文字幕亚洲 | 精品视频一区二区三区四区五区 | 国产精品99久久免费观看| 国产高清不卡| 18国产精品| 亚洲电影在线一区二区三区| 欧美一区成人| 国产亚洲午夜| 日韩在线中文| 国产精品宾馆| 青青草国产成人99久久| 激情91久久| 日本免费一区二区三区四区| 亚洲一区激情| 日韩高清欧美| 精品国产亚洲日本| 国产午夜精品一区在线观看| 日韩视频一区| 国产91久久精品一区二区| 国产精品a级| 国产精品入口久久| 少妇精品在线| 亚洲精品系列| 蜜桃视频一区二区三区| 国内精品福利| 欧美性感美女一区二区| 婷婷激情一区| 日韩欧美二区| av资源新版天堂在线| 国产精品videossex久久发布 | 国产精品av一区二区| 精品久久精品| 韩国一区二区三区视频| 激情久久一区二区| 国产精品成人一区二区不卡| 麻豆精品在线视频| 国产成人精品一区二区三区视频| 欧美交a欧美精品喷水| 日本在线视频一区二区| 日韩不卡一区二区三区| 日本午夜精品久久久久| 国产精品一线| 久久精品国产99| 国产 日韩 欧美一区| 国产91一区| 亚久久调教视频| 国产精品欧美三级在线观看| 精品国产一区二区三区2021| 日本不卡免费高清视频在线| 国精品一区二区三区| 亚洲精品无播放器在线播放| 国产精品免费99久久久| 成人一区而且| 久久精品av| 日韩免费精品| 神马午夜在线视频| 国产精品日本| 国产日韩欧美一区二区三区| 成人一区不卡| 一区二区不卡| 久久精品国产一区二区| 欧美日韩一区二区三区视频播放| 日韩精品一区第一页| 免费在线亚洲| 亚洲深夜福利| 国产成人精品三级高清久久91| 亚洲一级二级| 久久精品国产福利| 亚洲国产综合在线看不卡| 欧美伊人久久| 亚州av乱码久久精品蜜桃| 欧美亚洲自偷自偷| 激情久久久久久| 久久一区精品| 日本成人精品| 国精品一区二区三区| 狂野欧美性猛交xxxx| 亚洲精品无播放器在线播放| 999久久久免费精品国产| 国产精品调教视频| 水野朝阳av一区二区三区| 亚洲一级少妇| 国内精品伊人| 日本在线成人| 亚洲三级精品| 天堂成人国产精品一区| 欧洲一级精品| 九九久久国产| 国产日韩一区二区三免费高清| 国产精品社区| 图片区亚洲欧美小说区| 中文在线а√天堂 | 中文不卡在线| 国产麻豆综合| 国产一级一区二区| 女同性一区二区三区人了人一| 国语对白精品一区二区| 国产精品一区二区三区av| 日韩1区2区3区| 欧美日韩一区二区国产| 亚洲精品黄色| 91嫩草精品| 国产精品久久久久9999高清| 国产精品sm| 色网在线免费观看| 精品免费av在线| 国产夫妻在线| 亚洲精品va| 免费在线观看成人| 日韩av二区在线播放| 久久精品国产免费|