Sql语句内功心法
CREATE SCHEMA <模式名> AUTHORIZATION <用戶名>
定義模式實際上定義了一個命名空間,在這個空間可以進一步定義該模式包含的數據庫對象,例如基本表,視圖,索引
?
DROP SCHEMA <模式名>? <CASCADE|RESTRICT>
選擇了CASCADE 表示刪除模式的同時把該模式下的數據庫對象全部一起刪除;
選擇了RESTRICT表示如果在該模式下定義了數據庫對象,則拒絕該刪除語句的執行;
?
Mysql,sql語句賞析
DROP TABLE IF EXISTS dl_user;CREATE TABLE dl_user( id int(12) NOT NULL auto_increment, email varchar(50) NOT NULL, nick_name varchar(30) default NULL, password varchar(50) NOT NULL, user_integral int(12) not null default '0', is_email_verify char(3), email_verify_code varchar(50) default NULL, last_login_time bigint default NULL, last_login_ip varchar(15), PRIMARY KEY (id), UNIQUE KEY email(email) )ENGINE=InnoDB DEFAULT CHARSET=UTF8;insert into dl_user(email,nick_name,password, user_integral,is_email_verify,email_verify_code, last_login_time,last_login_ip) values("jdbc@126.com","jdbc","1234",10,"Y","ABSDFWE",345123421345,"192.168.3.1");select * from dl_user; insert into dl_user(email,nick_name,password,user_integral,is_email_verify,email_verify_code,last_login_time,last_login_ip) values("jdbc22@126.com","jdbc","1234",10,"Y","ABSDFWE",345123421345,"192.168.3.1")?注意:
1
當我們要想數據庫中插入數據時,
行列一定要對應;
數字不必加單引號,字符串必須加單引號
?
insert into user(name,num,password) values('sufeng',7,'1234567')?
2
更新操作(包括刪除),在選定表,選定行列的基礎上再去修改,否則會釀成錯誤,
update dl_user set email= '1814545@qq.com' where id=7?
3 查詢操作
列-》變量
where是判斷條件
常用的查詢語句
# 查詢主鍵為32的商品 select * from goods where good_id=32; # 選擇不屬于第三欄目的商品 select good_id,cat_id,good_name from goods where cat_id != 3; # 不高于三千元的商品 select good_id,good_name form goods where shop_price<=3000; # 取出第七欄或者第十欄的商品 select good_id,good_name,cat_id from goods where cat_id=7 or cat_id=10;select good_id,good_name,cat_id from goods where cat_id in <7,11> ; #查詢價格在100到500之間的所有商品 select good_id,good_name.shop_price from goods where shop_price>=100 and shop_price <= 500;
select good_id,good_name,shop_price from goods where shop_price between 100 and 500; #取出不屬于第三欄目且不屬于第四欄目的商品 select good_id,good_name,cat_id from goods where cat_id not in <4,5>; #取出第三個欄目下邊價格在1000到3000,并且點擊率大于5的系列商品 select good_id,good_name,cat_id,shop_price,click_cout from where cat_id=3 and (shop_price<1000 or shop_price>3000) and click_count>5; # 模糊查詢 %通配任意字符 select good_id ,cat_id,good_name from goods where goods_name like 'windows%'; #模糊查詢 "_"通配一個字符 select good_id,cat_id,good_name,from goods where goods_name like 'window__'
?
# 將10到20內數字記為10,將20到30內數字記為20 update userinfo set age=floor(age/10)*10; # 替換字符串 select goods_id,concat('htc',substring(good_name,4)) from goods where good_name like '諾基亞%';?
# 統計函數 平均值函數select avg(good_price) from goods; # 統計函數 統計個數 select count(*) from goods;?
# 按照商品類別分組進行統計 select cat_id,avg(good_price) from goods group by cat_id; # where是對數據本身的判斷,having 是對結果集的判斷,兩者雖然都能起篩選的作用,但有著本質的區別 select good_id,(mark_price-show_price) as pro from goods where 1 having pro >200; #.... select name,sum(score<60) as gk,avg(score) from grades group by name having gk>=2; #....order by排序 asc(默認)升序 desc降序 limit 開始位置 條目數 select good_id,good_name.cat_id,show_price from goods order by cat_id asc,show_price desc limit 2,3; #5種 子句 順序 where , group by, having ,order by, limit # where 型子查詢select good_id,good_name,show price,cat_id from goods where good_id=(select max(good_id) from goods); # where 型子查詢select good_id,good_name,show price,cat_id from goods where good_id in (select max(good_id) from goods group by cat_id); # from型 子查詢 select * from (select good_id,good_name,cat_id,show_price from goods order by cat_id asc,good_id_desc) group by cat_id; #exist 子查詢 select * from category where exists(select * from goods where good.cat_id=category.cat_id); # 內連接查詢select xx from table1 inner join table2 on table1.xx =table2.xx; #左連接 查詢 左表為主,右表為輔 select xx from table1 left join table2 on table1.xx =table2.xx;
?
# 比賽詳情示例1select m.*,t1.tname as hteam,t2.tname as gteam from m inner join t as t1 on m.hid=t1.tid inner join as t2 on m.git =t2.tid ; # 比賽詳情示例2 select mid,t1.tname as hteam,t2.tname as gteam from m inner join t as t1 on m.hid=t1.tid inner join t as t2 on m.gid=t2.tid where matme between '2006-06-01' and '2006-07-01'; # union操作
select uid,name from user union select id,name from tmp;
?
? ?union的語句必須滿足一個條件,各語句取出的列數相同,列名稱未必要一致,列名稱會使用第一條sql語句的列。
# union 操作 select id,sum(num) from (select * from a union all select * from b) as tmp group by id;?使用union時 ,完全相等的行將會被合并;因此,這里直接采取union all
create? table 表名(
列1? ? 列類型? ?【列屬性】
列2? ?列類型? ? 【列屬性】
);
engine=存儲引擎
charset=字符集
?
整型列?
bigint? ? ? ? ?int? ? ? ? ? ?mediumint? ? ? ?smallint? ? ? ?tinyint
# 增加無符號列 alert table t2 add unum tinyint unsigned; #0填充,固定長度4 alert table t2 add sn tinyint(4) zerofill; # 查看表結構 desc t2;浮點型
float/double 有精度損失
(M,D) M時總位數,D是精確度
decimal定點型 更精確
字符型
char? 定常
varchar 變長
text?
blog
enum
#enum操作 create table t7( gender enum('男','女') );時間日期類型
#創建年月日時間表 create table t8( ya year, dt date, tm time, dttm datetime);#創建年 insert into t8 (ya) values(1996);#創建日期 insert into t8(dt) values('1990-12-23');#創建時間 insert into t8 (tm) values('18:23:45');#詳細時間 insert into t8 (dttm) values('1996-01-07 23:34:56');timestamp時間戳類型
定義該屬性 該列自動填充系統時間
?
#建表 不允許空 帶默認值 create table t10( id int not null default 0, name char(10) not null default ' ' ); #聲明表的主鍵 create table t11( id int primary key, name char(2) );create table t12( id int, name char(2), primary key (id) );?primary key此列不重復 可以做到區分元組
數值型primary key 一般與auto_increament聯用
建表時注意定常與變長,常用與不常用的結合,時刻考慮查詢速度及空間浪費。
?
改表名字
rename table regist3 to reg3;?
增加列
alter table reg3 add height tinyint unsigned not null default 0;刪除列
alter table reg3 drop column height;在指定列后添加
alter table reg3 add height tinyint unsigned after weight;?
修改列
?
按照商品類別分組 計算每種類別售價均值
create view v3 as select cat_id,avg(show_price) from goods group by cat_id;?
視圖的引入
視圖又被叫做虛擬表,時sql語句的查詢結果,
保證了數據表的安全,權限的控制(體現在對部分列的開放和關閉,),以及簡化可sql語句
create view v3 as select cat_id,avg(shop_price) as pj from goods group by cat_id; select * from v3 order by pj limit 0,3;?
視圖的更新 刪除,分情況,
如果視圖的每一行是與物理表一一對應的,則可以,
view的行是由物理表多行計算得到的結果,則不可以。
?
視圖的algorthm
對于簡單查詢形成的view,再對view查詢,如where,order 等等,可以把建視圖的語句和查視圖的語句
合并成差五里的語句,這種視圖的算法叫merge算法;
對于復雜查詢生成的視圖,結果集形成臨時表temp 表,再對臨時表進行操作;
create algorithm=merge view v7 as select goods_id,goods_name from goods;?
表管理語句
#選擇數據庫 use test;#顯示表 show tables; (#視圖也會被顯示)#顯示表結構 desc t12;#刪除表 drop table tmp; #刪除視圖 drop view vuser;#查看表詳細信息 show table status \G #(\G豎著顯示) show table status where name='goods' \G;#改表名 rename table oldName to Newname;#清空表數據 truncate t12;?
frm存儲表結構
myd數據信息
myi索引
存儲引擎 Myisam InnoDB Memory?
?
客戶端提交的字符集? character_set_client = gbk;
客戶端返回的字符集 character_set_results = gbk? ?(/utf8);
服務器端的字符集 character_set_connection = gbk;
簡寫 為 set names gbk;
校對集 排序規則
索引是數據的目錄,能快速定位數據的位置,
提高了查詢速度,增加了增刪改的麻煩,
一般在查詢頻率較高,重復度較低的列上加;
?
primary key();
key key1( key11)
unique key key2(key22)
可以設定索引的長度,多列索引以及索引修飾
create table t18( id int, name char(10), email char(20), primary key(id), key name(name), unique key email(email(10))key xx(name,email) );
?
show index from t19 /G;?
explain select * from t19 where xing='朱' and ming='院長' \G;?
#刪除索引 alter table t20 drop index m; create table t16( email char(30); )insert into t16 values('abc@163.c0m','121313@163.com');select left (email,3) from t16;select position ('@' in email ) from t16;select left (email,position('@' in email)-1) from t16;?
時間日期?
select date_formate(now(),'%Y%m');?
事務
update account set money=money+500; start transaction;commit;rollback;?
?
?模糊查詢? ?% 一個或多個字符
select * from studentinfo where sname like '張%';? any?
select sname as 補考學生 from studentinfo where sno=any(select sno from elective where score<60);? ?
? ? in
select sname as 考試不及格的學生from studentinfo where sno in (select sno from elective where score < 60) ;?
? ?not exist
select tname ,tpro from teacher where not exists (select * from teacher where tpro='教授');?
存儲過程
#結束符定義 delimter //#創建存儲過程 create procedure p_jiaoshi()beginselect * from teacher where tedu='碩士研究生';end//# 調用存儲過程 call p_jiaoshi();?
存儲過程與存儲函數都哦是由sql語句和過程式所組成的代碼片段,并且可以被應用程序和其他的sql語句調用,他們之間的區別在于
(1)存儲函數不能有輸出參數,因為存儲函數本身就是輸出參數;而存儲過程可以擁有輸出參數。
(2)可以直接對存儲函數進行調用,而不需要使用call與語句:對存儲過程的調用,需要使用call語句。
(3) 存儲函數中必須包含一條return語句,而這條特殊的sql語句不允許包含于存儲過程中。
?
# 創建過程函數 create function addTwoNumber(x small unsigned, y smallint unsigned) return smallint unsigned begin declare a,b smallint usigned default 10: set a=x, b=y; return a+b; end# 函數調用 set @num1=10; set @num2=20; set result = addTwoNumber(@num1,@num2); select @ result;?
?事務,原子性, 一致性, 隔離性, 持久性
# 開啟事務 start transation; insert into teacher values('t006','張君瑞','男'); insert into teacher values('t007','趙楠','女');# 在事務處理過程中,為了使執行的修改操作保存在數據庫中,事務處理結束必須由用戶提交 # 確定,使用commit語句 commit; select * from teacher;?
?在進行事務處理過程中,如果事務尚未提交時發現某些操作不合理,可以通過事務的回滾來取消當前事務,把數據庫恢復到事務處理之前的狀態。
rollback;?
轉載于:https://www.cnblogs.com/Su-feng-address/p/9710722.html
總結
- 上一篇: jdk官网历史版本下载Oracle账号密
- 下一篇: 二分查找算法为什么要先排序