表操作
表相當於一個檔案,其形式與現實中的表格相同。表中的每條記錄都有相應的欄位,欄位就類似於表格的表頭。
表操作詳細:
#對錶進行操作(檔案)
#首先要切換到指定庫(即資料夾)下:use db1;
#1、增:
create table t1(id int,name char(10));#新建一個表,表中的欄位必須表上資料型別,char表示字串
create table t1(id int,name char(10))engine=innodb,default charset gbk;
#新建一個表,可以指定引擎,不指定的話預設的引擎就是innodb,default是指定其字元編碼,若不指定預設為該庫的字元編碼 #2、新增欄位
alter table t1 add age int(3) not null default 22;#新增一個不能為空的age欄位,預設值22
alter table t1 add number int(5) not null after name;#在name欄位後新增一個number欄位
alter table t1 add sex enum('male','female') default 'male' first;#在最前面新增列舉 #3、修改欄位:
alter table t1 modify name char(12);#修改name資料型別的位元組數為12 #4、增加約束
alter table t1 modify id int not null primary key;#設定為主鍵
alter table t1 modify id int not null auto_increment;#設定為自增,與上句連用可設定為自增主鍵
#alter table t1 add primary key(name,sex);#增加複合主鍵 #5、刪除主鍵
alter table t1 drop primary key;#因為主鍵唯一,所以無需指定 #6、刪除自增約束
alter table t1 modify id int not null; #7、刪除欄位
alter table t1 drop age;#刪除age欄位 #8、刪除表:
drop table t1; #9、複製表
create table t2 select * from t1 where 1=2;#只拷貝表結構,不拷貝表內容
alter table t2 modify id int primary key auto_increment;#將表修改成自增id #查:
show tables;#檢視所有表
show create table t1;#檢視建立的表
desc t1;#查看錶結構,比show好用
資料操作
資料操作是資料庫知識裡的重中之重,尤其是多表的關聯查詢操作!
資料操作分為資料的增、刪、改、查,其中最常用的是查詢操作。先來看比較簡單的增刪改。
一、增
#增資料的兩種形式(增即向表中插入,所以用insert形象的表達)
insert into db1.t1 values(1,'egon1'),(2,'egon2');#按位置傳參,可一次性插多條資料
insert into db1.t1 (name) values('egon'),('alex');#指定只傳name,id預設為null
二、刪
delete from t1 where id=4;#刪除指定欄位
三、改
update t1 set name='SB' where id=4;#將id為4的欄位的name修改為SB
update t1 set name='SB' where name='alex';#將name為alex的欄位的name修改為SB
update t1 set name='';#將name的值都修改為空,即清空所有name的內容
四、查
查詢分為單表查詢和多表查詢,先介紹單表查詢和查詢關鍵字。
首先我們要知道查詢的語法
select 欄位 from 表名 [條件]
簡單查詢(單表查詢)
首先建立一張表
create table t1(
id int not null unique auto_increment,
name varchar(20) not null,
sex enum('male','female') not null default 'male', #大部分是男的
age int(3) unsigned not null default 28,
hire_date date not null,
post varchar(50),
post_comment varchar(100),
salary double(15,2),
office int, #一個部門一個屋子
depart_id int
);
查詢語句
select * from t1;
select id,name,age from t1;
select distinct post from t1;#去重
select name,age*5 from t1;
select name,age as annual_age from t1;#將查詢後所得的表以annual_age 為表頭
select concat('姓名':name,'薪資':salary) from t1;#將2項整合輸出
select dep_id,group_concat(name) from t1 group by dep_id;#分組顯示
select dep_id,count(id) from t1 group by dep_id;#分組顯示總數
select dep_id,max(age) from t1 group by dep_id;#分組後顯示最大值
select dep_id,avg(age) from t1 group by dep_id;#分組後顯示最小值
查條件的關鍵字及優先順序
from #定位表
where #按條件進行過濾
group by #按條件將結果進行分組(如果有聚合函式則再進行聚合),與group_cincat()可一起使用
having #再次過濾,在聚合後進行
select #查,得到結果
distinct #去重
order by #按條件排序(升序asc、降序desc)
limit #限制顯示條數(limit x,y#從第x開始往後顯示y條,x不寫時預設為0)
一、where
where是最基本的條件篩選,條件中可以用比較運算子、邏輯運算子、between、in、like
#1:單條件查詢
SELECT name FROM employee
WHERE post='sale'; #2:多條件查詢
SELECT name,salary FROM employee
WHERE post='teacher' AND salary>10000; #3:關鍵字BETWEEN AND
SELECT name,salary FROM employee
WHERE salary BETWEEN 10000 AND 20000; SELECT name,salary FROM employee
WHERE salary NOT BETWEEN 10000 AND 20000; #4:關鍵字IS NULL(判斷某個欄位是否為NULL不能用等號,需要用IS)
SELECT name,post_comment FROM employee
WHERE post_comment IS NULL; SELECT name,post_comment FROM employee
WHERE post_comment IS NOT NULL; SELECT name,post_comment FROM employee
WHERE post_comment=''; 注意''是空字串,不是null
ps:
執行
update employee set post_comment='' where id=2;
再用上條檢視,就會有結果了 #5:關鍵字IN集合查詢
SELECT name,salary FROM employee
WHERE salary=3000 OR salary=3500 OR salary=4000 OR salary=9000 ; SELECT name,salary FROM employee
WHERE salary IN (3000,3500,4000,9000) ; SELECT name,salary FROM employee
WHERE salary NOT IN (3000,3500,4000,9000) ; #6:關鍵字LIKE模糊查詢
萬用字元’%’
SELECT * FROM employee
WHERE name LIKE 'eg%'; 萬用字元’_’
SELECT * FROM employee
WHERE name LIKE 'al__';
where具體用法
二、group by
group by 是用來分組的,分組就是將所有記錄按照某個相同欄位進行歸類,分組的依據一般選擇欄位值相同率較高的欄位,需要注意的是分組發生在where之後
group by通常與聚合函式通用,如數量count(),最大值max(),最小值min(),平均值avg(),求和sum()
#單獨使用GROUP BY關鍵字分組
SELECT post FROM employee GROUP BY post;
#注意:我們按照post欄位分組,那麼select查詢的欄位只能是post,想要獲取組內的其他相關資訊,需要藉助函式 #GROUP BY關鍵字和GROUP_CONCAT()函式一起使用
SELECT post,GROUP_CONCAT(name) FROM employee GROUP BY post;#按照崗位分組,並檢視組內成員名
SELECT post,GROUP_CONCAT(name) as emp_members FROM employee GROUP BY post; #GROUP BY與聚合函式一起使用
select post,count(id) as count from employee group by post;#按照崗位分組,並檢視每個組有多少人
group by 用法
三、having
having為分組後的過濾,與where不同的是having只能發生在group by 之後,可以理解為分組完之後的過濾
select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary) <20000;
四、正則表示式
在SQL查詢中也支援正則表達,其作用域like相似都是用於模糊匹配,不過正則更細緻一些,like則要與%同用
SELECT * FROM employee WHERE name REGEXP '^ale'; SELECT * FROM employee WHERE name REGEXP 'on$'; SELECT * FROM employee WHERE name REGEXP 'm{2}';
進階——多表查詢
多表查詢篇幅較長,故單獨成篇。地址:http://www.cnblogs.com/zhuminghui/p/8352571.html