SQL語句及查詢關鍵字


MySQL

約束條件

1.unsigned  設置無符號
# create table t2(id int unsigned);

2.zerofill  零填充
# create table t4(id int(10) zerofill);

3.not null  非空
	不等於''
#  create table t6(id int not null);

4.default  默認值

5.unique  唯一
# 單列唯一
create table t7(id int,name varchar(16) unique);

# 多列唯一
create table t8(id int,host varchar(16),port int,unique(host,port));

6.primary key 主鍵
  auto_increment  為主鍵使用,自增,每次加1
  
 # 主鍵相當於非空且唯一,InnoDB規定一張表必須有一個主鍵
 #	主鍵查詢速度快,本質也是一種索引
 create table t9(id int primary key auto_increment,name varchar(16));
 
 # 結論(默認創建格式)
 	id int primary key auto_increment
 	cid int primary key auto_increment
 	uid int primary key auto_increment

清空表數據

delete from t9; #  清空表數據,新建id繼續增加
truncate table t9; # 清空表數據,id會被重置為1,推薦使用

binlog   恢復數據

SQL語句實現表關系

# 一對多
# 外鍵建在多的一方
# 先創建沒有外鍵的表
create table dep(
	id int primary key auto_increment,
    name varchar(32),
    descript varchar(32)
)

create table userinfo(
	id int primary key auto_increment,
    username varchar(32),
    dep_id int,
    salary decimal(8,2),
    foreign key(dep_id) references dep(id)
)


# 多對多,需要創建第三張表,
create table book(
	id int primary key auto_increment,
    title varchar(16),
    price int
)
insert into book(title,price) values('豐乳肥臀',100);

create table author(
	id int primary key auto_increment,
    name varchar(16),
    phone int
)
insert into author(name,phone) values('莫言',123456);

create table book2author(
	id int primary key auto_increment,
    book_id int,
    author_id int,
    foreign key(book_id) references author(id)
    on update cascade   	# 級聯更新
    on delete cascade,		# 級聯刪除
    
    foreign key(author_id) references book(id)
    on update cascade   
    on delete cascade
)
# 一對一
create table author_detail(
	id int primary key auto_increment,
    title varchar(16),
    price int
)
insert into author_detail(title,price) values('暖陽',100);

create table author_2(
	id int primary key auto_increment,
    name varchar(16),
    phone int,
    author_detail_id int unique,
    foreign key(author_detail_id) references author_detail(id)
    on update cascade
    on delete cascade
)
insert into author_2(name,phone,author_detail_id) values('wuc',123,1);

聚合函數

分組之后頻繁需要使用的
	max		最大值
    min		最小值
    sum		求和
    count	計數
    avg		平均值

查詢關鍵字where

# 1.查詢id大於等於3小於等於6的數據
select *  from emp where id between 3 and 6;  

# 2.查詢薪資是20000或者18000或者17000的數據
select * from emp where salary in (20000,18000,17000);

# 3.查詢姓名中帶有字母o的員工姓名和薪資
select name,salary from emp where name like '%o%';

# 4.查詢姓名由四個字符組成的員工姓名和薪資
select name,salary from emp where name like '____';
select name,salary from emp where char_length(name) =4;

# 5.查詢id小於3或者大於6的數據
select *  from emp where id not between 3 and 6;

# 6.查詢薪資不在20000,18000,17000范圍的數據
select * from emp where salary not in (20000,18000,17000);

# 7.查詢崗位描述為空的員工名與崗位名  針對null不能用等號,只能用is
select name,post from emp where post_comment is NULL;

查詢關鍵字group by分組

分組
	將單個單個的個體按照指定的條件分成一個個整體
	
# 1.每個部門的最高薪資
select post,max(salary) from emp group by post;
# 2.每個部門的最低薪資
select post,min(salary) from emp group by post;
# 3.每個部門的平均薪資
select post,avg(salary) from emp group by post;
# 4.每個部門的人數
select post,count(id) from emp group by post;
# 5.每個部門的月工資總和
select post,sum(salary) from emp group by post;

# 別名
select post as '部門',sum(salary) as '總和' from emp group by post;

# 查詢分組之后的部門名稱和每個部門下所有的員工姓名
group_concat()  獲取分組以外的字段數據 並且支持拼接操作
select post,group_concat(name) from emp group by post;
select post,group_concat(name,':',salary) from emp group by post;

concat()  未分組之前使用的拼接功能
select concat(name,':',sex) from emp;

concat_ws()
select concat_ws(':',name,sex,salary,age) from emp;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM