Mysql 基礎入門知識查詢

MYSQL基本知識

查看錶結構

describe(desc)表名;

show create table 表名;

修改表

alter table 舊錶名 rename 新表名;  //修改表名

alert table 表名 change 舊屬性 新屬性名 行數據類型;  //修改字段

alert table 表名 add 屬性名 數據類型 [完整性約束] [first | after 屬性2];  //增加字段

alert table 表名 drop 屬性名字; //刪除字段

刪除表

DROP table 表名;

查詢

select * from t_student where age>22;

select * from t_student where age (not)in (21,30);

select * from t_student where age (not)between 20 and 24;

select * from t_student where studName like '張三';

select * from t_student where studName like '張三%';

select * from t_student where studName like '張三_';

%代表任意字符,_代表單個字符

select * from t_student where studName IS(NOT) NULL;

select * from t_student where age>22 and name='張三';

select * from t_student where age>22 or name='張三';
去除重複項
select DISTINCT gardeName from t_student;
排序
select * from t_student ORDER BY age ASC; //升序

select * from t_student ORDER BY age DESC; //降序
分組查詢

和GROUP_CONCAT()函數使用

select gradeName,GROUP_CONCAT(stuName) from t_student GROUP BY gradeName; 

與聚合函數一起使用

select gradeName,COUNT(stuName) FROM t_student GROUP BY gradeName;

與HAVING語句一起使用

select gradeName,COUNT(stuName) FROM t_student GROUP BY gradeName HAVING COUNT(stuName)>3;

WITH ROLLUP(總計)

select gradeName,COUNT(stuName) FROM t_student GROUP BY gradeName HAVING COUNT(stuName)>3 WITH ROLLUP
分頁查詢
select * from t_student LIMIT 0,5; //頁只顯示前5條

select * from t_student LIMIT 5,5; //頁顯示後5條

select * from t_student LIMIT 10,5;

使用聚合函數查詢

統計函數
select COUNT(*) as total from t_grade;
select stuName,COUNT(*) from t_grade GROUP BY stuName;
求和函數
select stuName,SUM(score) from t_grade where stuName="張三";

select stuName,SUM(score) from t_grade GROUP BY stuName;
求平均值
select stuName,AVG(score) FROM t_grade GROUP BY stuName;
求最大值
select stuName,couse,MAX(score) FROM t_grade where stuName='張三';

select stuName,MAX(score) FROM t_grade GROUP BY	 stuName;
求最小值
select stuName,couse,MIN(score) FROM t_grade where stuName='張三';

select stuName,MIN(score) FROM t_grade GROUP BY	 stuName;

連接查詢

內連接查詢
select * from t_book,t_bookType where t_book.bookTypeId=t_bookType.id ;
使用別名
select tb.bookName,tb.author,tby.bookTypeName FROM t_book tb,t_bookType tby where tb.bookTypeId=tby.id;
left join(左聯接):

返回包括左表中的所有記錄和右表中聯結字段相等的記錄

select * from t_book LEFT JOIN 	t_bookType ON t_book. bookTypeId=t_bookType.id;

right join(右聯接): 返回包括右表中的所有記錄和左表中聯結字段相等的記錄。

select * from t_book RIGHT JOIN t_bookType ON t_book. bookTypeId=t_bookType.id;

子查詢

in 語句
select * from t_book where booktypeId in (select id from t_booktype);

select * from t_book where booktypeId in (select id from t_booktype);
>= (後面子查詢是一個數據)
select * from t_book where price >=(select pricelevel where priceLevel=1)
exists語句
select * from t_book where EXISTS (select * from t_booktype);

select * from t_book where NOT EXISTS (select * from t_booktype);
ANY ALL(後面的子查詢是一個集合)
select * FROM t_book where price >=ANY (select price from t_pricelevel) ;//符合一個就刷選

select * FROM t_book where price >=ANY (select price from t_pricelevel) ;//符合全部才刷選

合併查詢

select id from t_book UNION select id from t_booktype;
select id from t_book ALL UNION select id from t_booktype; //不去掉重複的

插入,修改,刪除數據

插入所有字段數據
insert into t_book values(NULL,"sadas",20,1);
插入指定的數據
insert into t_book(bookname,author) VALUES('我愛我家','張三')
插入多條數據
insert into t_book values(NULL,"sadas",20,1),(NULL,"sadas",20,1);
更新語句
UPDATE t_book set bookName='java編程',price=120 where id=1;
更新多條
update t_book set bookname='我' where bookName LIKE '%我是%';
刪除數據
delete from t_book where id=5;

索引

創建表時創建索引

普通的單列索引
create table t_user1(id int,userName varchar(20),
password varchar(20),
index (userName)
);
唯一的單列索引
create table t_user1(id int,userName varchar(20),
password varchar(20),
unique index index_userName(userName)
);
普通的多列索引
create table t_user1(id int,userName varchar(20),
password varchar(20),
index index_userName_password(userName,password)
);

創建好表後創建索引

create INDEX index_userName ON t_user4(userName);
create UNIQUE INDEX index_userName ON t_user4(userName);
create INDEX index_userName_password ON t_user(username,password);

