1、數據庫操作相關SQL ---- database
創建數據庫 create database 數據庫名稱; ------ 在sql後通過 character set 指定數據庫本身字符集,如果沒有指定將服務器默認
* 服務器默認字符集 mysql安裝目錄/my.ini [mysqld] default-character-set
查看當前有哪些數據庫 show databases;
修改數據庫(修改數據庫字符集) 數據庫字符集存放mysql安裝目錄/data/數據庫文件夾/db.opt
alter database 數據庫名稱 character set 字符集;
* collate 校對方式 ----- 用於數據庫排序; 每一種字符集都存在一種默認校對方式(可以不用修改)
刪除數據庫 drop database 數據庫名稱;
切換數據庫(設置當前使用數據庫) use 數據庫名稱;
* select database(); 查看當前使用數據庫
2、數據表操作相關SQL ---- table表結構
創建數據表 create table 表名(列名 類型(長度) 約束,列名 類型(長度) 約束... ) ----- 在創建表之前必須指定數據庫
* 查看當前有哪些數據表 show tables;
查看數據表表結構 desc table;
修改表結構:
修改列類型(長度) : alter table 表名 modify ...
添加一個新列 : alter table 表名 add ...
修改列名稱 : alter table 表名 change ...
刪除列 : alter table 表名 drop 列名
修改表名 : rename table 舊錶名 to 新表名
* table 在創建時 character set 指定表字符集,如果沒有指定採用數據庫默認字符集
刪除表 drop table 表名;
3、數據記錄增刪改查 insert update delete select
數據記錄插入 insert into 表名(列,...) values(值,...);
* 值的順序和列順序一致,個數一致 , 在開發中經常省略列名,值按照表結構所有字段進行設值
數據查看 select * from 表名;
數據記錄修改 update 表名 set 列名=值,列名= 值 where 條件語句
數據記錄刪除 delete from 表名 where 語句
* truncate 與 delete區別 ? truncate刪除表,重新創建, delete from 逐行刪除----- 性能truncate好於 delete,delete被事務控制,刪除後回滾取消刪除,truncate不可恢復
select 語句
S - F - W - G - H - O
select ... from ... where ... group by ... having ... order by ... ; 順序固定的
1、from 指定查詢數據表
2、where 前置過濾條件 --- 將表數據過濾掉一部分
3、group by 對where 過濾後數據進行分組
4、having 對分組後結果添加條件過濾
5、select 指定檢索哪些字段
6、order by 對檢索結果排序
4、數據庫備份和恢復
備份命令: mysqldump -u 用戶名 -p 數據庫名 > sql腳本位置 (回車輸入密碼)
恢復命令: mysql -u 用戶名 -p 數據庫名 < sql腳本位置 (回車輸入密碼 )
* 在mysql連接後,通過source 進行數據庫恢復 source sql腳本位置
5、數據庫完整性約束 ----- 保證數據表中記錄完整性
主鍵約束 primary key : 用來指定數據表數據記錄的唯一標識
唯一約束 unique : 該字段取值唯一
非空約束 not null ; 該字段值不能爲null
外鍵約束 foreign key : 當兩個數據表存在關聯時,添加外鍵約束,外鍵約束引用另一張表主鍵
條件約束 check : mysql不支持 Oracle支持 check age<100 ; 向數據表存入age值,必須小於100
* 完整性約束有5類
----------------------------------------------------------
多表設計
數據表與數據表之間關係三種:實體之間關係 多對多、一對多、一對一
多對多案例:項目和程序員
一個項目可以由多個程序員參與
一個程序員可以參與多個項目開發
建表原則:多對多關係,必須引入第三張數據表,同時引入另兩張實體表主鍵作爲外鍵
一對多案例:老師與課程
一個老師可以教授多門課程
一門課程只能有一個老師教授
建表原則:一對多關係,在多的一方添加一方 主鍵作爲外鍵
一對一關係:班級與班長關係
一個班只能有一個班長
一個班長只能負責一個班
* 該關係比較少見
建表原則:一對一關係,可以在任何一方添加 另一方主鍵作爲外鍵
建表練習:
設計學生成績管理系統數據表
1、每個教師可以教多門課程
2、每門課程可以由多個學生選修
3、每個學生可以選修多門課程
4、學生選修課程要有成績
關係表表名,通常用兩個實體表表名組合而成!
-------------------------------------------------------------------------------
笛卡爾積
當兩個數據表進行關聯查詢時,用第一張數據表每一條記錄去匹配第二張數據表每一條記錄。
第一張表10條數據
第二張表20條數據
使用笛卡爾積 結果 10*20 = 200 條記錄
在實際開發中,獲得笛卡爾積中有意義的記錄 ? ---- 連接查詢
內連接
外連接
內連接 : 將兩張表相同意義字段連接起來
select * from A,B where A.A_ID = B.A_ID; 條件 A表中A_ID與 B表中 A_ID 相等匹配
* 返回結果一定是兩個表都存在信息 , 最有意義的信息,如果第一張表記錄在第二張表找不到匹配信息,不顯示,第二張表記錄在第一張表無匹配信息,不顯示
第一張表10條數據
第二張表20條數據
內連接 結果 <= 10條
語法:select * from a inner join b on A.A_ID = B.A_ID;
簡化:select * from a,b where A.A_ID = B.A_ID;
外連接:左外連接、右外連接、全外連接
左外連接 :用第一張表每條記錄去匹配第二張表對應記錄,無論是否找到匹配信息,都顯示第一張表匹配結果
例如:每個水果價格 ? 沒有價格水果也要顯示
select * from a left outer join b on A.A_ID = B.A_ID ;
第一張表10條數據
第二張表20條數據
左外連接 --- 10條
右外連接:從第二張表找第一張表匹配記錄,無論是否找到,第二張表所有記錄都顯示
select * from a right outer join b on A.A_ID = B.A_ID ;
第一張表10條數據
第二張表20條數據
右外連接 --- 20條
全外連接:左外連接與右外連接 結果和 ---- 排除重複數據
select * from a full outer join b on A.A_ID = B.A_ID ; ----- MySQL 不支持
使用union關鍵字實現全外連接效果
select * from A left outer join B on A.A_ID = B.A_ID
union
select * from A right outer join B on A.A_ID = B.A_ID;
------------------------------------------------------------------------------------
關聯子查詢:將第一個查詢結果 ,作爲第二個查詢條件
查詢student表中年齡最大學員的信息
select * from student where age = (select max(age) from student);
等價於
select max(age) from student; ----- 25
select * from student where age = 25; ----- 學生信息
IN/EXISTS 當前查詢記錄在子查詢結果中存在
查詢所有成績小於60分的同學名稱
查詢studentcource表成績小於60 所有記錄
select student_id from studentcource where score < 60; --- 小於60分學生學號 2,8
再根據id去查詢學生表,得知學生姓名
select * from student where id in(2,8);
select * from student where id in(select student_id from studentcource where score < 60);
exists實現上面in 語句效果
select name from student where exists (select * from studentcource where score < 60 and student.id = studentcource.student_id);
select * from studentcource,student where score < 60 and student.id = studentcource.student_id;
select name from student where exists (select * from studentcource where score < 60 and student.id = studentcource.student_id);
* 在實際開發中 exists比 in效率要高
ANY、SOME、ALL 用法
SOME和ANY作用相同的 ----- 一些 >any(1,2,3) 大於任何一個都可以 等價於 >min
ALL ---- 所有 >all(1,2,3) 必須同時大於三個值 等價於 >max
查詢獲得最高分的學生學號
select max(score) from studentcource; 最高學分
select student_id from studentcource where score = (select max(score) from studentcource);
* 自我比較
select student_id from studentcource where score >=all(select score from studentcource);
查詢編號2課程比編號1課程成績高所有學號
select score from studentcource where cource_id = 2 and score > any(select score from studentcource where cource_id = 1);
select score from studentcource where cource_id = 2; 課程2所有成績
select score from studentcource where cource_id = 1; 課程1所有成績
使用union將兩個查詢結果合併,union 排重重複數據 union all 不會排重重複數據
* 合併時列名必須一致
------------------------------------------------------------------------------------------------------
查詢語文課程比數學課程成績高的所有學生的學號
mysql> select * from cource,studentcource where cource.id = studentcource.cource
_id and cource.name='語文';
+----+------+------------+------------+-----------+-------+
| id | name | teacher_id | student_id | cource_id | score |
+----+------+------------+------------+-----------+-------+
| 1 | 語文 | 1 | 1 | 1 | 80 |
| 1 | 語文 | 1 | 3 | 1 | 71 |
| 1 | 語文 | 1 | 5 | 1 | 60 |
| 1 | 語文 | 1 | 6 | 1 | 76 |
| 1 | 語文 | 1 | 10 | 1 | 77 |
+----+------+------------+------------+-----------+-------+
5 rows in set (0.02 sec)
mysql> select * from cource,studentcource where cource.id = studentcource.cource
_id and cource.name='數學';
+----+------+------------+------------+-----------+-------+
| id | name | teacher_id | student_id | cource_id | score |
+----+------+------------+------------+-----------+-------+
| 2 | 數學 | 1 | 1 | 2 | 90 |
| 2 | 數學 | 1 | 2 | 2 | 53 |
| 2 | 數學 | 1 | 3 | 2 | 70 |
| 2 | 數學 | 1 | 4 | 2 | 90 |
| 2 | 數學 | 1 | 5 | 2 | 70 |
| 2 | 數學 | 1 | 6 | 2 | 88 |
| 2 | 數學 | 1 | 8 | 2 | 71 |
| 2 | 數學 | 1 | 9 | 2 | 88 |
| 2 | 數學 | 1 | 10 | 2 | 76 |
+----+------+------------+------------+-----------+-------+
9 rows in set (0.00 sec)
select t1.student_id,t1.score 語文,t2.score 數學 from (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='語文') t1,(select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='數學') t2 where t1.student_id = t2.student_id and t1.score > t2.score;
查詢平均成績大於70分的同學的學號和平均成績
* 按人取平均成績 ------ 分組
select student_id,avg(score) from studentcource group by student_id having avg(score)>70;
* 打印學生姓名
select student.name,t.avgscore from student,(select student_id,avg(score) avgscore from studentcource group by student_id having avg(score)>70) t where student.id = t.student_id;
查詢所有同學的學號、姓名、選課數、總成績
*學生信息:select * from student;
*選課數、總成績 select student_id,count(*),sum(score) from studentcource group by student_id;
select student.id 學號,student.name 姓名,t.courcenum 選課數, t.sumscore 總成績 from student,(select student_id,count(*) courcenum,sum(score) sumscore from studentcource group by student_id) t where student.id = t.student_id;
查詢沒學過關羽老師課的同學的學號、姓名
* 關羽老師教什麼課 select cource.id from teacher,cource where teacher.id = cource.teacher_id and teacher.name='關羽';
* 選過關羽老師課 select distinct student_id from studentcource where cource_id in (select cource.id from teacher,cource where teacher.id = cource.teacher_id and teacher.name='關羽');
select id,name from student where id not in (select distinct student_id from studentcource where cource_id in (select cource.id from teacher,cource where teacher.id = cource.teacher_id and teacher.name='關羽'));
查詢學過語文並且也學過數學課程的同學的學號、姓名
* 語文和數據 課程編號 select id from cource where name='語文' or name='數學';
* 學過語文的學生 select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='語文';
* 學過數學的學生 select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='數學';
select t1.student_id from (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='語文') t1, (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='數學') t2 where t1.student_id = t2.student_id ;
select student.id,student.name from student,(select t1.student_id from (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='語文') t1, (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name='數學') t2 where t1.student_id = t2.student_id) t where student.id = t.student_id;
查詢學過趙雲老師所教的所有課的同學的學號、姓名
查詢沒有學三門課以上的同學的學號、姓名
查詢至少有一門課與學號爲“1”的同學所學相同的同學的學號和姓名
查詢和小李同學學習的課程完全相同的其他同學學號和姓名
查詢各科成績最高和最低的分
查詢學生信息和平均成績
查詢上海和北京學生數量
查詢不及格的學生信息和課程信息
查詢每門功成績最好的前兩名
統計每門課程的學生選修人數(超過兩人的進行統計)
把成績表中“關羽”老師教的課的成績都更改爲此課程的平均成績