mysql常用sql與命令之從入門到刪庫跑路
啟動與停止
啟動mysql服務(wù)
sudo /usr/local/mysql/support-files/mysql.server start
停止mysql服務(wù)
sudo /usr/local/mysql/support-files/mysql.server stop
重啟mysql服務(wù)
sudo /usr/local/mysql/support-files/mysql.server restart
進(jìn)入mysql目錄文件
cd /usr/local/mysql/support-files
進(jìn)入mysql命令行
/usr/local/MySQL/bin/mysql -uroot -p12345678
退出數(shù)據(jù)庫
exit;
數(shù)據(jù)庫相關(guān)操作
查詢所有數(shù)據(jù)庫
show databases;
選擇(使用)數(shù)據(jù)庫
use mybatis;
查詢當(dāng)前正在使用的數(shù)據(jù)庫名稱
select database();
創(chuàng)建數(shù)據(jù)庫
create database 數(shù)據(jù)庫名稱;
創(chuàng)建數(shù)據(jù)庫,判斷不存在,再創(chuàng)建: create database if not exists 數(shù)據(jù)庫名;
刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名稱;
判斷數(shù)據(jù)庫存在,存在再刪除:drop database if exists 數(shù)據(jù)庫名稱;
數(shù)據(jù)庫表相關(guān)操作
創(chuàng)建數(shù)據(jù)庫表
create table 表名( 列名1 數(shù)據(jù)類型1, 列名2 數(shù)據(jù)類型2, .... 列名n 數(shù)據(jù)類型n );
復(fù)制表
create table 表名 like 被復(fù)制的表名;
查看某個數(shù)據(jù)庫中的所有的數(shù)據(jù)表
show tables;
查看數(shù)據(jù)表的結(jié)構(gòu)
desc pet;或describe pet;
修改表名
alter table 表名 rename to 新的表名;
修改表的字符集
alter table 表名 character set 字符集名稱;
添加一列
alter table 表名 add 列名 數(shù)據(jù)類型;
刪除列
alter table 表名 drop 列名;
刪除表
drop table 表名;或drop table if exists 表名 ;
添加數(shù)據(jù)
insert into 表名(列名1,列名2,...列名n) values(值1,值2,...值n);
其中列名和值要一一對應(yīng)。如果表名后,不定義列名,則默認(rèn)給所有列添加值,如:insert into 表名 values(值1,值2,...值n);除了數(shù)字類型,其他類型需要使用引號(單雙都可以)引起來.
刪除數(shù)據(jù)
delete from 表名 where 條件
其中:如果不加條件,則刪除表中所有記錄。如果要刪除所有記錄, 使用delete from 表名;一般不推薦使用。這種操作有多少條記錄就會執(zhí)行多少次刪除操作.
TRUNCATE TABLE 表名;推薦使用,效率更高 先刪除表,然后再創(chuàng)建一張一樣的表.
修改數(shù)據(jù)
update 表名 set 列名1 = 值1, 列名2 = 值2,... where 條件;如果不加任何條件,則會將表中所有記錄全部修改.
insert into user2 values (1,'李四','123'); // 增 delete from pet where ower = 'disn'; //刪 update pet set name = '后裔' where ower = 'dfn'; //改
查詢數(shù)據(jù)
①> 、< 、<= 、>= 、= 、<> ②BETWEEN...AND ③ IN( 集合) ④LIKE 模糊查詢 ⑤_單個任意字符 ⑥%多個任意字符 ⑦IS NULL ⑧and 或 && ⑨or 或 || ⑩not 或 ! 查詢條件應(yīng)用舉例: SELECT * FROM user WHERE age >= 18; SELECT * FROM user WHERE age >= 18 AND age <=36; SELECT * FROM user WHERE age BETWEEN 40 AND 70; SELECT * FROM user WHERE age IN (6,18,37); // 關(guān)于NULL SELECT * FROM user WHERE height = NULL; 錯誤,因?yàn)閚ull值不能使用=或(!=) 判斷 SELECT * FROM user WHERE height IS NULL;(正確) SELECT * FROM user WHERE height IS NOT NULL;(正確) // 查詢姓陳的有哪些?< like> SELECT * FROM user WHERE NAME LIKE '陳%'; // 查詢姓名第二個字是新的人 SELECT * FROM user WHERE NAME LIKE "_新%"; // 查詢姓名是三個字的人 SELECT * FROM user WHERE NAME LIKE '___'; // 查詢姓名中包含狗的人 SELECT * FROM user WHERE NAME LIKE '%狗%';
約束相關(guān)
主鍵約束 (primary key)
能夠唯一確定一張表中的的一條記錄,我們通過給某個字段添加約束, 可以使得這個字段不重復(fù)且不為空.
create table user ( id int primary key auto_increment, // 在創(chuàng)建表時,添加主鍵約束,并且完成主鍵自增 name varchar(20) ); -- 聯(lián)合主鍵: 由多個字段聯(lián)合組成的主鍵, 只要聯(lián)合的主鍵加起來不重復(fù)就可以.聯(lián)合主鍵中的任何一個字段都不能為空. create table user2 ( id int, name varchar(20), password varchar(20), primary key(id, name) );
表創(chuàng)建完成后:
添加主鍵.如:
①alter table user add primary key(id);
②alter table user modify id int primary key;
刪除主鍵:alter table user drop primary key;
唯一約束:unique 約束修飾的字段的值不可以重復(fù).
create table user1 ( id int primary key auto_increment, phone_num varchar(20) unique ); create table user2 ( id int primary key auto_increment, name varchar(20), unique(id, name) // 表示兩個字段在一起不重復(fù)就可以 );
也可以在表創(chuàng)建完成后, 通過alter table user3 add unique(phone_num);
或alter table user3 modify phone_num varchar(20) unique;
來添加unique約束.
刪除unique約束:alter table user3 drop index phone_num;
非空約束:not null 修飾的字段不能為空NULL
create table user3 ( id int primary key auto_increment, name varchar(20) not null );
刪除非空約束:alter table user3 modify name varchar(20);
默認(rèn)約束
當(dāng)我們插入字段值時候,如果對應(yīng)的字段沒有插入值,則會使用默認(rèn)值.如果傳入了值,則不會使用默認(rèn)值.
create table user4( id int primary key auto_increment, age int default 18, name varchar(20) not null );
外鍵約束:foreign key
create table 表名( .... 外鍵列 constraint 外鍵名稱 foreign key (外鍵列名稱) references 主表名稱(主表列名稱) ); // 班級 create table classes( id int primary key, name varchar(20) ); // 學(xué)生表 create table student ( id int primary key, name varchar(20), class_id int, foreign key(class_id) references classes(id) );
數(shù)據(jù)庫查詢進(jìn)階
查詢所有記錄 例如:查詢student表中的所有記錄. select * from student; 查詢指定字段 例如:查詢student中的sname,ssex,class. select sname,ssex,class from student; 查詢教師表中所有的單位即不重復(fù)的depart列. <排除重復(fù)distinct> select distinct depart from teacher; 查詢score表中成績在60到80之間的所有記錄 <查詢區(qū)間 between…and…> select * from score where degree between 60 and 80; select * from score where degree > 60 and degree < 80; 查詢score表中成績?yōu)?5,86或88的記錄 select * from score where degree in(85, 86, 88); 查詢student表中'95031'班或性別為'女'的同學(xué)記錄. <or 表示或者> select *from student where class = '95031' or sex = '女'; 以class降序查詢student表的所有記錄 <降序:desc, 升序asc,默認(rèn)升序(省略)>. select * from student order by class desc; 以cno升序,degree降序查詢score表的所有記錄 select * from score order by cno asc,degree desc; 查詢"95031'班的學(xué)生人數(shù) <統(tǒng)計(jì) count> select count(*) from student where class = '95031'; 查詢score表中最高分的學(xué)生學(xué)號和課程號(子查詢) select sno, cno from score where degree = (select max(degree) from score );其中:select max(degree) from score 先查出最高分. select sno,cno degree from score order by degree desc limit 0,1;其中:limit第一個數(shù)字表示從多少開始,第二個表示多少條.當(dāng)有多個相同最高分時,容易出bug,不推薦使用這種方式查詢. 查詢每門課的平均成績 select cno, avg(degree) from score group by cno; 查詢score表中至少有2名學(xué)生選修的并以3開頭的課程的平均分?jǐn)?shù). select cno, avg(degree) from score group by cno having count(cno) >= 2 and cno like '3%'; 查詢分?jǐn)?shù)大于70, 小于90的sno列. select sno, degree from score where degree between 70 and 90; 查詢所有學(xué)生的sname, cno和degree列. select sname, cno, degree from student, score where student.sno = score.sno; 查詢所有學(xué)生的sno,cname和degree列 select sno,cname,degree from course ,score where course.cno = score.cno; 查詢"95031"班學(xué)生每門課的平均分. select cno, avg(degree) from score where sno in (select sno from student where class = '95031') group by cno; 查詢選修"3-105"課程的成績高于"109"號同學(xué)"3-105"成績的所有同學(xué)的記錄. select * from score where cno = '3-105' and degree > (select degree from score where sno = '109' and cno = '3-105'); 查詢成績高于學(xué)號為"109", 課程號為"3-105"的成績的所有記錄 select * from score where degree > (select degree from score where sno = '109' and cno = '3-105'); 查詢和學(xué)號為108,101的同學(xué)同年出生的所有的sno, sname, sbirthday select *from student where year(sbirthday) in (select year(sbirthday) from student where sno in(108, 101)); 查詢"張旭"教師任課的學(xué)生成績 select * from score where cno = ( select cno from course where tno = (select tno from teacher where tname = "張旭")); 查詢選修某課程的同學(xué)人數(shù)多于5人的教師姓名. select tname from teacher where tno = (select tno from course where cno = (select cno from score group by cno having count(*) > 5)); 查詢存在有85分以上的成績的課程的cno select cno, degree from score where degree > 85; 查詢出"計(jì)算機(jī)系"教師所教課程的成績表 select * from score where cno in (select cno from course where tno in (select tno from teacher where depart = "計(jì)算機(jī)系")); 查詢選修編號為"3-105"課程且成績至少高于選休息編號為"3-245"的同學(xué)的cno,sno和degree,并按degree從高到低次序排序. any 至少一個.
select * from score where cno = '3-105' and degree > any(select degree from score where cno = '3-245') order by degree desc;
查詢選修編號為"3-105"課程且成績高于選休息編號為"3-245"的同學(xué)的cno,sno和degree,并按degree從高到低次序排序.
all 表示所有
select * from score where cno = '3-105' and degree > all(select degree from score where cno = '3-245') order by degree desc;
查詢所有教師和同學(xué)的name, sex和birthday
select tname as name, tsex as sex, tbirthday as birthday from teacher union select sname, ssex, sbirthday from student;
查詢所有"女"教師和"女"同學(xué)的name,sex和birthday
select tname as name, tsex as sex, tbirthday as birthday from teacher where tsex = '女' union select sname, ssex, sbirthday from student where ssex = '女';
查詢成績比該課程成績低的同學(xué)的成績表
思路: 從a表查出對應(yīng)的分?jǐn)?shù)跟b表篩選出來的平均分作比較.
select * from score a where degree < (select avg(degree) from score b where a.cno = b.cno); 表a +-----+-------+--------+ | sno | cno | degree | +-----+-------+--------+ | 101 | 3-105 | 91 | | 102 | 3-105 | 92 | | 103 | 3-105 | 92 | | 103 | 3-245 | 86 | | 103 | 6-166 | 85 | | 104 | 3-105 | 81 | | 105 | 3-105 | 88 | | 105 | 3-245 | 75 | | 105 | 6-166 | 79 | | 109 | 3-105 | 76 | | 109 | 3-245 | 68 | | 109 | 6-166 | 81 | +-----+-------+--------+ 12 rows in set (0.00 sec) 表b | sno | cno | degree | +-----+-------+--------+ | 101 | 3-105 | 91 | | 102 | 3-105 | 92 | | 103 | 3-105 | 92 | | 103 | 3-245 | 86 | | 103 | 6-166 | 85 | | 104 | 3-105 | 81 | | 105 | 3-105 | 88 | | 105 | 3-245 | 75 | | 105 | 6-166 | 79 | | 109 | 3-105 | 76 | | 109 | 3-245 | 68 | | 109 | 6-166 | 81 | +-----+-------+--------+ 12 rows in set (0.00 sec)
查詢所有任課教師的tname和depart
select tname, depart from teacher where tno in (select tno from course);
查詢至少有兩名男生的班號
select class from student where ssex= '男' group by class having count(*) > 1
查詢student表中不姓"王"的同學(xué)記錄
select * from student where sname not like '王%';
查詢student表中每個學(xué)生的姓名和年齡
select sname, year(now()) - year(sbirthday) as '年齡' from student;
查詢student表中最大和最小的sbirthday日期值
select max(sbirthday) as '最大', min(sbirthday) as '最小' from student;
以班號和年齡從大到小的順序查詢student表中的全部記錄
select * from student order by class desc, sbirthday;
查詢"男"教師及其所上的課程
select * from course where tno in (select tno from teacher where tsex = '男');
查詢最高分同學(xué)的sno, cno和degree列
select * from score where degree = (select max(degree) from score);
查詢和李軍同性別的所有同學(xué)的sname
select sname from student where ssex = (select ssex from student where sname = '李軍');
查詢和李軍同性別并同班 同學(xué)sname
select sname from student where ssex = (select ssex from student where sname = "李軍") and class = (select class from student where sname = '李軍');
查詢所有選修"計(jì)算機(jī)導(dǎo)論"課程的"男"的成績表
select * from score where cno = (select cno from course where cname = '計(jì)算機(jī)導(dǎo)論') and sno in(select sno from student where ssex = '男');
SQL的四種連接查詢
分析用例的數(shù)據(jù)準(zhǔn)備: mysql> select * from person; +----+--------+--------+ | id | name | cardId | +----+--------+--------+ | 1 | 張三 | 1 | | 2 | 李四 | 3 | | 3 | 王五 | 6 | +----+--------+--------+ 3 rows in set (0.00 sec) mysql> select * from card; +------+-----------+ | id | name | +------+-----------+ | 1 | 飯卡 | | 2 | 建行卡 | | 3 | 農(nóng)行卡 | | 4 | 工商卡 | | 5 | 郵政卡 | +------+-----------+ 5 rows in set (0.00 sec)
內(nèi)連接
inner join
或者 join
, 后面通常跟對一個on
表示條件
---- 內(nèi)聯(lián)查詢: 就是兩張表中的數(shù)據(jù), 通過某個字段相等,查詢出相關(guān)記錄數(shù)據(jù).
<當(dāng)前表中的cardid與id相同.>
select * from person inner join card on person.cardId = card.id; +----+--------+--------+------+-----------+ | id | name | cardId | id | name | +----+--------+--------+------+-----------+ | 1 | 張三 | 1 | 1 | 飯卡 | | 2 | 李四 | 3 | 3 | 農(nóng)行卡 | +----+--------+--------+------+-----------+ 2 rows in set (0.00 sec)
外連接
左外連接:左連接 left join
或者 left outer join
---- 左外連接, 會把左邊表里面的所有數(shù)據(jù)取出來, 而右邊表中的數(shù)據(jù),如果有相等的,就顯示出來, 如果沒有, 則會補(bǔ)NULL.
select * from person left join card on person.cardId = card.id; +----+--------+--------+------+-----------+ | id | name | cardId | id | name | +----+--------+--------+------+-----------+ | 1 | 張三 | 1 | 1 | 飯卡 | | 2 | 李四 | 3 | 3 | 農(nóng)行卡 | | 3 | 王五 | 6 | NULL | NULL | +----+--------+--------+------+-----------+ 3 rows in set (0.00 sec)
右外連接:右連接 right join
或者right outer join
----右外連接, 會把右邊表里面的所有數(shù)據(jù)取出來, 而左邊表中的數(shù)據(jù),如果有相等的,就顯示出來, 如果沒有, 則會補(bǔ)NULL.
select * from person right join card on person.cardId = card.id; +------+--------+--------+------+-----------+ | id | name | cardId | id | name | +------+--------+--------+------+-----------+ | 1 | 張三 | 1 | 1 | 飯卡 | | 2 | 李四 | 3 | 3 | 農(nóng)行卡 | | NULL | NULL | NULL | 2 | 建行卡 | | NULL | NULL | NULL | 4 | 工商卡 | | NULL | NULL | NULL | 5 | 郵政卡 | +------+--------+--------+------+-----------+ 5 rows in set (0.01 sec)
全外連接:完全外連接 full join
或者full outer join
<mysql不支持full join>
mysql> select * from person full join card on person.cardId= card.id; ERROR 1054 (42S22): Unknown column 'person.cardId' in 'on clause' **** 解決mysql不支持full join的方法**** <左連接 + 右鏈接> , 即通過union來連接左右連接. <左連接 union 右鏈接>. eg: select * from person left join card on person.cardId = card.id union select * from person right join card on person.cardId = card.id; +------+--------+--------+------+-----------+ | id | name | cardId | id | name | +------+--------+--------+------+-----------+ | 1 | 張三 | 1 | 1 | 飯卡 | | 2 | 李四 | 3 | 3 | 農(nóng)行卡 | | 3 | 王五 | 6 | NULL | NULL | | NULL | NULL | NULL | 2 | 建行卡 | | NULL | NULL | NULL | 4 | 工商卡 | | NULL | NULL | NULL | 5 | 郵政卡 | +------+--------+--------+------+-----------+ 6 rows in set (0.01 sec)
要點(diǎn)梳理
where 和 having 的區(qū)別?
(1) having通常用在聚合函數(shù)前面,對聚合函數(shù)進(jìn)行過濾,(MAX、MIN、COUNT、SUM).having通常和group by 一起連用,因?yàn)閣here不能加在group by的后面.
(2) where 在分組之前進(jìn)行限定,如果不滿足條件,則不參與分組。having在分組之后進(jìn)行限定,如果不滿足結(jié)果,則不會被查詢出來. where 后不可以跟聚合函數(shù),having可以進(jìn)行聚合函數(shù)的判斷。
MYSQL執(zhí)行語句順序,嚴(yán)格遵循次順序,不能改變 select from where group by having order by
mysql的事務(wù)
關(guān)于事務(wù)
mysql中, 事務(wù)其實(shí)是一個最小的不可分割的工作單元. 事務(wù)能夠保證一個業(yè)務(wù)的完整性.
分析:
例如: a --> -100 update user set money = money - 100 where name = 'a'; b --> +100 update user set money = money + 100 where name = 'b'; -- 實(shí)際程序中, 如果只有一條sql語句執(zhí)行成功了,而另外一條沒有執(zhí)行成功?則會出現(xiàn)前后數(shù)據(jù)不一致的情況. update user set money = money - 100 where name = 'a'; update user set money = money + 100 where name = 'b'; 在多條sql語句,可能會有同時成功的要求,要么就同時失敗.
事務(wù)控制
(1)事務(wù)主要包含自動提交@@autocommit=1;,手動提交commit;和事務(wù)回滾rollback;.
(2) mysql默認(rèn)是開啟事務(wù)的(自動提交).
----當(dāng)我們?nèi)?zhí)行一個sql語句的時候,效果會立即提現(xiàn)出來,且不能回滾.
set autocommit = 0;設(shè)置mysql是否自動提交,<0為否, 1為是.>
select @@autocommit;查看mysql的自動提交方式.
commit; 手動提交.
具體事務(wù)控制相關(guān)參照下面代碼分析:
mysql> select @@autocommit; +--------------+ | @@autocommit | +--------------+ | 1 | +--------------+ 1 row in set (0.00 sec) // 建表 create database bank; create table user ( id int primary key, name varchar(20), money int ); // 首先在表中插入一條用戶數(shù)據(jù)a. insert into user values (1,'a',1000); Query OK, 1 row affected (0.00 sec) // 進(jìn)行回滾操作. mysql> rollback; Query OK, 0 rows affected (0.00 sec) // 執(zhí)行回滾后,查看數(shù)據(jù)表信息,發(fā)現(xiàn)即使調(diào)用了rollback,但插入的數(shù)據(jù)依然存在.說明當(dāng)前不能回滾. mysql> select * from user; +----+------+-------+ | id | name | money | +----+------+-------+ | 1 | a | 1000 | +----+------+-------+ 1 row in set (0.00 sec) // 可以通過設(shè)置msql的回滾自動提交為false. set autocommit = 0; Query OK, 0 rows affected (0.00 sec) mysql> select @@autocommit; +--------------+ | @@autocommit | +--------------+ | 0 | +--------------+ 1 row in set (0.00 sec) // 也就說, 通過上面的set autocommit = 0;操作關(guān)閉了mysql的自動提交(commit). *******再次插入數(shù)據(jù):******* insert into user values (2,'b',1000); Query OK, 1 row affected (0.00 sec) // 插入數(shù)據(jù)后查看表,用戶2數(shù)據(jù)添加成功. mysql> select * from user; +----+------+-------+ | id | name | money | +----+------+-------+ | 1 | a | 1000 | | 2 | b | 1000 | +----+------+-------+ 2 rows in set (0.00 sec) // 執(zhí)行回滾操作. mysql> rollback; Query OK, 0 rows affected (0.00 sec) // 回滾后再次查看表,發(fā)現(xiàn)剛才插入的數(shù)據(jù)已經(jīng)被干掉了. mysql> select * from user; +----+------+-------+ | id | name | money | +----+------+-------+ | 1 | a | 1000 | +----+------+-------+ 1 row in set (0.01 sec) **** 對于這種場景,如果想讓用戶b數(shù)據(jù)成功提交, 可以通過commit;命令執(zhí)行手動提交操作.手動提交后,如果想再次通過rollback來撤銷,則是不可以的.也就是說,事務(wù)一旦提交,執(zhí)行的sql語句就不可以再撤銷,也就是說事務(wù)一旦提交數(shù)據(jù)就會持久的產(chǎn)生效果.
(3)手動開啟事務(wù)
begin和start transaction都可以手動開啟一個事務(wù). 也就是說,當(dāng)我們當(dāng)前的mysql如果默認(rèn)的是自動提交模式,則執(zhí)行rollback進(jìn)行事務(wù)回滾則是無效的. 但是可以通過begin和start transaction手動開啟事務(wù).
即: 當(dāng)前默認(rèn)為自動提交模式,此時執(zhí)行rollback無效.執(zhí)行下面sql語句: start transaction;(或者begin;) update user set money = money - 100 where name = 'a'; update user set money = money + 100 where name = 'b'; 執(zhí)行完插入a,b用戶數(shù)據(jù)后,再執(zhí)行rollback,發(fā)現(xiàn)可以成功回滾事務(wù).可以成功切換成手動開啟事務(wù)的模式.若想使得插入的數(shù)據(jù)生效,也需要手動執(zhí)行commit進(jìn)行提交操作. 事務(wù)開啟之后,一旦commit提交,就不可以回滾,也就說,當(dāng)前的這個事務(wù)在提交的時候就已經(jīng)結(jié)束了.
事務(wù)的四大特征
A 原子性: 事務(wù)是最小的單元, 不可以在分割.
C 一致性: 事務(wù)要求, 同一事務(wù)中的sql語句必須保證同時成功,同時失敗.
I 隔離性: 事務(wù)1 和事務(wù)2之間shi具有隔離性的.
D 持久性: 事務(wù)一旦結(jié)束(commit,rollback),就不可以返回.
事務(wù)的隔離性
多個事務(wù)之間隔離的,相互獨(dú)立的。但是如果多個事務(wù)操作同一批數(shù)據(jù),則會引發(fā)一些問題,設(shè)置不同的隔離級別就可以解決這些問題.
存在問題:
(1) 臟讀:一個事務(wù),讀取到另一個事務(wù)中沒有提交的數(shù)據(jù).
(2)不可重復(fù)讀(虛讀):在同一個事務(wù)中,兩次讀取到的數(shù)據(jù)不一樣.
(3)幻讀:一個事務(wù)操作(DML)數(shù)據(jù)表中所有記錄,另一個事務(wù)添加了一條數(shù)據(jù),則第一個事務(wù)查詢不到自己的修改.
read uncommitted; 讀未提交的–>產(chǎn)生的問題:臟讀、不可重復(fù)讀、幻讀.
read committed; 讀已經(jīng)提交的–>產(chǎn)生的問題:不可重復(fù)讀、幻讀repeatable read; 可以重復(fù)讀–>產(chǎn)生的問題:幻讀
serializable; 串行化<性能特差>
通常是隔離級別越高,性能越差.
(1)查看數(shù)據(jù)庫的隔離級別
mysql默認(rèn)的隔離級別: REPEATABLE-READ
mysql8.0:
系統(tǒng)級別的:select @@global.transaction_isolation;
會話級別的:select @@transaction_isolation;
mysql5.x:
系統(tǒng)級別的:select @@global.tx_isolation;
會話級別的:select @@tx_isolation;
mysql> select @@global.transaction_isolation; +--------------------------------+ | @@global.transaction_isolation | +--------------------------------+ | REPEATABLE-READ | +--------------------------------+ 1 row in set (0.00 sec)
(2)修改隔離級別
set global tansaction isolation level read uncomitted;
數(shù)據(jù)庫的三大范式
第一范式
數(shù)據(jù)表中的所有字段都是不可分割的原子項(xiàng).初步可以理解為:字段值還可以繼續(xù)拆分的,就不滿足第一范式.
比如某表中有一個address的字段,插入值為"中國陜西省西安市碑林區(qū)柏樹林11號".該字段值是可以繼續(xù)拆分的,原則上就不滿足第一范式.可以依次拆分為:國家/省/市/區(qū)/街道等等.
當(dāng)然,范式設(shè)計(jì)的越詳細(xì),對某些實(shí)際操作可能會更好.但不一定都是好處.<比如對address字段來說,可能拆分開來永遠(yuǎn)都用不到這么詳細(xì)的信息,可能就沒有拆分的必要.>
第二范式
必須是滿足第一范式的前提下,第二范式要求,除主鍵外的每一列都必須完全依賴主鍵.如果要出現(xiàn)不完全依賴,只可能發(fā)生在聯(lián)合主鍵的情況下.
例如: create table myorder( product_id int, customer_id int, product_name varchar(20), customer_name varchar(20), primary key(product_id, customer_id ); 當(dāng)前表中, 除主鍵以外的其他列, 只依賴于主鍵的部分字段.則不滿足第二范式,通常需要拆表. create table myorder( order_id int primary key, product_id int, customer_id int ); create table product ( id int primary key, name varchar(20) ); create table customer( id int primary key, name varchar(20) ); 拆分成三個表后,滿足第二范式.
第三范式
必須先滿足第二范式.除開主鍵列的其他列之間不能有傳遞依賴關(guān)系.
附件
查詢語句所涉及的sql語句
create table student( sno varchar(20) primary key, sname varchar(20) not null, ssex varchar(20) not null, sbrithday datetime, class varchar(20) ); create table student( sno varchar(20) primary key, sname varchar(20) not null, ssex varchar(10) not null, sbirthday datetime, class varchar(20) ) create table teacher( tno varchar(20) primary key, tname varchar(20) not null, tsex varchar(20) not null, tbirthday datetime, prof varchar(20) not null, depart varchar(20) not null ); create table course( cno varchar(20) primary key, cname varchar(20) not null, tno varchar(20) not null, foreign key(tno) references teacher(tno) ); create table score( sno varchar(20) not null, degree decimal, primary key (sno, cno), foreign key (sno) references student(sno), foreign key (cno) references course(cno) ); insert into student values ('101','曾華','男','1977-09-01','95033'); insert into student values ('102','匡明','男','1975-10-02','95031'); insert into student values ('103','王麗','女','1976-01-23','95033'); insert into student values ('104','李軍','男','1976-02-20','95033'); insert into student values ('105','王芳','女','1975-02-10','95031'); insert into student values ('106','陸君','男','1974-06-03','95031'); insert into student values ('107','王尼瑪','男','1976-02-20','95033'); insert into student values ('108','張全蛋','男','1975-02-10','95031'); insert into student values ('109','趙鐵柱','男','1974-06-03','95031'); insert into teacher values ('804','李成','男','1958-12-02','副教授','計(jì)算機(jī)系'); insert into teacher values ('856','張旭','男','1969-03-12','講師','電子工程系'); insert into teacher values ('825','王萍','女','1972-05-05','助教','計(jì)算機(jī)系'); insert into teacher values ('831','劉冰','女','1977-08-14','助教','電子工程系'); insert into course values ('3-105','計(jì)算機(jī)導(dǎo)論', '825'); insert into course values ('3-245','操作系統(tǒng)', '804'); insert into course values ('6-166','數(shù)字電路', '856'); insert into course values ('9-888','高等數(shù)學(xué)', '831'); insert into score values('103','3-245','86'); insert into score values('105','3-245','75'); insert into score values('109','3-245','68'); insert into score values('103','3-105','92'); insert into score values('105','3-105','88'); insert into score values('109','3-105','76'); insert into score values('103','3-105','64'); insert into score values('105','6-166','79'); insert into score values('109','6-166','81'); create table person( id int primary key auto_increment, name varchar(20), cardId int ); create table card ( id int, name varchar(20) ); insert into card values (1,'飯卡'); insert into card values (2,'建行卡'); insert into card values (3,'農(nóng)行卡'); insert into card values (4,'工商卡'); insert into card values (5,'郵政卡'); insert into person values (1,'張三',1); insert into person values (2,'李四',3); insert into person values (3,'王五',6);
到此這篇關(guān)于mysql常用sql與命令之從入門到刪庫跑路的文章就介紹到這了,更多相關(guān)mysql 入門內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。