电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Mysql DDL常見操作匯總

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

庫的管理

創建庫

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 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:**為該字段設置默認值,默認值為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> exitByeC:Windowssystem32>net stop mysqlmysql 服務正在停止..mysql 服務已成功停止。C:Windowssystem32>net start mysqlmysql 服務正在啟動 .mysql 服務已經啟動成功。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] 新表名;

表設置備注

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: 0mysql> 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: 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種方式區別: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: 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)

到此這篇關于Mysql DDL常見操作匯總的文章就介紹到這了,更多相關Mysql DDL操作內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: MySQL 數據庫
相關文章:
主站蜘蛛池模板: 东莞螺杆空压机_永磁变频空压机_节能空压机_空压机工厂批发_深圳螺杆空压机_广州螺杆空压机_东莞空压机_空压机批发_东莞空压机工厂批发_东莞市文颖设备科技有限公司 | 不锈钢管件(不锈钢弯头,不锈钢三通,不锈钢大小头),不锈钢法兰「厂家」-浙江志通管阀 | 北京晚会活动策划|北京节目录制后期剪辑|北京演播厅出租租赁-北京龙视星光文化传媒有限公司 | SOUNDWELL 编码器|电位器|旋转编码器|可调电位器|编码开关厂家-广东升威电子制品有限公司 | 泉州陶瓷pc砖_园林景观砖厂家_石英砖地铺石价格 _福建暴风石英砖 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | 大功率金属激光焊接机价格_不锈钢汽车配件|光纤自动激光焊接机设备-东莞市正信激光科技有限公司 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 无轨电动平车_轨道平车_蓄电池电动平车★尽在新乡百特智能转运设备有限公司 | 美侍宠物-专注宠物狗及宠物猫训练|喂养|医疗|繁育|品种|价格 | 防勒索软件_数据防泄密_Trellix(原McAfee)核心代理商_Trellix(原Fireeye)售后-广州文智信息科技有限公司 | 菏泽商标注册_菏泽版权登记_商标申请代理_菏泽商标注册去哪里 | MVR蒸发器厂家-多效蒸发器-工业废水蒸发器厂家-康景辉集团官网 | 液压升降货梯_导轨式升降货梯厂家_升降货梯厂家-河南东圣升降设备有限公司 | 河南空气能热水器-洛阳空气能采暖-洛阳太阳能热水工程-洛阳润达高科空气能商行 | 青岛代理记账_青岛李沧代理记账公司_青岛崂山代理记账一个月多少钱_青岛德辉财税事务所官网 | 自动钻孔机-全自动数控钻孔机生产厂家-多米(广东)智能装备有限公司 | 健身器材-健身器材厂家专卖-上海七诚健身器材有限公司 | 广西资质代办_建筑资质代办_南宁资质代办理_新办、增项、升级-正明集团 | 塑木弯曲试验机_铜带拉伸强度试验机_拉压力测试台-倾技百科 | 打孔器,打孔钳厂家【温州新星德牌五金工具】 | 衡阳耐适防护科技有限公司——威仕盾焊接防护用品官网/焊工手套/焊接防护服/皮革防护手套 | 机房监控|动环监控|动力环境监控系统方案产品定制厂家 - 迈世OMARA | 冷油器,取样冷却器,热力除氧器-连云港振辉机械设备有限公司 | 工控机-工业平板电脑-研华工控机-研越无风扇嵌入式box工控机 | 轴流风机-鼓风机-离心风机-散热风扇-罩极电机,生产厂家-首肯电子 | YJLV22铝芯铠装电缆-MYPTJ矿用高压橡套电缆-天津市电缆总厂 | 锂电叉车,电动叉车_厂家-山东博峻智能科技有限公司 | 专业生产动态配料系统_饲料配料系统_化肥配料系统等配料系统-郑州鑫晟重工机械有限公司 | 硫化罐-胶管硫化罐-山东鑫泰鑫智能装备有限公司 | 变位机,焊接变位机,焊接变位器,小型变位机,小型焊接变位机-济南上弘机电设备有限公司 | 恒温振荡混匀器-微孔板振荡器厂家-多管涡旋混匀器厂家-合肥艾本森(www.17world.net) | 沈阳真空机_沈阳真空包装机_沈阳大米真空包装机-沈阳海鹞真空包装机械有限公司 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 温湿度记录纸_圆盘_横河记录纸|霍尼韦尔记录仪-广州汤米斯机电设备有限公司 | 团建-拓展-拓展培训-拓展训练-户外拓展训练基地[无锡劲途] | 纸塑分离机-纸塑分离清洗机设备-压力筛-碎浆机厂家金双联环保 | 专注提供国外机电设备及配件-工业控制领域一站式服务商-深圳市华联欧国际贸易有限公司 | 防水套管|柔性防水套管|伸缩器|伸缩接头|传力接头-河南伟创管道 防水套管_柔性防水套管_刚性防水套管-巩义市润达管道设备制造有限公司 | 台湾HIWIN上银直线模组|导轨滑块|TBI滚珠丝杆丝杠-深圳汉工 | 校车_校车价格_19座幼儿园校车_幼儿园校车_大鼻子校车 | 温控器生产厂家-提供温度开关/热保护器定制与批发-惠州市华恺威电子科技有限公司 |