數據庫基礎(代碼)

--數據庫操做(DDL:Data Definition Languages) --建立數據庫(在磁盤上建立一個對應的文件夾)
CREATE DATABASE [IF NOT EXISTS ] aggressive2019 [character SET utf8]
--查看數據庫
SHOW databases; --查看全部數據庫
show CREATE DATABASE aggressive2019 --查看數據庫的建立方式
ALTER DATABASE aggressive2019 CHARACTER SET utf8 --修改數據庫
use aggressive2019;--使用數據庫
SELECT database();--查看當前使用的數據庫

--數據表操做
CREATE TABLE employee( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20),gender bit DEFAULT 1, birthday DATE, entry_data date, job VARCHAR(20), salary DOUBLE(4,2) UNSIGNED, resume text); --查看錶結構
DESC employee; --查看錶結構
SHOW columns from employee; --查看當前數據庫表建表語句
SHOW CREATE TABLE employee; --增長列(字段)ALTER
ALTER TABLE employee ADD addr VARCHAR(20) NOT NULL UNIQUE AFTER name; --添加多個字段
ALTER TABLE employee ADD age int FIRST , ADD workage VARCHAR(20) AFTER addr; --修改一列類型 modify
ALTER TABLE employee MODIFY age TINYINT DEFAULT 20; --修改列名 change
ALTER TABLE employee CHANGE age AGE INT DEFAULT 28 FIRST; --刪除一列
ALTER TABLE employee DROP addr; --修改表名
RENAME TABLE employee to employee1; --修改表所用的字符集
ALTER TABLE employee CHARACTER SET utf8; --刪除表
drop table employee; --添加主鍵,刪除主鍵
ALTER TABLE employee add PRIMARY KEY(id); ALTER TABLE employee MODIFY id INT AUTO_INCREMENT; --刪除主鍵
alter table employee modify id int; ALTER TABLE employee drop PRIMARY KEY ; --添加惟一索引
ALTER TABLE employee ADD UNIQUE INDEX index_age(age); --添加聯合索引
ALTER TABLE employee ADD UNIQUE INDEX name_age(name,age); --刪除惟一索引
ALTER TABLE employee DROP INDEX name_age; --建立文章表
CREATE TABLE article( id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(20), publish_date INT, click_num INT, is_top TINYINT(1), content TEXT); --完整性約束條件之主鍵約束(一張表只能有一個主鍵,非空且惟一,主鍵類型不必定是非整型) --單字段主鍵
CREATE TABLE users(id INT PRIMARY KEY , name VARCHAR(20), city VARCHAR(20)); --多字段聯合主鍵
CREATE TABLE users2( id INT, name VARCHAR(20), city VARCHAR(20), PRIMARY KEY (name,id));
數據表操做
--表記錄之增、刪、改 --增長一條記錄
CREATE TABLE employee_new( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20) not null unique, birthday VARCHAR(20), salary FLOAT(7,2)); INSERT INTO employee_new(id, name, birthday, salary) VALUES (1,'yuan','1990-09-09',9000); INSERT INTO employee_new  VALUES (2,'吳西平','1988-08-07',10000); INSERT INTO employee_new (name,salary)VALUES ('xialv',1000); --插入多條數據
INSERT into employee_new values(4,'alvin1','1993-04-20',3000), (5,'alvin2','1995-05-12',5000); INSERT INTO employee_new SET id=12,name='alvin3'; --set 插入法 --修改表記錄
UPDATE employee_new SET birthday='1989-10-24' WHERE id=1; --將yuan的薪水在原有的基礎上增長1000元
UPDATE employee_new SET salary=salary+1000 where name='yuan'; --刪除表記錄(delete from employee_new WHERE ...) --刪除表中名稱爲alex的記錄
DELETE FROM employee_new WHERE name='alex'; --刪除表中全部記錄
DELETE from employee_new; --使用truncate刪除表中的記錄
TRUNCATE table employee_new;
表記錄操做
--查詢表達式
   SELECT *|field1,filed2 ...   FROM tab_name WHERE 條件 GROUP BY field HAVING 篩選 ORDER BY field LIMIT 限制條數 CREATE TABLE ExamResult(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20), JS DOUBLE, Django DOUBLE, OpenStack DOUBLE); INSERT INTO ExamResult VALUES (1,'yuan',98,98,98), (2,'xialv',35,98,67), (3,'alex',59,59,62), (4,'wusir',88,89,82), (5,'alvin',88,98,67), (6,'yuan',86,100,55); --查詢表中全部學生的信息
