MySQl的库操作、表操作和数据操作
一、庫操作
1.1庫的增刪改查
(1)系統(tǒng)數(shù)據(jù)庫:
performance_schema:用來收集數(shù)據(jù)庫服務(wù)器的性能參數(shù),記錄處理查詢時發(fā)生的各種事件、鎖等現(xiàn)象
mysql:授權(quán)庫,主要存儲系統(tǒng)用戶的權(quán)限信息
test:MySQl數(shù)據(jù)庫系統(tǒng)自動創(chuàng)建的測試數(shù)據(jù)庫
?
(2)數(shù)據(jù)庫操作
創(chuàng)建:create database db1 charset utf8;
(數(shù)據(jù)庫命名規(guī)則:可以是字母、數(shù)字、下劃線等的組合,不能單獨使用數(shù)字,不能使用關(guān)鍵字例如create select等)
查看:show databases;? 或者 show create database db1;?
選擇數(shù)據(jù)庫:use db1;
刪除:drop database db1;
修改:alter database db1 charset utf8;
?
二、表操作
2.1存儲引擎介紹
(1)類似于處理文本用txt類型,圖片用png,音樂用mp3類型,數(shù)據(jù)庫的表也有不同類型。表類型(存儲和操作此表的類型)又稱存儲引擎。MySQL數(shù)據(jù)庫提供了多種存儲引 擎。
(2)查看MySQL支持的存儲引擎:
show engines\G? #查看所有支持的存儲引擎
show variables like 'storage_engine%';? #查看正在使用的存儲引擎
?
(3)部分存儲引擎的簡單介紹:有InnoDB、MyISAM、NDB、Memory、Infobright、NTSE、BLACKHOLE等;其中InnoDB是MySQL默認(rèn)和最常用的一個存儲引擎,具備高可用性、高性能以及高可擴展性。其他詳情點擊:具體介紹
(4)使用存儲引擎
建表時指定
create table innodb_t1(id int,name char)engine=innodb;
show create table innodb_t1;
?
2.2表的增刪改查
(1)表介紹
(2)創(chuàng)建表 create table student(sid int(11),sname char(10),gender enum('男','女'),class_id int(11));
? 查看庫下所有表? show tables;
?往表中插入數(shù)據(jù)? insert into student values(10,'nuo','女',5);
?
(3)查看表結(jié)構(gòu)? desc student;? 或者 show create table student\G;? #查看詳細(xì)表結(jié)構(gòu),可加\G
?
(4)修改表結(jié)構(gòu)
(4.1)修改存儲引擎? alter table student engine=NDB;
(4.2)添加字段? alter table student add age int not null;
(4.3)刪除字段? alter table student drop age;
(4.4)修改字段類型? alter table student modify age int(9) not null primary key;
(4.5)刪除主鍵? alter table student drop primary key;
(5)復(fù)制表
復(fù)制表結(jié)構(gòu)+記錄 (key不會復(fù)制: 主鍵、外鍵和索引)mysql> create table new_service select * from service;只復(fù)制表結(jié)構(gòu)mysql> select * from service where 1=2; //條件為假,查不到任何記錄Empty set (0.00 sec)mysql> create table new1_service select * from service where 1=2; Query OK, 0 rows affected (0.00 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> create table t4 like employees;(6)刪除表 drop table student;
2.3數(shù)據(jù)類型
mysql常用數(shù)據(jù)類型有 數(shù)值類型 int , float 等、字符串類型 char varchar 、日期類型 datatime等、枚舉類型和集合類型
(1)數(shù)值類型
(2)日期類型
(3)字符串類型 char 和 varchar
(char定長,varchar不定長,雖然varchar使用起來較為靈活,但是從整個系統(tǒng)的性能角度來說,char數(shù)據(jù)類型的處理速度更快,有時甚至可以超出varchar處理速度的 50%。因此,在選擇時,應(yīng)該綜合考慮,以求達到最佳的平衡。)
(4)枚舉類型和集合類型
枚舉enum(),集合set();枚舉單選,集合可多選;
2.4完整性約束
not null (非空)、default (默認(rèn))、primary key(主鍵,唯一)、foreign key(外鍵)、unique(唯一)、auto_increment(自增,整數(shù)類型且為主鍵)
(1)設(shè)置唯一約束unique:
方法一: create table department1( id int, name varchar(20) unique, comment varchar(100) );方法二: create table department2( id int, name varchar(20), comment varchar(100), constraint uk_name unique(name) );create table service( id int primary key auto_increment, name varchar(20), host varchar(15) not null, port int not null, unique(host,port) #聯(lián)合唯一 ); View Code
(2)primary key
1 # 單列做主鍵 2 #方法一:not null+unique 3 create table department1( 4 id int not null unique, #主鍵 5 name varchar(20) not null unique, 6 comment varchar(100) 7 ); 8 9 #方法二:在某一個字段后用primary key 10 create table department2( 11 id int primary key, #主鍵 12 name varchar(20), 13 comment varchar(100) 14 ); 15 16 #方法三:在所有字段后單獨定義primary key 17 create table department3( 18 id int, 19 name varchar(20), 20 comment varchar(100), 21 constraint pk_name primary key(id); #創(chuàng)建主鍵并為其命名pk_name 單列做主鍵 1 create table service( 2 ip varchar(15), 3 port char(5), 4 service_name varchar(10) not null, 5 primary key(ip,port) 6 ); 多列做主鍵
(3)foreign key
#表類型必須是innodb存儲引擎,且被關(guān)聯(lián)的字段,即references指定的另外一個表的字段,必須保證唯一 create table department( id int primary key, name varchar(20) not null )engine=innodb;#dpt_id外鍵,關(guān)聯(lián)父表(department主鍵id),同步更新,同步刪除 create table employee( id int primary key, name varchar(20) not null, dpt_id int, constraint fk_name foreign key(dpt_id) references department(id) on delete cascade on update cascade )engine=innodb;#先往父表department中插入記錄 insert into department values (1,'歐德博愛技術(shù)有限事業(yè)部'), (2,'艾利克斯人力資源部'), (3,'銷售部');#再往子表employee中插入記錄 insert into employee values (1,'egon',1), (2,'alex1',2), (3,'alex2',2), (4,'alex3',2), (5,'李坦克',3), (6,'劉飛機',3), (7,'張火箭',3), (8,'林子彈',3), (9,'加特林',3) ; View Code?
(4)auto_increment
#不指定id,則自動增長 create table student( id int primary key auto_increment, name varchar(20), sex enum('male','female') default 'male' );#應(yīng)該用truncate清空表,比起delete一條一條地刪除記錄,truncate是直接清空表,在刪除大表時用它 mysql> truncate student; Query OK, 0 rows affected (0.01 sec)#設(shè)置步長 sqlserver:自增步長基于表級別create table t1(id int。。。)engine=innodb,auto_increment=2 步長=2 default charset=utf8mysql自增的步長:show session variables like 'auto_inc%';#基于會話級別set session auth_increment_increment=2 #修改會話級別的步長#基于全局級別的set global auth_increment_increment=2 #修改全局級別的步長(所有會話都生效) 清空表:delete from t1; #如果有自增id,新增的數(shù)據(jù),仍然是以刪除前的最后一樣作為起始。 truncate table t1;數(shù)據(jù)量大,刪除速度比上一條快,且直接從零開始, View Code
三、數(shù)據(jù)操作
3.1數(shù)據(jù)的增刪改
假設(shè)已經(jīng)建了一張表student
增加:insert into student values
(值1,值2,值3...),
(值1,值2,值3...),
(值1,值2,值3...);
刪除:delete from student
where condition
更新:update student set
字段1=值1,
字段2=值2,
where condition;
?
3.2單表查詢
一、單表查詢基本語法
select 字段1,字段2... from 表名
where 條件
group by field
having 篩選
order by field
limit 限制條數(shù)
二、關(guān)鍵字在執(zhí)行中的優(yōu)先級
重點中的重點:關(guān)鍵字的執(zhí)行優(yōu)先級
from
where
group by
having
select
distinct?? 去重
order by
limit
三、where約束
1、比較運算符:> < >=? <= <>? !=
2、between 80 and 100 值在10到20之間
3、in(80,90,100) 值是10或20或30
4、like 'egon%'
pattern可以是%或_,
%表示任意多字符
_表示一個字符
5、邏輯運算符:在多個條件直接可以使用邏輯運算符 and or not
四、group by 分組
如果我們用unique的字段作為分組的依據(jù),則每一條記錄自成一組,這種分組沒有意義 多條記錄之間的某個字段值相同,該字段通常用來作為分組的依據(jù)
五、聚合函數(shù)
#強調(diào):聚合函數(shù)聚合的是組的內(nèi)容,若是沒有分組,則默認(rèn)一組
示例:
SELECT COUNT(*) FROM employee;
SELECT COUNT(*) FROM employee WHERE depart_id=1;
SELECT MAX(salary) FROM employee;
SELECT MIN(salary) FROM employee;
SELECT AVG(salary) FROM employee;
SELECT SUM(salary) FROM employee;
SELECT SUM(salary) FROM employee WHERE depart_id=3;
六、having
having和where不一樣的地方在于:
1、執(zhí)行優(yōu)先級從高到低? where > group by > having
2、where 在group by 之前,因此where中可以有任意字段,但是絕對不可以使用聚 合函數(shù)
3、having發(fā)生在分組group by之后,因此可以使用分組的字段,無法直接取到其他 字段,可以使用聚合函數(shù)。
七、order by 查詢排序
按單列排序:
SELECT * FROM employee ORDER BY salary;
SELECT * FROM employee ORDER BY salary ASC;
SELECT * FROM employee ORDER BY salary DESC;
?
按多列排序:先按照age排序,如果年紀(jì)相同,則按照薪資排序
SELECT * from employee
ORDER BY age, salary DESC;
八、使用正則表達式查詢
SELECT * FROM employee WHERE name REGEXP '^ale';
SELECT * FROM employee WHERE name REGEXP 'on$';
SELECT * FROM employee WHERE name REGEXP 'm{2}';
小結(jié):
對字符串匹配的方式
WHERE name = 'egon';
WHERE name LIKE 'yua%';
WHERE name REGEXP 'on$';
?
3.3多表查詢
多表連接查詢
符合條件連接查詢
子查詢
?
一、多表連接查詢
select 字段?
from 表一? inner|left|right? join 表二
on 表一.字段 = 表二.字段;
(1)交叉連接:不適用任何匹配條件,生成笛卡爾積
假設(shè)我們已經(jīng)生成employee,department兩張表
select * from employee,department;
(2) 內(nèi)連接:只連接匹配的行
select employee.id department.name?
from employee inner join department
on employee.id = department.id;
(3)外連接之左連接:優(yōu)先顯示左表全部記錄
select employee.id department.name?
from employee left join department
on employee.id = department.id;
(4)外連接之右連接:優(yōu)先顯示右表全部記錄
right join ,其他參考左連接。
(5)全外連接 :顯示左右兩個表全部記錄
select * from employee left join department on employee.dep_id = department.id
union
select * from employee right join department on employee.dep_id = department.id;
?
?
二、符合條件連接查詢
#示例:以內(nèi)連接的方式查詢employee和department表,并且以age字段的升序方式顯示
select employee.id,employee.name,employee.age,department.name
from employee,department
where employee.dep_id = department.id and age > 25
order by age asc;
?
三、子查詢
# 子查詢是將一個查詢語句嵌套在另一個查詢語句中。
#2:內(nèi)層查詢語句的查詢結(jié)果,可以為外層查詢語句提供查詢條件。
#3:子查詢中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等關(guān)鍵字
#4:還可以包含比較運算符:= 、 !=、> 、<等
1 帶IN關(guān)鍵字的子查詢 #查詢平均年齡在25歲以上的部門名 select id,name from departmentwhere id in (select dep_id from employee group by dep_id having avg(age) > 25);#查看技術(shù)部員工姓名 select name from employeewhere dep_id in (select id from department where name='技術(shù)');#查看不足1人的部門名 select name from departmentwhere id in (select dep_id from employee group by dep_id having count(id) <=1); 2 帶比較運算符的子查詢 #比較運算符:=、!=、>、>=、<、<=、<> #查詢大于所有人平均年齡的員工名與年齡 mysql> select name,age from emp where age > (select avg(age) from emp); +---------+------+ | name | age | +---------+------+ | alex | 48 | | wupeiqi | 38 | +---------+------+ rows in set (0.00 sec)#查詢大于部門內(nèi)平均年齡的員工名、年齡 select t1.name,t1.age from emp t1 inner join (select dep_id,avg(age) avg_age from emp group by dep_id) t2 on t1.dep_id = t2.dep_id where t1.age > t2.avg_age; 3 帶EXISTS關(guān)鍵字的子查詢 EXISTS關(guān)字鍵字表示存在。在使用EXISTS關(guān)鍵字時,內(nèi)層查詢語句不返回查詢的記錄。 而是返回一個真假值。True或False 當(dāng)返回True時,外層查詢語句將進行查詢;當(dāng)返回值為False時,外層查詢語句不進行查詢 #department表中存在dept_id=203,Ture mysql> select * from employee-> where exists-> (select id from department where id=200); +----+------------+--------+------+--------+ | id | name | sex | age | dep_id | +----+------------+--------+------+--------+ | 1 | egon | male | 18 | 200 | | 2 | alex | female | 48 | 201 | | 3 | wupeiqi | male | 38 | 201 | | 4 | yuanhao | female | 28 | 202 | | 5 | liwenzhou | male | 18 | 200 | | 6 | jingliyang | female | 18 | 204 | +----+------------+--------+------+--------+#department表中存在dept_id=205,False mysql> select * from employee-> where exists-> (select id from department where id=204); Empty set (0.00 sec) View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/NuoMiGao/p/9955836.html
總結(jié)
以上是生活随笔為你收集整理的MySQl的库操作、表操作和数据操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实验三总结
- 下一篇: DescriptionAttribute