1.創建數據庫demo: create database demo;
2.創建表:
use demo;
CREATE TABLE user(
id int UNSIGNED auto_increment,
name VARCHAR(20) not null,
age int(3) UNSIGNED not NULL
)ENGINE=INNODB CHARSET=utf8;
這裏簡單說一下engine的概念,即存儲引擎,MySQL常用的存儲引擎有兩個,一個是Myisam,一個是innodb,區別在於前者不支持事物,速度快,後者反之。
3.修改表之增加列:
語法:
alter table tbName add 列名稱1 列類型 [列參數] [not null default ]
如修改user表增加列:
alter TABLE user change department department varchar(11);
4.修改表之修改列:
語法:
alter table tbName change 舊列名 新列名 列類型 [列參數] [not null default ]
如:修改user表的department字段改爲不可爲null:
alter TABLE user change department department varchar(11) not null;
5.修改表之減少列:
語法:
alter table tbName drop 列名稱;
如:刪除user表的department列字段:
ALTER TABLE user drop department
6.修改表之增加主鍵:
語法:
alter table tbName add primary key(主鍵所在列名);
如:將user表的id列設爲主鍵:
alter table user add PRIMARY key(id);
7:修改表之刪除主鍵:
alter table user drop primary key
8:修改表之增加索引:
語法
alter table tbName add [unique|fulltext] index 索引名(列名);
如:將user表的age字段增加索引:
alter table user add UNIQUE index age_index(age);
9.修改表之刪除索引
語法
alter table tbName drop index 索引名;
如:刪除剛剛創建的索引
ALTER TABLE USER DROP index age_index
10.清空表數據:
truncate user
11.增刪改查基本操作:
5.1 插入數據 insert into 表名(col1,col2,……) values(val1,val2……); -- 插入指定列 insert into 表名 values (,,,,); -- 插入所有列 insert into 表名 values -- 一次插入多行 (val1,val2……), (val1,val2……), (val1,val2……); 5.3修改數據 update tablename set col1=newval1, col2=newval2, ... ... colN=newvalN where 條件; 5.4,刪除數據 delete from tablenaeme where 條件; 5.5, select 查詢 (1) 條件查詢 where a. 條件表達式的意義,表達式爲真,則該行取出 b. 比較運算符 = ,!=,< > <= >= c. like , not like ('%'匹配任意多個字符,'_'匹配任意單個字符) in , not in , between and d. is null , is not null (2) 分組 group by 一般要配合5個聚合函數使用:max,min,sum,avg,count (3) 篩選 having (4) 排序 order by (5) 限制 limit
12.內連接與外連接:
先對數據庫demo做出如下修改:
create table class
(
id int(11) not null PRIMARY key auto_increment,
class_name VARCHAR(11) not null,
grade varchar(11) not null)ENGINE=INNODB CHARSET=utf8;
alter table user add PRIMARY key(id);
alter TABLE user add c_id int(11) not null;
創建新表class班級表,修改user表增加主鍵以及增加c_id列。
插入數據:
insert into user VALUES(1,'張三',11,1),(2,'李四',32,1),(3,'王五',24,2),(4,'趙流',45,3),(5,'小偉',33,5);
INSERT into class VALUES(1,'軟件一班','大一'),(2,'軟件二班','大二'),(3,'軟件三班','大一'),(4,'計算機網絡一班','大二')
12.1:內連接查詢:
語法:.... inner join ..... on ... ...
如:user表內連接class表,條件爲user.cid = class.id:
SELECT * from user inner join class on user.c_id = class.id
結果如下:
12.2:左連接:
同樣的查詢要求,如下:
SELECT * from user left join class on user.c_id = class.id
結果:
12.3:右連接:
如下:
SELECT * from user right join class on user.c_id = class.id
結果:
總結:內連接是根據條件查詢兩表的交集,即符合連接條件的結果保留,不符合的全部丟棄。左外連接查詢以左表爲基準,左表的數據保留,右表不符合連接條件的丟棄,而右外鏈接正好是相反的。
13.子查詢:
13.1:where型子查詢:即where條件後面跟的是一條查詢語句。
如:
SELECT * from user where c_id in (select id FROM class)
結果:
13.2:from型子查詢:內層查詢結果作爲一張表,以供外層sql進行再次查詢:
如:
SELECT class_name from (select * from class where id = 1) as temp
結果:
13.3:exists子查詢:後面的參數,可以是任意一個子查詢,這個子查詢的作用相當於測試,它不產生任何數據,只返回TRUE或FALSE ,當返回值爲TRUE時,外層的查詢纔會執行。
如:看一下結果返回false的情況:
select * from class where EXISTS (select * from class where id >7)
結果:
注意:外層的select * from class這條sql語句並沒有執行。
那麼,返回true的情況,只需要將上面的sql中id>7改爲大於2,即返回true執行查詢語句:
結果:
14.這裏記錄一下索引的相關概念:
索引的作用在於提高了查詢速度,但是降低了增刪改的速度,所以這裏要根據實際需求來使用,因此,我們常常將經常作爲查詢條件的列添加索引。值越分散的列,索引的效果越好。
索引類型:
primary key:主鍵索引。
index :普通索引
unique index :唯一性索引
fulltext index:全文索引(對中文無效)