SELECT * FROM ExamResult; --過濾表中的重複數據
SELECT DISTINCT JS FROM ExamResult; --查詢表中全部學生的姓名和對應的JS成績
SELECT name,JS FROM ExamResult; --SELECT 也能夠使用表達式,而且能夠使用:字段 as 別名或者:字段 別名
SELECT name,JS+10,Django+10,OpenStack+10 FROM ExamResult; --統計每一個學生的總分
SELECT name,JS+Django+OpenStack FROM ExamResult; --使用別名表示學生總分
SELECT name as 姓名,JS+Django+OpenStack as 總成績 FROM ExamResult; select name ,JS+Django+OpenStack 總成績 FROM ExamResult; SELECT name JS FROM ExamResult;--記得加逗號
表記錄之查詢(單表查詢)
--查詢姓名爲yuan的學生成績
SELECT * from ExamResult WHERE name='yuan'; --查詢JS成績大於90分的同窗
SELECT id,name,JS FROM ExamResult WHERE JS>90; --查詢總分大於200分的同窗
SELECT name,JS+Django+OpenStack as 總成績 FROM ExamResult WHERE JS+Django+OpenStack>200; --查詢JS分數在70——100之間的同窗
SELECT name,JS FROM ExamResult WHERE JS BETWEEN 70 AND 100; --查詢Django分數爲75,98,77的同窗
SELECT name,Django FROM ExamResult WHERE Django in (75,98,77); --查詢全部姓王的學生的成績
SELECT * FROM ExamResult WHERE name like '王%';(若是是%則表示任意多字符,此例如唐僧,唐國強 若是是_則表示一個字符唐_,只有唐僧符合。兩個_則表示兩個字符:__) --查詢JS>90,Django>90的同窗
SELECT id,name FROM ExamResult WHERE JS>90 AND Django>90; --查詢缺考數學的學生姓名
SELECT name FROM ExamResult WHERE JS is NULL ;
使用where子句,進行過濾查詢
1 --Asc升序、Desc 降序,其中asc爲默認值 order by 子句應位於select 語句的結尾
2 --對JS成績排序後輸出
3 SELECT * FROM ExamResult ORDER BY JS; 4 --對總分排序後從高到底的順序輸出
5 SELECT name,JS+Django+OpenStack as 總成績 FROM ExamResult ORDER BY 總成績 DESC ; 6 --對姓李學生成績排序輸出
7 SELECT name,JS+Django+OpenStack as 總成績 FROM ExamResult WHERE name LIKE 'a%' ORDER BY 總成績 DESC ;
order by 指定排序的列,排序的列便可是表中的列名,也能夠是select語句後製定的別名
CREATE TABLE order_menu(id INT PRIMARY KEY AUTO_INCREMENT, product_name VARCHAR(20), price FLOAT(6,2), born_date DATE, class VARCHAR(20)); INSERT INTO order_menu (product_name,price,born_date,class) VALUES ("蘋果",20,20170612,"水果"), ("香蕉",80,20170602,"水果"), ("水壺",120,20170612,"電器"), ("被罩",70,20170612,"牀上用品"), ("音響",420,20170612,"電器"), ("牀單",55,20170612,"牀上用品"), ("草莓",34,20170612,"水果"); --group by 字句,其後能夠接多個列名,也能夠跟having子句,對group by的結果進行篩選 --按位置字段進行篩選
select * from order_menu group by 5; --對購物表按類名分組後顯示每一組商品的價格總和
SELECT class,sum(price) from order_menu GROUP BY class; --對購物表按類名分組後顯示每一組商品價格總和超過150的商品
SELECT class,sum(price) from order_menu GROUP BY class HAVING sum(price)>150; --注意:having 和 where二者均可以對查詢結果進行進一步的過濾,差異有:
                     <1>where語句只能用在分組以前的篩選,having能夠用在分組以後的篩選; <2>使用where語句的地方均可以用having進行替換 <3>having中能夠用聚合函數,where中就不行。 --group_concat()函數
