MySQL---數據庫從入門走向大神系列--基礎入門

從最開始的創建數據庫,創建表,創建列開始寫起,再到常用的EXISTS函數,SELECT 複雜查詢,模糊查詢LIKE,創建視圖 等深入學習。
爲了對單詞加深印象,全部在DOS下演示!

創建數據庫、表

<span style="font-size:14px;">create database hncu character set utf8; </span>

創建名爲hncu編碼爲utf-8的數據庫。

<span style="font-size:14px;">use hncu; </span>
 打開hncu這個數據庫。(必須要打開一個數據庫才能在這個數據庫下面創建table哦)




創建表格stud
<span style="font-size:14px;"> create table stud(
 sno varchar(15) not null primary key,
 sname varchar(15) not null,
 age int,
 saddress varchar(15)
 );</span>




表格添加數據:
<span style="font-size:14px;">insert into stud values('1001','Jack',20,'紐約');
insert into stud values('1002','Tom',30,'紐約');
insert into stud values('1003','張三',24,'湖南益陽');
insert into stud values('1004','張四',15,'湖南長沙');
insert into stud values('1005','李四',22,'湖南益陽');
insert into stud values('1006','張三丰',80,'武俠');
insert into stud values('1007','郭襄',75,'武俠');
insert into stud values('1008','滅絕師太',10,'武俠');</span>


查看stud表的數據:

<span style="font-size:14px;">select * from stud;</span>



給列名取別名顯示:

<span style="font-size:14px;">select sno as 編號,sname as 姓名,age as 年齡, saddress as 地址 from stud;</span>




select 複雜查詢:

查詢stud表格中age大於等於24的:

<span style="font-size:14px;">select * from stud where age>=24;</span>




查詢stud表格中age大於等於20且下雨等於30的數據:

<span style="font-size:14px;">select * from stud where age>=20 and age <=30;</span>


還有一種方法:

<span style="font-size:14px;">select * from stud where age between 20 and 30;</span>




查詢年齡等於20或者年齡等於30的stud表中的數據:

select * from stud where age=20 or 30;


還有一種方法:用 in();

查詢年齡爲20,22和30的stud表中的數據:

<span style="font-size:14px;">select * from stud where age in(20,22,30);</span>


有一個和in相對的:not in

<span style="font-size:14px;">select * from stud where age not in(20,22,30);</span>



模糊查詢LIKE  '%'匹配所有  '_'匹配單字符  ---必須和LIKE共同使用:

也就是說通配符只能在有like的情況下使用,如果是和=一起使用,那就只是普通的字符了。

查詢名字是張開頭的:

select * from stud where sname like '張%';




查詢名字張開頭的,而且名字只有2個字符的:

<span style="font-size:14px;">select * from stud where sname like '張_';</span>



查詢名字張開頭的,而且名字只有3個字符的:

<span style="font-size:14px;">select * from stud where sname like '張__';</span>




查詢名字中帶有‘三’的:

<span style="font-size:14px;">select * fom stud where sname like '%三%';</span>




查詢名字中帶有‘三’的而且年齡大於30的:

<span style="font-size:14px;">select * from stud where sname like '%三%' and age >30;</span>




爲表格增加一列:

<span style="font-size:14px;">alter table stud add column sex char(1);</span>

省略column 也可以添加

<span style="font-size:14px;">alter table stud add sex char(1);</span>



從stud表格刪除sex列

<span style="font-size:14px;">alter table stud drop sex;</span>

也可以用:

<span style="font-size:14px;">alter table stud drop column sex;</span>

判斷NULL值時,不能用‘=’號判斷,而是用is:

險插入一行數據,讓他的age爲null;



<span style="font-size:14px;">update stud set age=20 where age=null;</span>
這一句是不起作用的,因爲這個無法用來判斷age是否爲null。

應該用下面這句:


<span style="font-size:14px;">elect stud set age=20 where age is null;</span>
作用是:如果stud表格中哪行的age爲null,就設置age爲20.



如果是判斷哪個爲空字符,就直接可以用=''  來判斷。

例:

<span style="font-size:14px;">select * from stud where saddress='';</span>

作用是:如果stud表中有saddress爲空(注意!是空,不是null),就查詢顯示出來。


將saddress爲紐約的改爲硅谷

<span style="font-size:14px;">update stud set saddress='硅谷' where saddress='紐約'; </span>

注意:不是:這裏不能寫成 update table stud set...;




同時修改多個字段的值:

<span style="font-size:14px;">update stud set sname='ROSE', saddress='北京' where sno='1002';</span>



刪除名字是悟空的行:

