日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Mysql数据库基础系列(二):表结构、键值

發布時間:2025/3/21 数据库 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mysql数据库基础系列(二):表结构、键值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

表結構

約束條件

查看約束條件

mysql> desc 庫名.表名;

字段名----| 類型---------------| 空------ |鍵值-- |默認值---- |額外設置 |

設置約束條件

  • 約束條件的作用:限制字段賦值
    null---------------允許為空(默認設置)
    not null-----------不允許為null(空)
    key-----------------鍵值類型
    default------------設置默認值,缺省為NULL,默認值要與該類型匹配
    extra----------------額外設置
mysql> create database db2; Query OK, 1 row affected (0.00 sec)mysql> use db2 Database changed mysql> create table db2.t1 (name char(15) not null , sex enum("boy" , "girl") default "boy" ,likes set("eat" , "game" , "money" , "it") not null default "eat,money" ); Query OK, 0 rows affected (0.03 sec)mysql> desc db2.t1; +-------+--------------------------------+------+-----+-----------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------------------------+------+-----+-----------+-------+ | name | char(15) | NO | | NULL | | | sex | enum('boy','girl') | YES | | boy | | | likes | set('eat','game','money','it') | NO | | eat,money | | +-------+--------------------------------+------+-----+-----------+-------+ 3 rows in set (0.00 sec)測試使用默認值賦值:mysql> insert into db2.t1(name) values ("nb"); Query OK, 1 row affected (0.00 sec)mysql> select * from db2.t1; +------+------+-----------+ | name | sex | likes | +------+------+-----------+ | nb | boy | eat,money | +------+------+-----------+ 1 row in set (0.00 sec)不使用默認值賦值:mysql> insert into db2.t1 values ("dmy" , "girl" , "game,it" ); Query OK, 1 row affected (0.00 sec)mysql> select * from db2.t1; +------+------+-----------+ | name | sex | likes | +------+------+-----------+ | nb | boy | eat,money | | dmy | girl | game,it | +------+------+-----------+ 2 rows in set (0.00 sec)測試null空值的使用:mysql> insert into db2.t1 values (null , null , null ); ERROR 1048 (23000): Column 'name' cannot be null #name字段不能為空mysql> insert into db2.t1 values ("null" , null , null ); Query OK, 1 row affected (0.00 sec)mysql> insert into db2.t1 values (null , null , null ); ERROR 1048 (23000): Column 'likes' cannot be null #字段likes不能為空值mysql> select * from db2.t1; +------+------+-----------+ | name | sex | likes | +------+------+-----------+ | nb | boy | eat,money | | dmy | girl | game,it | | null | NULL | it | +------+------+-----------+ 3 rows in set (0.00 sec)mysql> drop table db2.t1; Query OK, 0 rows affected (0.01 sec)mysql> create table db2.t1(-> name char(15) not null default "" ,-> sex enum("boy" , "girl") default "boy",-> likes set("eat" , "game" , "money", "it") not null default "eat,money" ); Query OK, 0 rows affected (0.01 sec)mysql> desc db2.t1; +-------+--------------------------------+------+-----+-----------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------------------------+------+-----+-----------+-------+ | name | char(15) | NO | | | | | sex | enum('boy','girl') | YES | | boy | | | likes | set('eat','game','money','it') | NO | | eat,money | | +-------+--------------------------------+------+-----+-----------+-------+mysql> insert into db2.t1(sex,likes) values("girl" , "it"); Query OK, 1 row affected (0.00 sec)mysql> select * from t1; +------+------+-------+ | name | sex | likes | +------+------+-------+ | | girl | it | +------+------+-------+ 1 row in set (0.00 sec)""雙引號內沒有內容不代表為空,而是表示默認為0個字符

修改表結構

命令格式:
alter table 庫名.表名 執行動作;

  • 執行動作:

①添加新字段,新字段默認添加在字段末尾
mysql > alter table 庫名.表名 add 字段名 類型(寬度) 約束條件 [after 字段名 | first];

②修改字段類型,修改的字段類型不能與已存儲的數據沖突
mysql > alter table 庫名.表名 modify 字段名 類型(寬度) 約束條件 [after 字段名 | first];