SELECT id,group_concat(name),group_concat(JS) FROM ExamResult group by id;
group by 分組查詢
--統計表中全部記錄 --COUNT(列名)統計行的個數 --統計一個班有多少學生,先查出全部學生,再用count包上
select count(*) from ExamResult; --統計JS成績大於70的學生有多少個
select count(JS) from ExamResult where JS>70; --統計總分大於280的人數有多少個
select count(name) from ExamResult where JS+Django+OpenStack>280;
聚合函數(先把要求的內容查出來再包上聚合函數便可)
--統計一個班JS的總成績,先查出全部JS成績,再用SUM包上
SELECT sum(JS) AS JS總成績 FROM ExamResult; --統計一個班各科分別的總成績
select sum(JS)AS JS總成績,sum(Django) AS Django總成績,sum(OpenStack) AS OpenStack總成績 FROM ExamResult; --統計一個班各科的成績總和
select sum(ifnull(JS,0)+ifnull(Django,0),+ifnull(OpenStack,0)) AS 總成績 FROM ExamResult; SELECT sum(JS+OpenStack+Django) AS 總成績 FROM ExamResult; --統計一個班JS成績平均分
SELECT sum(JS)/count(*) FROM ExamResult;
SUM(列名):統計知足條件的行的內容和
--求一個班級JS平均分?先查出全部JS分,而後用avg包上
SELECT avg(JS) FROM ExamResult; --求一個班級總分平均分
SELECT avg(JS+Django+OpenStack) FROM ExamResult; SELECT avg((ifnull(JS,0)+ifnull(Django,0)+ifnull(OpenStack,0))) FROM ExamResult;
AVG(列名)
--求班級最高分和最低分(數值範圍在統計中特別有用)
SELECT max(ifnull(JS,0)+ifnull(Django,0)+ifnull(OpenStack,0)) 最高分 FROM ExamResult; SELECT min(ifnull(JS,0)+ifnull(Django,0)+ifnull(OpenStack,0)) 最低分 FROM ExamResult; --求購物表中單價最高的商品名稱及價格
SELECT id,max(price) FROM order_menu; SELECT max(price) FROM order_menu;
max,min
--注意
Mysql在執行sql語句時的執行順序: -- from where select group by having order by
分析: SELECT JS as JS成績 FROM ExamResult WHERE JS成績>90;不成功 SELECT JS as JS成績 FROM ExamResult HAVING JS成績>90;--成功
注意
SELECT * FROM ExamResult LIMIT 2; --按順序顯示兩條記錄
SELECT * FROM ExamResult LIMIT 2,3;--跳過前兩條顯示接下來的三條記錄
SELECT * FROM ExamResult LIMIT 2,2;
limit
SELECT * FROM ExamResult WHERE name REGEXP 'yu'; SELECT * FROM ExamResult WHERE name REGEXP 'yun$'; SELECT * FROM ExamResult WHERE name REGEXP 'm{2}';
使用正則表達式查詢
建立外鍵 --每個班主任會對應多個學生,而每個學生只能對應一個班主任 --主表
create table ClassCharger( id tinyint primary key auto_increment, name VARCHAR(20), age INT, is_married boolean ); insert into ClassCharger(name, age, is_married) VALUES ('冰冰',12,0), ('丹丹',14,0), ('歪歪',22,0), ('姍姍',20,0), ('小雨',21,0); --子表(--做爲外鍵必定要和關聯主鍵的數據類型保持一致)
create table Student( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20), charger_id TINYINT) ENGINE =innodb; INSERT INTO Student(name,charger_id) VALUES ("alvin1",2), ("alvin2",4), ("alvin3",1), ("alvin4",3), ("alvin5",1), ("alvin6",3), ("alvin7",2); --增長和刪除外鍵
alter table Student ADD constraint abc foreign key (charger_id) references ClassCharger(id); alter TABLE Student drop FOREIGN KEY abc;
外鍵約束
CREATE DATABASE aggressive2020 CHARACTER SET utf8; create table employee(emp_id INT primary key not null, emp_name varchar(50), age int, dept_id int); INSERT into employee(emp_id,emp_name,age,dept_id)VALUES (1,'A',19,200), (2,'B',26,201), (3,'C',30,201), (4,'D',24,202), (5,'E',20,200), (6,'F',38,204); create table department( dept_id int, dept_name varchar(100)); insert into department values (200,'人事部'), (201,'技術部'), (202,'銷售部'), (203,'財政部'); --內鏈接:僅選出兩張表中互相匹配的記錄,而外鏈接會先出其餘不匹配的記錄,最經常使用的是內鏈接 -- select * from employee,department where employee.dept_id = department.dept_id; --select * from employee inner join department on employee.dept_id = department.dept_id;
外鏈接: 左鏈接:包含全部左邊表中的記錄甚至是右邊表中沒有和它匹配的記錄 右鏈接:包含全部右邊表中的記錄甚至是左邊表中沒有和它匹配的記錄 全外鏈接:在內鏈接的基礎上增長左邊有右邊沒有和右邊有左邊沒有的結果 select * from employee RIGHT JOIN department on employee.dept_id = department.dept_id UNION
   select * from employee LEFT JOIN department on employee.dept_id = department.dept_id; 多表查詢之複合條件查詢 --查詢員工年齡大於等於25的部門
select distinct dept_name from employee,department where employee.dept_id =department.dept_id AND age>25; --之內鏈接的方式查詢employee和department表,而且以age字段升序方式顯示
SELECT * FROM employee,department WHERE employee.dept_id=department.dept_id ORDER BY age ASC ; --多表查詢之子查詢 --查詢employee表,但dept_id必須在department表中出現過
select * from employee WHERE dept_id in (SELECT dept_id FROM department); --查詢員工年齡大於等於25歲的部門
select dept_id,dept_name FROM department WHERE dept_id IN ( SELECT DISTINCT dept_id FROM employee WHERE age>25); select * FROM employee WHERE exists(SELECT dept_name FROM department WHERE dept_id=205);
多表查詢
相關文章
相關標籤/搜索