表操作
表相當於一個文件,其形式與現實中的表格相同。表中的每條記錄都有相應的字段,字段就類似於表格的表頭。
表操作詳細:
#對表進行操作(文件) #首先要切換到指定庫(即文件夾)下: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__';
二、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;#按照崗位分組,並查看每個組有多少人
三、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