<span style="font-size:14px;">delete from stud where sname='悟空';</span>




知識點:

select 字段 from 表名 where 條件 and 條件 or 條件

update tableName set 需要設置的值 where 條件

delete from tableName where 條件


創建視圖:cerate view 視圖名 as select 子句

(虛表)---只存在內存中

create view aview as select * from stud where age>20;

從視圖aview中查詢年齡小於40的sname,age,ano:

<span style="font-size:14px;">select sname,sno,age from aview where age<40;</span>



聚合函數:


統計非null數據的行數:(*號和1 代表只要表中一行有非null的列數據,這一行就是非null)

一般要專門給個別用: as 別名

<span style="font-size:14px;">select count(*) from stud;
select count(1) from stud;</span>



統計age不爲空的行數:

也就是age爲null就不會被統計進去。

<span style="font-size:14px;">select count(age) from stud;</span>



顯示出stud表中所有age的平均值:

<span style="font-size:14px;">select avg(age) as averageAge from stud;</span>



顯示所有age平均值的四捨五入。

<span style="font-size:14px;">select round(avg(age)) as averageAge2 from stud;</span>

還有:

Sum求和。
Max求最大值,
Min求最小值。

<span style="font-size:14px;">select  sum(age)  as sunAge from stud;
select max(age) as maxAge from stud;
select min(age) as minAge from stud;</span>



選擇年齡最小的那個人的名字和年齡:

<span style="font-size:14px;">select sname , age from stud where age = ( selectt min(age) from stud );</span>
這樣用in也可以:

<span style="font-size:14px;">select sname ,age from stud where age in(select min(age) from stud);</span>


再創建一個年齡等於10的行:

<span style="font-size:14px;">insert into stud value('1009','李白',10,'湖南');</span>
再查年齡最小的那個人的年齡:

<span style="font-size:14px;">select age from stud where age=(select min(age) from stud);</span>


我們可以看到,因爲有2個數據的年齡都是最小值,所有顯示了2行,但是它們是重複的,完全沒必要顯示2行。

這個時候我們就要用到:distinct ,把完全相同的行,合併顯示!


<span style="font-size:14px;">select distinct age from stud where age = (select min(age) from stud);</span>




排序-升序和降序:

按年齡升序排:

<span style="font-size:14px;">select * from stud order by age asc;</span>


按年齡降序排:

<span style="font-size:14px;">select sno,sname,age from stud order by age desc;</span>



exists存在判斷

<span style="font-size:14px;">select sname,age from stud where exists (select * from stud where age = 20);</span>

exists (select * from stud where age=20) ---只要存在age=20的,就返回true、

也就是exists(...) 是判斷括號內的表達式是不是null的,如果是null則返回false,否則返回true;

此句因爲stud存在age=20的行,所以會輸出所有的sname,age。




分組  group by 

<span style="font-size:14px;">select saddress , avg(age) as 平均年齡 from stud group by saddress;</span>

按照saddress來分組,求出每組的平均年齡。

只要saddress不同就是不同的組!




按照saddress分組後每組的年齡總和:

select saddress,sum(age) as 年齡總和 from stud group by saddress;



有2個固定搭配:

排序:

select ... from ... where ... order by ...  

分組:

select ... from ... group by ... by ... having ... (條件判斷在having後面,不是用where)


這裏的sum(age)也可以用as 別名  取一個別用,在判斷的時候直接可以用別名的。



字符串處理函數

<pre name="code" class="sql"><span style="font-size:14px;">Length(str) - 求字符串長度
Ltrim(str) - 去掉左邊的空格
Rtrim(str) - 去掉右邊的空格
trim(str)  - 去掉兩邊的空格
Left(str,n); - 從左邊取出n個字符
Right(str,n); - 從右邊取出n個字符
Substring(str,begin,end) -返回子串
Reverse(str) –返回顛倒的字符串
Lower(str) - 轉成小寫
Upper(str) - 轉成大寫
Concat(Str,str…..)串聯字符串。
Instr(str,s) – 返回s在str中出面的位置,沒有則返回0</span>


這裏就只選取幾個來演示了:

演示left();

顯示saddress開始2個字符爲湖南的行

<span style="font-size:14px;">select * from stud where left(saddress,2)='湖南';</span>



串聯字符串:

<span style="font-size:14px;">select concat(snon,sname,saddress) as 串聯字符串 from stud;</span>



instr(str,s) 返回s在str中出面的位置,沒有則返回0

其實就是返回字串自一次出現的位置(從1開始計數)

select sname,instr(sname,'三') as ind from stud;'

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章