DDL,DML,DQL

--DML操做:DML操做是對錶中的數據進行增、刪、改的操做.不要與DDL混淆了(INSERT、UPDATE、DELETE
---在mysql操做中字符串和日期都要用單引號,空值爲null;

--增長
insert into t_class (class_no,class_name)VALUES(1004,"二年三班");

--刪除
DELETE from t_subject where SUBJECT_no='B01';
select *from t_subject;
INSERT into t_subject values ('B001',"mysql");;

--更新
update t_subject set SUBJECT_no="B01" where SUBJECT_no="B001";
select *from t_subject;


--DQl操做:數據庫執行DQL語句不會對數據進行改動,而是hi數據庫發送結果給客戶端
---查詢返回的結果是一張虛擬表
__查詢關鍵字:SELECT

--1:基礎查詢
--1.1:查詢全部列
select *from t_class;

--1.2:查詢指定列
select class_no,class_name from t_class;


--2:條件查詢:條件查詢就是在查詢時where後面給出查詢條件

--2.1:查詢指定列(查詢class_no爲1006的時候class_name的值)
select  class_name from t_class where class_no=1006; --當要查詢的字符串條件值爲數值時能夠省略引號

--3:模糊查詢:當只知道查詢結果的一部分時
---通配符:
----任意一個字母用:-;
---- 任意0~n個字母用:%;

--3.1.1查詢姓名中含有字母a的學生的信息:
select *from t_student where stu_name like '%a%';

--3.1.2查詢姓名中第二個字母是h的學生的信息
select *from t_student where stu_name like '_h%';

--3.2:字符段的控制查詢
--3.2.1:去除重複記錄
select *from t_subject
--distinct
select distinct subject_no from t_subject;

---3.4排序
select * from t_student order by stu_no ASC--升序
select *from t_student order by stu_no desc --降序

--3.5聚合函數
--count():指定列不爲bull的記錄行數
select count(stu_no) from t_student;
max():計算指定列的最大值,若是指定列是字符串類型,那麼使用字符串排序運算
select max(score) from t_score ;
select max(subject_no) from t_score;
min :計算指定列的最小值
select min(score) from t_score;
--sum():計算指定列的數值和,若是指定類型不是數值類型.那麼計算結果爲0
select sum(score) from t_score;
select sum(subject_no) from t_subject;--結果爲0;
avg():計算指定列的平均值,若是指定列類型不是數值類型,那麼計算 結果爲0;
select avg(score) from t_score;
select avg(subject_no) from t_subject;--結果爲0;

--3.6:分組查詢
--注:凡和聚合函數同時出現的列名,則必定要寫在group by 以後;
--查詢學生的編號,和學生成績之和
select stu_no, sum(score) from t_score group by stu_no;
--注:having 和where 的區別:
1:having 是在分組後的對數據進行過濾;
  where 是在分組前對數據進行過濾
2 :having 後面能夠使用分組函數(既統計函數);
  where 後面不能夠使用分組函數;
where 是對分組前記錄的調件,若是某行記錄沒有知足where 的條件,那麼這條記錄不會參加分組
having 是對分組後的數據的約束;
--3.7
LIMIT分頁
LIMIT用來限定查詢結果的起始行,以及總行數
select *from t_score limit 0,2;--起始行從0開始,既第一行開始!
select *from t_score limit 2,2;查詢兩行記錄,起始行從第三行開始
insert into t_score values(88,2017001,'B01',4);
select *from t_score
分頁查詢:
查詢語句書寫順序:select-from -where group by -having -order by-limit;
查詢語句執行順序:from-where_group by-having-select-order by-limit;
4:DDL操做:使用的關鍵字:CREATE,ALTER,DROP
4.1:操做數據庫
建立
create database lihangdb1;
create  database lihangdb1 character set UTF8;--設置字符集,默認utf-8
查詢
查看當前數據服務器中的全部數據庫
show databases;--資料庫,數據庫
查看建立的lihangdb1數據庫的定義信息
show create database lihangdb1;
刪除前面建立的lihangdb1數據庫
drop database lihangdb1
查看當前使用的數據庫;
select database();
切換數據庫
use  db_student;
當前數據庫中的全部表
show TABLES;
查看錶的字段信息
desc t_student;
在上面的學生表再加一個age 列
alter table t_student add age blob;
alter table t_student add age DOUBLE;--後面列跟建立列的類型
查看錶格的建立細節
show create table t_student
刪除 age列
alter table t_student drop age;
--查詢
select *from t_student;mysql

相關文章
相關標籤/搜索