
mysql數(shù)據(jù)庫基礎知識總結(jié)
一、基礎常用命令
1.創(chuàng)建命令
create user ‘用戶名’ @‘ip’ identified by ‘密碼’; 創(chuàng)建數(shù)據(jù)庫用戶
drop user 用戶名@ip 刪除用戶
show databases; 查數(shù)據(jù)庫
show tables; 看表
create database 數(shù)據(jù)庫名 default charset utf8; 創(chuàng)建數(shù)據(jù)庫
create table 表名(列名 數(shù)據(jù)類型 約束···,列名 數(shù)據(jù)類型 約束···)engine=innodb default charset=utf8 創(chuàng)建表
其中數(shù)據(jù)類型種類 數(shù)字(int,tinyint,smallint,float,double),字符串(char(個數(shù))varchar(個數(shù)))時間(DATE,TIME,DATETIME),枚舉enum(值只能是枚舉中的元素),集合set(值只能是結(jié)合元素的組合)
2.查表命令
條件查詢
select * from 表名;
select 列名···from 表名;
select 列名 from 表名 where 列名(id等) >/</!= value;
select 列名,常量 from 表名; 增加一個常量列
select 列名 from 表名 where 列名 in/not in/between and value;
select 列名 from 表名 where 條件1 and 條件2;
特殊的:select 列名 from 表名 where 列名 in (select 列名(只能一列) from 表名);
select 列名 from 表名 where 列名 like ‘xx%’/’%xx’/“xx_”; 查詢以xx開頭/xx結(jié)尾 ,%代表任意位,_代表一位
select xx,(select xx from …) from … 查詢條件做常量值
分頁
select 列名 from 表名 limit num; 顯示num個
select 列名 from 表名 limit num1,num2;從num1后取num2行數(shù)據(jù),num1是起始位置,num2是個數(shù)
select 列名 from 表名 limit num1 offset num2;從num2后取num1行數(shù)據(jù),num2是起始位置,num1是個數(shù)
排序
select * from 表名 order by 列名 asc;升序
select * from 表名 order by 列名 desc;降序
select * from 表名 order by 列名1 desc 列名2 asc; 首先遵循列1降序,遇到相同數(shù)據(jù)時,升序
select count/sum/max/min/avg(列名1),列名2 from 表名 group by 列名(通常是列名2);
分組
select count/sum/max/min/avg(列名1),列名2 from 表名 group by 列名(通常是列名2)having 條件;分組操作后篩選
連表
select * from 表1 left join 表2 on 表1.列名=表2.列名; 左連接
select * from 表1 right join 表2 on 表1.列名=表2.列名; 右連接
select * from 表1 inner join 表2 on 表1.列名=表2.列名; 內(nèi)連接
注意:如果超過3個表聯(lián)合操作,如果其中兩個表操作時已經(jīng)改變了表結(jié)構(gòu),應該將這兩個表操作的結(jié)果作為一個臨時表再與第三個表聯(lián)合操作。
3.刪除,修改,插入命令
插入
insert into 表名(列名1,列名2···) values(值1,值2···),(值1,值2···),(值1,值2···); 插入值
insert into 表名1(列名) select 列名 from 表2; 在表1中插入表2中數(shù)據(jù)
修改
update 表名 set 列名1=value1,列名2=value2 where 條件1 [and/or 條件2];
刪除
delete from 表名; 清除表(如果有自增id,id 不會重新開始)
delete from 表名 where 條件; 清除特定數(shù)據(jù)
truncate table 表名;清除表(如果有自增id,id 會重新開始)
4.修改表結(jié)構(gòu)
alter table 表名 auto_increment=value;設置自增鍵起始值;
alter table 表名 drop 列名;刪除列
alter table 表名 add 列名 數(shù)據(jù)類型 約束; 增加列
alter table 表名 change 舊列名 新列名 數(shù)據(jù)類型; 修改字段類型
alter table 表名 modify 列名 數(shù)據(jù)類型; 修改數(shù)據(jù)類型
alter table 舊表名 rename 新表名; 修改表名
alter table 表名 drop primary key; 刪除表中主鍵
alter table 表名 add 列名 數(shù)據(jù)類型 primary key;添加主鍵
alter table 表名 add primary key(列名);設置主鍵
alter table 表名 add column 列名 數(shù)據(jù)類型 after 列名;在某一列后添加主鍵
二.mysql進階
1.外鍵
外鍵適用于一對多,一對一,多對多三種情況
一對多
典型案例員工與部門,一個部門對應于多個員工,一個員工對應于一個部分,所以要在員工表中設置部門id列,并設置為外鍵,與部門表id關(guān)聯(lián)。
一對一
案例博客園用戶與博客,不是每個用戶都寫博客,寫博客的用戶與擁有的博客地址一一對應,所以在博客用戶表user中設置blog_id,設置成外鍵和唯一索引,與博客表blog中id關(guān)聯(lián)
create table user(id int not null auto_increment primary key,name char(10), blog_id int,unique uq1(blog_id),constraint fk_user_blog foreign key(blog_id) references blog(id))engine=innodb default charset=utf8;
多對多
典型案例電腦與用戶,一個用戶可以使用多臺電腦,一臺電腦對應多個用戶,多對多,此時一般選擇新建一個表contact,在其中設置兩個外鍵,同時關(guān)聯(lián)用戶表id與電腦表id
create table contact(id int not null auto_increment primary key,user_id int,computer_id int,unique uq2(user_id,computer_id),constraint fk_user foreign key(user_id) references user(id),constraint fk_user foreign key(computer_id) references computer(id))engine=innodb default charset=utf8;
可以看情況決定要不要把兩個外鍵弄成聯(lián)合唯一索引。