ALTER table t_user5 ADD INDEX index_userName(userName);
ALTER table t_user5 ADD UNQIE INDEX index_userName(userName);	
ALTER table t_user5 ADD INDEX index_userName(userName,password);	

刪除索引

DROP INDEX 索引名 on 表名;

視圖

創建單表視圖
create VIEW v1 AS select * from t_book

create VIEW v3(b,p) as select bookname,price from t_book
創建多表視圖
create VIEW v5 as select tb.bookName,tby.booktypeName from t_book tb,t_booktype tby where tb.bookTypeId=tby.id
查看視圖
desc V5

show table status like 'v5';
	
show table status like 't_book';

show create view v5 
修改視圖
select * from v1;
CREATE OR REPLACE VIEW v1(bookName,price)AS select bookName,price from t_book;
alter view v1 as select * from t_book;
視圖的更新
INSERT INTO v1 values(null,'java good',120,'feng',1);

UPDATE v1 set bookName='java very good',price=200 where id=5;

delete from v1 where id=5;
刪除視圖
drop view if extsts v4;

觸發器

創建單語句觸發器
create trigger trig_book after insert
    on t_book for each row
		update t_bookType set bookName=bookNum+1 where new.bookTypeId=t_booktype.id;

insert into t_book values(NULL,'java好',100,'ka',1);
創建多語句觸發器
DELIMITER |
create trigger trig_book2 after delete
	on t_book for each row
	begin 
		update t_bookType set bookNum=bookNum=1 where old.bookTypeId=t_booktype.id;
		insert into t_log values(Null,Now(),'在book表刪除一個數據');
		delete from t_test where old.bookTypeId=t_test.id;
	end
|
DELIMITER;

delete from t_book where id=5;
查看觸發器
show triggers;
刪除觸發器
drop trigger trig_book;

加密函數

不可解密(就是獲取數據庫也不能知道密碼)
INSERT INTO test VALUES(NULL,'xiaozhu',PASSWORD('1314520'))

INSERT INTO test VALUES(NULL,'xiaozhu',MD5('1314520'))
可解密(可以恢復成原來用戶設置的密碼)
INSERT INTO test VALUES(NULL,'xiaozhu2',ENCODE('1314520','key'))

SELECT DECODE(PASSWORD,'key') FROM test WHERE id=9;

存儲過程

變量

declare a,b varchar(20);//聲明
set a='aavvva';//賦值
select userName,password into a,b from t_user where id=1; //賦值,值在表中拿出來

例子

DELTMITER &&
create procedure pro_book (IN bt int,OUT count_num INT)
READS SQL DATA 
BEGIN
	SELECT conut(*) from t_book where bookTypeId=bt;
END
&&
DELTMITER;

CALL pro_book(1,@tatal);//調用

if語句

DELTMITER &&
create procedure pro_user5 (IN bookId INT)
	begin
		select count(*) into @num from t_user2 where id2=bookId;
		IF @num>0 then update set userName='java12345'
			where id=bookId;
		else
			insert into t_user values(null,'231222','122132');
		end if;
	end
&&
DELTMITER;

call pro_user5 (5);//調用

case語句

DELTMITER &&
create procedure pro_user6 (IN bookId INT)
begin
	select count(*) into @num from t_user2 where id2=bookId;	
	case @num	
	WHEN 1 THEN update t_user set userName='java12345' where id=bookId;
	WHEN 2 THEN insert into t_user values(null,'2222','2222');
	ELSE insert into t_user values(null,'222','1111');
    END CASE;
end
&&
DELTMITER;

call pro_user6 (2);//調用

loop語句

DELTMITER &&
create procedure pro_user7 (IN totalNum INT)
begin
	aaa:LOOP
		set totalNum=totalNum-1;
		if totalNum=0 THEN LEAVE aaa;
		else insert into t_user values (totalNum,'12354','255654');
		end if;
	END	
&&
DELTMITER;
	
call pro_user6 (5);//調用

ITERATE

跳出循環,繼續下一個循環,相當於continue
ITERATE aaa

refeat

DELTMITER &&	
create procedure pro_user8 (IN totalNum INT)
	begin
		refeat
			set totalNum=total-1;
			insert into t_user values(totalNum,'1323','22555');
		end refeat
	end
&&
DELTMITER;

call pro_user8 (5); //調用 

while語句

DELTMITER &&	
create procedure pro_user10(IN totalNum INT)
	begin
		WHILE totalNum>0 DO
			insert into t_user values(totalNum,'223565','225465');
			set totalNum=totalNum-1;
		end while;
	end
&&
DELTMITER;

call pro_user10(11) //調用

存儲函數

DELTMITER &&
create function func_book(bookId int)
reture varchar(20)
BEGIN
	RETURE(SELECT bookName from t_book where id=bookId);
END
DELTMITER;

select func_book(1);//函數調用

查看存儲過程和存儲函數

查看狀態

show procedure status like 'pro_book';

查看定義

show create procedure pro_book;

修改存儲過程

alter procedure pro_book comment '我來測試一個comment';

刪除存儲過程

drop procedure pro_user10;

數據得備份和還原

數據的備份

bin目錄下用cmd打開mysqldump
mysqldump -u root -p db-book > d:\db_book.sql

數據還原

發佈了35 篇原創文章 · 獲贊 7 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章