③修改字段名,也可以用來修改字段類型
mysql > alter table 庫名.表名 change 源字段名 新字段名 類型(寬度) 約束條件 ;
當跟新類型和約束條件時,可以修改字段類型

④刪除字段,表中有多條記錄時,所有列的此字段的值都會被刪除
mysql > alter table 庫名.表名 drop 字段名;

⑤修改表名,表對應的文件名,也會被改變,表記錄不受影響
mysql > alter table 表名 rename 新表名;

mysql> use db2; Database changed mysql> alter table t1 add class char(7) default "1234567" first; Query OK, 0 rows affected (0.09 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> alter table t1 add email varchar(50); Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> alter table t1 add age tinyint unsigned default 23 after name; Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> desc db2.t1; +-------+--------------------------------+------+-----+-----------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------------------------+------+-----+-----------+-------+ | class | char(7) | YES | | 1234567 | | | name | char(15) | NO | | | | | age | tinyint(3) unsigned | YES | | 23 | | | sex | enum('boy','girl') | YES | | boy | | | likes | set('eat','game','money','it') | NO | | eat,money | | | email | varchar(50) | YES | | NULL | | +-------+--------------------------------+------+-----+-----------+-------+ 6 rows in set (0.00 sec)mysql> insert into db2.t1(sex ,likes) values("girl" ,"it");mysql> select * from db2.t1; +---------+------+------+------+-------+-------+ | class | name | age | sex | likes | email | +---------+------+------+------+-------+-------+ | 1234567 | | 23 | girl | it | NULL | +---------+------+------+------+-------+-------+ 1 row in set (0.00 sec)修改字段類型:mysql> use db2; Database changedmysql> show tables; +---------------+ | Tables_in_db2 | +---------------+ | t1 | +---------------+ 1 row in set (0.00 sec)mysql> desc db2.t1; +-------+--------------------------------+------+-----+-----------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------------------------+------+-----+-----------+-------+ | class | char(7) | YES | | 1234567 | | | name | char(15) | NO | | | | | age | tinyint(3) unsigned | YES | | 23 | | | sex | enum('boy','girl') | YES | | boy | | | likes | set('eat','game','money','it') | NO | | eat,money | | | email | varchar(50) | YES | | NULL | | +-------+--------------------------------+------+-----+-----------+-------+ 6 rows in set (0.01 sec)mysql> select * from t1; +---------+------+------+------+-------+-------+ | class | name | age | sex | likes | email | +---------+------+------+------+-------+-------+ | 1234567 | | 23 | girl | it | NULL | +---------+------+------+------+-------+-------+ 1 row in set (0.00 sec)mysql> alter table t1 modify email char(30) default "stu@163.com"; Query OK, 1 row affected (0.02 sec) Records: 1 Duplicates: 0 Warnings: 0mysql> alter table t1 modify age tinyint unsigned default 23 after class; Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> desc db2.t1; +-------+--------------------------------+------+-----+-------------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------------------------+------+-----+-------------+-------+ | class | char(7) | YES | | 1234567 | | | age | tinyint(3) unsigned | YES | | 23 | | | name | char(15) | NO | | | | | sex | enum('boy','girl') | YES | | boy | | | likes | set('eat','game','money','it') | NO | | eat,money | | | email | char(30) | YES | | stu@163.com | | +-------+--------------------------------+------+-----+-------------+-------+ 6 rows in set (0.00 sec)修改字段名: #把name字段變為了user字段mysql> alter table t1 change name user char(15) not null default "" ; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> desc t1; +-------+--------------------------------+------+-----+-------------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------------------------+------+-----+-------------+-------+ | class | char(7) | YES | | 1234567 | | | age | tinyint(3) unsigned | YES | | 23 | | | user | char(15) | NO | | | | | sex | enum('boy','girl') | YES | | boy | | | likes | set('eat','game','money','it') | NO | | eat,money | | | email | char(30) | YES | | stu@163.com | | +-------+--------------------------------+------+-----+-------------+-------+ 6 rows in set (0.00 sec)刪除字段: #class字段和email字段被刪除mysql> alter table t1 drop class , drop email; Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> desc t1; +-------+--------------------------------+------+-----+-----------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------------------------+------+-----+-----------+-------+ | age | tinyint(3) unsigned | YES | | 23 | | | user | char(15) | NO | | | | | sex | enum('boy','girl') | YES | | boy | | | likes | set('eat','game','money','it') | NO | | eat,money | | +-------+--------------------------------+------+-----+-----------+-------+ 4 rows in set (0.01 sec)修改表名:mysql> alter table t1 rename stuinfo; Query OK, 0 rows affected (0.00 sec)mysql> show tables; +---------------+ | Tables_in_db2 | +---------------+ | stuinfo | +---------------+ 1 row in set (0.00 sec)mysql> select * from stuinfo; +------+------+------+-------+ | age | user | sex | likes | +------+------+------+-------+ | 23 | | girl | it | +------+------+------+-------+ 1 row in set (0.00 sec)

MySQL鍵值

MySQL鍵值概述

鍵值類型
  • 根據數據存儲要求,選擇鍵值:
    ① index ---------- 普通索引
    ② unique --------- 唯一索引
    ③ fulltext --------- 全文索引
    ④ primary key — 主鍵
    ⑤ foreign key — 外鍵
索引介紹
  • 索引是什么?
    1.類似于書的目錄
    2.對表中字段值進行排序
    3.索引類型包括:Btree、B+tree、hash
    默認的索引類型是Btree(二叉樹)
索引的優缺點
  • 索引優點
  • 通過創建唯一性的索引,可以保證數據庫表中每一行數據的唯一性
  • 可以加快數據的查詢速度
  • 索引缺點
  • 當對表中的數據進行增加、刪除和修改的時候,索引也要動態的調整,降低了數據的維護速度
  • 索引需要占物理空間

MySQL鍵值的使用

index 普通索引
  • 使用規則
  • 一個表中可以有多個index字段
  • 字段的值允許重復,且可以賦予NULL值
  • 通常把作為查詢條件的字段設置為index字段
  • index字段的標志是 MUL
[root@host51 ~]# ls /var/lib/mysql/db2/ #索引信息存在stuinfo.ibd目錄下 db.opt stuinfo.frm stuinfo.ibd
  • 建表時創建索引
  • index (字段名),index(字段名)……
mysql> create table db2.t2 (-> name char(10),-> chengji int ,-> class char(7) ,-> index(name) ,index(chengji)-> ); Query OK, 0 rows affected (0.01 sec)mysql> desc db2.t2; +---------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+----------+------+-----+---------+-------+ | name | char(10) | YES | MUL | NULL | | | chengji | int(11) | YES | MUL | NULL | | | class | char(7) | YES | | NULL | | +---------+----------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
  • 在已有表里創建索引
  • create index 索引名 on 表名(字段名);
mysql> create index aaa on db2.stuinfo(user); #刪除時直接刪除aaa即可 Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> desc db2.stuinfo; +-------+--------------------------------+------+-----+-----------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------------------------+------+-----+-----------+-------+ | age | tinyint(3) unsigned | YES | | 23 | | | user | char(15) | NO | MUL | | | | sex | enum('boy','girl') | YES | | boy | | | likes | set('eat','game','money','it') | NO | | eat,money | | +-------+--------------------------------+------+-----+-----------+-------+ 4 rows in set (0.00 sec)
  • 查看索引信息
  • show index from 表名 \G
mysql> show index from db1.t5\G; #查看db1的t5表,t5表沒有做索引,所以顯示空 Empty set (0.00 sec)ERROR: No query specifiedmysql> show index from db2.stuinfo\G; *************************** 1. row ***************************Table: stuinfo Non_unique: 1Key_name: aaa #索引名稱,如果是建表時同時創建索引,則默認索引名和字段名相同Seq_in_index: 1Column_name: userCollation: ACardinality: 1Sub_part: NULLPacked: NULLNull: Index_type: BTREE #排隊類型,Btree(二叉樹)Comment: Index_comment: 1 row in set (0.00 sec)
  • 刪除索引
  • drop index 索引名 on 表名;
mysql> drop index chengji on db2.t2; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> desc db2.t2; +---------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+----------+------+-----+---------+-------+ | name | char(10) | YES | MUL | NULL | | | chengji | int(11) | YES | | NULL | | | class | char(7) | YES | | NULL | | +---------+----------+------+-----+---------+-------+ 3 rows in set (0.00 sec)mysql> drop index name on db2.t2; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> desc db2.t2; +---------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+----------+------+-----+---------+-------+ | name | char(10) | YES | | NULL | | | chengji | int(11) | YES | | NULL | | | class | char(7) | YES | | NULL | | +---------+----------+------+-----+---------+-------+ 3 rows in set (0.00 sec)

在建表時指定索引名,不使用默認和字段名同名,但不推薦修改索引名,因為如果要刪除還要先show create 查找當前表使用的索引名,再次刪除

mysql> create table db2.y (name char(3) ,age int , index(name)); Query OK, 0 rows affected (0.01 sec)mysql> desc db2.y; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | name | char(3) | YES | MUL | NULL | | | age | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.01 sec)mysql> show create table db2.y\G *************************** 1. row ***************************Table: y Create Table: CREATE TABLE `y` (`name` char(3) DEFAULT NULL,`age` int(11) DEFAULT NULL,KEY `name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec)mysql> create table db2.z (name char(3) ,age int ,key zzz (name)); Query OK, 0 rows affected (0.01 sec)mysql> desc db2.z; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | name | char(3) | YES | MUL | NULL | | | age | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.00 sec)mysql> show create table db2.z\G *************************** 1. row ***************************Table: z Create Table: CREATE TABLE `z` (`name` char(3) DEFAULT NULL,`age` int(11) DEFAULT NULL,KEY `zzz` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec)
primary key 主鍵
  • 使用規則
  • 字段值不允許重復,且不允許賦NULL值
  • 一個表中只能有一個primary key字段
  • 多個字段都作為主鍵,稱為復合主鍵,必須一起創建
  • 主鍵字段的標志是PRI
  • 主鍵通常與auto_increment連用
  • 通常把表中唯一標識記錄的字段設置為主鍵[記錄編號字段]
  • 在建表的時候,如果主鍵字段為int類型,還可以為其設置AUTO_INCREMENT自增屬性,這樣當添加新的表記錄時,此字段的值會自動從1開始逐個增加,無需手動指定。
  • 建表時創建主鍵
  • primary key(字段名);
mysql> create table db2.t3( name char(3) , age int , homeaddr char(9) , tel char(11), primary key(tel) ); Query OK, 0 rows affected (0.01 sec)mysql> desc db2.t3; +----------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+----------+------+-----+---------+-------+ | name | char(3) | YES | | NULL | | | age | int(11) | YES | | NULL | | | homeaddr | char(9) | YES | | NULL | | | tel | char(11) | NO | PRI | NULL | | +----------+----------+------+-----+---------+-------+ 4 rows in set (0.00 sec)mysql> create table db2.t4( name char(3) , age int , homeaddr char(9) , tel char(11) primary key ); #此時primary key做參數,也可以創建 Query OK, 0 rows affected (0.01 sec)mysql> desc db2.t4; +----------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+----------+------+-----+---------+-------+ | name | char(3) | YES | | NULL | | | age | int(11) | YES | | NULL | | | homeaddr | char(9) | YES | | NULL | | | tel | char(11) | NO | PRI | NULL | | +----------+----------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
  • 在已有表里添加主鍵,要求,必須是空表!!!
  • alter table 表名 add primary key(字段名);
mysql> select * from db2.t2; Empty set(0.00 sec)mysql> alter table db2.t2 add primary key(name); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> desc db2.t2; +---------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+----------+------+-----+---------+-------+ | name | char(10) | NO | PRI | NULL | | | chengji | int(11) | YES | | NULL | | | class | char(7) | YES | | NULL | | +---------+----------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
  • 刪除主鍵
  • 移除主鍵前,如果有自增屬性,必須先去掉
  • alter table 表名 drop primary key;
mysql> alter table db2.t2 drop primary key; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> desc db2.t2; +---------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+----------+------+-----+---------+-------+ | name | char(10) | NO | | NULL | | | chengji | int(11) | YES | | NULL | | | class | char(7) | YES | | NULL | | +---------+----------+------+-----+---------+-------+ 3 rows in set (0.00 sec)刪除主鍵后允許寫同樣的字段值,但依舊不允許為null
  • 創建復合主鍵(表中多列一起做主鍵),插入記錄是復合主鍵的值只要不是同時重復即可
  • primary key(字段名列表);
  • 與auto_increment連用
mysql> create table db2.t5(client_ip char(15) ,ser_port smallint , access_status enum ("allow" , "deny") , primary key (client_ip ,ser_port) ); Query OK, 0 rows affected (0.01 sec)mysql> desc db2.t5; +---------------+----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+----------------------+------+-----+---------+-------+ | client_ip | char(15) | NO | PRI | NULL | | | ser_port | smallint(6) | NO | PRI | NULL | | | access_status | enum('allow','deny') | YES | | NULL | | +---------------+----------------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)mysql> insert into db2.t5 values("1.1.1.1" , 21 , "allow" ); Query OK, 1 row affected (0.00 sec)mysql> insert into db2.t5 values("1.1.1.1" , 22 , "deny" ); Query OK, 1 row affected (0.00 sec)mysql> insert into db2.t5 values("2.1.1.1" , 22 , "deny" ); Query OK, 1 row affected (0.01 sec)mysql> select * from db2.t5; +-----------+----------+---------------+ | client_ip | ser_port | access_status | +-----------+----------+---------------+ | 1.1.1.1 | 21 | allow | | 1.1.1.1 | 22 | deny | | 2.1.1.1 | 22 | deny | +-----------+----------+---------------+ 3 rows in set (0.00 sec)主鍵與auto_increment連用 字段值自增長 相當于 i++mysql> create table db2.t6 (id int primary key auto_increment ,-> name char(10) ,-> tel char(11) ,-> qq char(11) -> ); Query OK, 0 rows affected (0.01 sec)mysql> desc db2.t6; +-------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | char(10) | YES | | NULL | | | tel | char(11) | YES | | NULL | | | qq | char(11) | YES | | NULL | | +-------+----------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)mysql> insert into db2.t6(name ,tel ,qq) values("bob" , "121212" , "2222"); Query OK, 1 row affected (0.00 sec)mysql> select * from t6; +----+------+--------+------+ | id | name | tel | qq | +----+------+--------+------+ | 1 | bob | 121212 | 2222 | +----+------+--------+------+ 1 row in set (0.00 sec)mysql> insert into db2.t6(name ,tel ,qq) values("aaa" , "133212" , "2222"); Query OK, 1 row affected (0.00 sec)mysql> select * from db2.t6; +----+------+--------+------+ | id | name | tel | qq | +----+------+--------+------+ | 1 | bob | 121212 | 2222 | | 2 | aaa | 133212 | 2222 | +----+------+--------+------+ 2 rows in set (0.00 sec)mysql> insert into db2.t6 values(null ,"aaa" , "133212" , "2222"); Query OK, 1 row affected (0.00 sec) #用null時沒有值,而我們給它自增,所以可以直接自增存入數據 mysql> select * from db2.t6; +----+------+--------+------+ | id | name | tel | qq | +----+------+--------+------+ | 1 | bob | 121212 | 2222 | | 2 | aaa | 133212 | 2222 | | 3 | aaa | 133212 | 2222 | +----+------+--------+------+ 3 rows in set (0.00 sec)mysql> insert into db2.t6 values(9 ,"xxxx" , "133212" , "2334"); Query OK, 1 row affected (0.00 sec)mysql> select * from db2.t6; +----+------+--------+------+ | id | name | tel | qq | +----+------+--------+------+ | 1 | bob | 121212 | 2222 | | 2 | aaa | 133212 | 2222 | | 3 | aaa | 133212 | 2222 | | 9 | xxxx | 133212 | 2334 | +----+------+--------+------+ 4 rows in set (0.00 sec)mysql> insert into db2.t6(name ,tel ,qq) values("sss" , "133212" , "5344"); Query OK, 1 row affected (0.00 sec)mysql> select * from db2.t6; +----+------+--------+------+ | id | name | tel | qq | +----+------+--------+------+ | 1 | bob | 121212 | 2222 | | 2 | aaa | 133212 | 2222 | | 3 | aaa | 133212 | 2222 | | 9 | xxxx | 133212 | 2334 | | 10 | sss | 133212 | 5344 | +----+------+--------+------+ 5 rows in set (0.01 sec)mysql> delete from db2.t6; #刪除所有 Query OK, 5 rows affected (0.00 sec)mysql> select * from t6; Empty set (0.00 sec)mysql> insert into db2.t6(name ,tel ,qq) values("qqq" , "133512" , "222344"); #再次添加,查看,發現id依然自增 Query OK, 1 row affected (0.01 sec)mysql> select * from db2.t6; +----+------+--------+--------+ | id | name | tel | qq | +----+------+--------+--------+ | 11 | qqq | 133512 | 222344 | +----+------+--------+--------+ 1 row in set (0.00 sec)mysql> insert into db2.t6(name ,tel ,qq) values("eee" , "13332112" , "26744"); Query OK, 1 row affected (0.00 sec)mysql> insert into db2.t6(name ,tel ,qq) values("rrr" , "004858" , "26989"); Query OK, 1 row affected (0.00 sec)mysql> select * from db2.t6; +----+------+----------+--------+ | id | name | tel | qq | +----+------+----------+--------+ | 11 | qqq | 133512 | 222344 | | 12 | eee | 13332112 | 26744 | | 13 | rrr | 004858 | 26989 | +----+------+----------+--------+ 3 rows in set (0.00 sec)只要沒有刪除auto_increment,自增就不會取消
foreign key 外鍵
  • 外鍵功能
  • 插入記錄時,字段值在另一個表字段值范圍內選擇
  • 使用規則
  • 表存儲引擎必須是innodb
  • 字段類型要一致
    被參照字段必須要是索引類型的一種(primary key)
  • 創建外鍵
  • create table 表名(字段名列表 , foreign key(字段名) references 表名(字段名) on update cascade on delete cascade)engine=innodb ;
  • foreign key(字段名) references 表名(字段名) #指定外鍵
  • on update cascade #同步更新
  • on delete cascade #同步刪除
  • engine=innodb #指定存儲引擎
mysql> create table db2.yg ( yg_id int primary key auto_increment , name char(15) )engine=innodb; Query OK, 0 rows affected (0.09 sec)mysql> create table db2.gz ( gz_id int , name char(15) , gz float(7,2), foreign key(gz_id) references db2.yg (yg_id) on update cascade on delete cascade )engine=innodb; Query OK, 0 rows affected (0.02 sec) #n update cascade on delete cascade 同步更新,表示與db2.yg表同步更新mysql> desc db2.yg; +-------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+----------------+ | yg_id | int(11) | NO | PRI | NULL | auto_increment | | name | char(15) | YES | | NULL | | +-------+----------+------+-----+---------+----------------+ 2 rows in set (0.06 sec)mysql> desc db2.gz; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | gz_id | int(11) | YES | MUL | NULL | | | name | char(15) | YES | | NULL | | | gz | float(7,2) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)mysql> insert into db2.yg(name) values ("bob"); Query OK, 1 row affected (0.00 sec)mysql> insert into db2.gz values(1 , "bob" , 50000) ; Query OK, 1 row affected (0.00 sec) #測試插入數據的約束功能mysql> select * from db2.yg; +-------+------+ | yg_id | name | +-------+------+ | 1 | bob | +-------+------+ 1 row in set (0.00 sec)mysql> select * from db2.gz; +-------+------+----------+ | gz_id | name | gz | +-------+------+----------+ | 1 | bob | 50000.00 | +-------+------+----------+ 1 row in set (0.00 sec) mysql> insert into db2.gz values(2 , "tom" , 60000) ; ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`db2`.`gz`, CONSTRAINT `gz_ibfk_1` FOREIGN KEY (`gz_id`) REFERENCES `yg` (`yg_id`) ON DELETE CASCADE ON UPDATE CASCADE) #測試插入數據的約束功能,不能在子鍵中直接創建,應該在db2.yg中測試同步更新:mysql> update db2.yg set yg_id=8 where name="bob" ; Query OK, 1 row affected (0.07 sec) Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from db2.yg; +-------+------+ | yg_id | name | +-------+------+ | 8 | bob | +-------+------+ 1 row in set (0.00 sec)mysql> select * from db2.gz; +-------+------+----------+ | gz_id | name | gz | +-------+------+----------+ | 8 | bob | 50000.00 | +-------+------+----------+ 1 row in set (0.00 sec)測試同步刪除:mysql> select * from db2.gz; +-------+------+----------+ | gz_id | name | gz | +-------+------+----------+ | 8 | bob | 50000.00 | +-------+------+----------+ 1 row in set (0.00 sec)mysql> delete from db2.yg where name="bob"; Query OK, 1 row affected (0.06 sec)mysql> select * from db2.yg; Empty set (0.00 sec)mysql> select * from db2.gz; Empty set (0.00 sec) mysql> insert into db2.yg values(6 , "jerry"); Query OK, 1 row affected (0.01 sec)mysql> insert into db2.gz values(6 , "jerry" , 65000); Query OK, 1 row affected (0.00 sec)mysql> select * from db2.yg; +-------+-------+ | yg_id | name | +-------+-------+ | 6 | jerry | +-------+-------+ 1 row in set (0.00 sec)mysql> select * from db2.gz; +-------+-------+----------+ | gz_id | name | gz | +-------+-------+----------+ | 6 | jerry | 65000.00 | +-------+-------+----------+ 1 row in set (0.00 sec)mysql> insert into db2.yg(name) values( "jerry"); Query OK, 1 row affected (0.00 sec)mysql> insert into db2.gz values(7 , "jerry" , 66000); Query OK, 1 row affected (0.00 sec)mysql> select * from db2.yg; +-------+-------+ | yg_id | name | +-------+-------+ | 6 | jerry | | 7 | jerry | +-------+-------+ 2 rows in set (0.00 sec)mysql> select * from db2.gz; +-------+-------+----------+ | gz_id | name | gz | +-------+-------+----------+ | 6 | jerry | 65000.00 | | 7 | jerry | 66000.00 | +-------+-------+----------+ 2 rows in set (0.00 sec)mysql> insert into db2.gz values(null , "tian" , 66000); Query OK, 1 row affected (0.00 sec)mysql> select * from db2.gz; +-------+-------+----------+ | gz_id | name | gz | +-------+-------+----------+ | 6 | jerry | 65000.00 | | 7 | jerry | 66000.00 | | NULL | tian | 66000.00 | +-------+-------+----------+ 3 rows in set (0.00 sec)mysql> insert into db2.gz values(7 , "jerry" , 66000); Query OK, 1 row affected (0.00 sec)mysql> select * from db2.gz; +-------+-------+----------+ | gz_id | name | gz | +-------+-------+----------+ | 6 | jerry | 65000.00 | | 7 | jerry | 66000.00 | | NULL | tian | 66000.00 | | 7 | jerry | 66000.00 | +-------+-------+----------+ 4 rows in set (0.00 sec) #此時,gz_id表中可以出現兩次7以及null,會出現沖突,我們要將gz_id設為主鍵 mysql> delete from db2.gz; Query OK, 0 rows affected (0.00 sec)mysql> alter table db2.gz add primary key (gz_id) ; Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> desc db2.gz; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | gz_id | int(11) | NO | PRI | NULL | | | name | char(15) | YES | | NULL | | | gz | float(7,2) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)mysql> select * from db2.gz; Empty set (0.01 sec)mysql> delete from db2.yg; Query OK, 2 rows affected (0.00 sec)mysql> insert into db2.yg values(6, "tom" ); Query OK, 1 row affected (0.00 sec)mysql> insert into db2.yg values(7, "tian" ); Query OK, 1 row affected (0.00 sec)mysql> insert into db2.yg values(6, "tom" ); Query OK, 1 row affected (0.00 sec)mysql> insert into db2.yg values(7, "tian" ); Query OK, 1 row affected (0.00 sec)mysql> select * from db2.yg; +-------+------+ | yg_id | name | +-------+------+ | 6 | tom | | 7 | tian | +-------+------+ 2 rows in set (0.00 sec)mysql> insert into db2.gz values (6 , "tom" ,20000); Query OK, 1 row affected (0.01 sec)mysql> insert into db2.gz values (7 , "tian" ,30000); Query OK, 1 row affected (0.01 sec)mysql> select * from db2.gz; +-------+------+----------+ | gz_id | name | gz | +-------+------+----------+ | 6 | tom | 20000.00 | | 7 | tian | 30000.00 | +-------+------+----------+ 2 rows in set (0.00 sec)mysql> insert into db2.gz values (7 , "tian" ,30000); ERROR 1062 (23000): Duplicate entry '7' for key 'PRIMARY' mysql> insert into db2.gz values (6 , "tom" ,20000); ERROR 1062 (23000): Duplicate entry '6' for key 'PRIMARY' mysql> insert into db2.gz values (null, "haha" ,20000); ERROR 1048 (23000): Column 'gz_id' cannot be null #此時gz_id不允許重復以及為空
  • 查看表的外鍵:
  • mysql> show create table 庫名.表名 \G
mysql> show create table db2.yg \G *************************** 1. row ***************************Table: yg Create Table: CREATE TABLE `yg` (`yg_id` int(11) NOT NULL AUTO_INCREMENT,`name` char(15) DEFAULT NULL,PRIMARY KEY (`yg_id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1 1 row in set (0.00 sec)mysql> show create table db2.gz \G *************************** 1. row ***************************Table: gz Create Table: CREATE TABLE `gz` (`gz_id` int(11) NOT NULL,`name` char(15) DEFAULT NULL,`gz` float(7,2) DEFAULT NULL,PRIMARY KEY (`gz_id`),CONSTRAINT `gz_ibfk_1` FOREIGN KEY (`gz_id`) REFERENCES `yg` (`yg_id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec)# CONSTRAINT `gz_ibfk_1`默認外鍵名稱
  • 刪除外鍵
  • alter table 表名 drop foreign key 外鍵名;
mysql> alter table db2.gz drop foreign key gz_ibfk_1 ; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> show create table db2.gz \G *************************** 1. row ***************************Table: gz Create Table: CREATE TABLE `gz` (`gz_id` int(11) NOT NULL,`name` char(15) DEFAULT NULL,`gz` float(7,2) DEFAULT NULL,PRIMARY KEY (`gz_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec)mysql> insert into db2.gz values (11 , "max" , 80000); Query OK, 1 row affected (0.00 sec)mysql> select * from db2.gz; +-------+------+----------+ | gz_id | name | gz | +-------+------+----------+ | 6 | tom | 20000.00 | | 7 | tian | 30000.00 | | 11 | max | 80000.00 | +-------+------+----------+ 3 rows in set (0.00 sec)#此時添加字段不受外鍵約束在已有表添加外鍵: mysql> alter table db2.gz add foreign key(gz_id) references db2.yg(yg_id) on update cascade on delete cascade ; #此時不允許添加外鍵,因為yg表中沒有id=11字段,受外鍵約束 ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`db2`.`#sql-443_3`, CONSTRAINT `#sql-443_3_ibfk_1` FOREIGN KEY (`gz_id`) REFERENCES `yg` (`yg_id`) ON DELETE CASCADE ON UPDATE CASCADE)mysql> delete from db2.gz where gz_id=11; #可以刪除gz表中id=11的字段,也可以在yg表中添加id=11字段 Query OK, 1 row affected (0.00 sec)mysql> alter table db2.gz add foreign key(gz_id) references db2.yg(yg_id) on update cascade on delete cascade ; #再次添加,此時不受約束,可以添加成功 Query OK, 2 rows affected (0.03 sec) Records: 2 Duplicates: 0 Warnings: 0mysql> show create table db2.gz \G #查看表的外鍵 *************************** 1. row ***************************Table: gz Create Table: CREATE TABLE `gz` (`gz_id` int(11) NOT NULL,`name` char(15) DEFAULT NULL,`gz` float(7,2) DEFAULT NULL,PRIMARY KEY (`gz_id`),CONSTRAINT `gz_ibfk_1` FOREIGN KEY (`gz_id`) REFERENCES `yg` (`yg_id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec)

總結

以上是生活随笔為你收集整理的Mysql数据库基础系列(二):表结构、键值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。