2018/12/6 星期四 19:34:07 authot by dabaine 資料庫註釋; 這就是註釋 / ..... / 這也是註釋 創建庫; create databse [if not exists] dabaine [character set "utf8"]; 查看所有資料庫; sho ...
2018/12/6 星期四 19:34:07
authot by dabaine
資料庫註釋;
-- 這就是註釋
/*.....*/ 這也是註釋
創建庫;
create databse [if not exists] dabaine [character set "utf8"];
查看所有資料庫;
show databses;
查看資料庫結構:
show create database dabaine;
查看當前資料庫;
select database();
修改資料庫;
alter database dabaine [character set "gbk"];
刪除資料庫;
drop database [if exists] dabaine;
使用資料庫;
use database;
創建表;
create table dabaine(
id smallint(10) primary key not null auto_increment,
name varchar(25) not null,
gender boolean not null
);
刪除表;
drop table dabaine;
查看表結構;
eg1:show create table dabaine;
eg2:show columns from dabaine;
查看表的全部信息;
desc dabaine;
修改表結構;
增加欄位:
alter table dabaine add [column],add [column]......;
修改類型:
alter table dabaine modify colum_name attribute [first|after column_name] colum_name;
修改列名:
alter table dabaine change column_name new_column_name type [約束條件];
刪除欄位:
alter table dabaine drop [column];
重命名:
rename table table_name to new_table_name;
修改表內容;
插入:
eg1:insert into dabaine (id, name) values(1,"dabaine");
eg2:insert into dabaine set id = 2,name="dabaine";
更新:
update dabaine set name="cody" where name="dabaine";
刪除:
eg1:delete from dabaine where name = "cody";
eg2:truncate table dabaine; --把表摧毀,重新創建一張新表;
查詢順序;
select [distinct] *|field ... from dabaine
where (不分組篩選)
group by field
having (分組後篩選)
order by field
limit
查詢別名;
selct distinct id + 10 as id from dabaine;
執行順序;
from,where,select,group by,having, order by
聚合函數;
select name, sum(grade) from dabaine group by name;
ifnull(grade,0) --如果grade為空,則給它定為0;
外鍵約束;
創建主表:
create table class(
id int(10) primary key auto_increment,
name varchar(20),
age int(5)
);
主表添加數據(多條):
insert into class(name,age) values
("cody",18),
("solider",19),
("guan",21),
("lee",22),
("strong",28),
("pig",38);
創建子表:
create table student(
id int(10) primary key auto_increment,
name varchar(20),
age int(5),
teacher_id int(10), --綁定外鍵的欄位要和主表中的欄位類型保持一致;
constraint dabaine --給外鍵命名大白訥
foreign key (teacher_id) --給子表的屬性選擇外鍵綁定
references class(id) --映射主表的屬性(追隨主表的id欄位)
);
子表添加數據:
insert into student(name,age,teacher_id) values
("cody",18,1),
("solider",19,2),
("guan",21,3),
("lee",22,4),
("strong",28,5),
("pig",38,6);
這時,主表和子表已經有關聯了,不可以隨便刪除主表的記錄;
增加外鍵:
alter table son_table_name add constraint cody
foreign key(son_table_field)
references primary_table(field);
刪除外鍵:
alter table son_table_name drop foreign key cody;
級聯刪除(cascade);
create table studentNew(
id int(10) primary key auto_increment,
name varchar(20),
age int(5),
teacher_id int(10),
constraint cody foreign key (teacher_id)
references class(id)
on delete cascade --級聯刪除
);
constraint cody foreign key (teacher_id)
references class(id)
on delete set null --主表刪除後,子表記錄設置為空值,且子表的欄位屬性不能設置為not null;
on delete restrict --拒絕對主表進行更新刪除操作;
on delete no action --類似於restrict
多表查詢;
笛卡爾積連接:
A表中的全部數據m條 * B表中的全部數據n條;
連接查詢~內連接:
inner join
eg1:select tableA.id,tableA.name,tableB.name from
tableA,tableB where tableA.id = tableB.tableA_id
eg2:select tableA.id,tableA.name,tableB.name from tableA
inner join tableB on tableA.id = tableB.tableA_id
+---------+----+---------+
| name | id | name |
+---------+----+---------+
| cody | 1 | cody |
| solider | 2 | solider |
| guan | 3 | guan |
| cody | 4 | lee |
| strong | 5 | strong |
| lee | 6 | pig |
+---------+----+---------+
連接查詢~左外連接(左連接):
left join
select tableA.id,tableA.name,tableB.name from tableA
left join tableB on tableA.id = tableB.tableA_id
--左連接以左表為主,select所選擇的欄位,左表中的記錄會全部顯示,而右表會去匹配左表裡的記錄,沒有的則顯示空值;
+----+---------+---------+
| id | name | name |
+----+---------+---------+
| 1 | cody | cody |
| 2 | solider | solider |
| 3 | guan | guan |
| 4 | lee | cody |
| 5 | strong | strong |
| 6 | pig | lee |
+----+---------+---------+
連接查詢~右外連接(右連接):
right join
類似左連接,以右表為主;
+------+---------+---------+
| id | name | name |
+------+---------+---------+
| 1 | cody | cody |
| 4 | lee | cody |
| 2 | solider | solider |
| 3 | guan | guan |
| 6 | pig | lee |
| 5 | strong | strong |
| NULL | NULL | pig |
+------+---------+---------+
嵌套;
查詢嵌套:
select * from table_name where field in (select field from table_name);
複製表:
create table new_table(select * from old_table); --原表中的約束不會複製過來,需要重新添加
selcet * from table_name where exists
(selcet field from table_name where....)
--exists 後面的語句會返回一個布爾值,true則執行前面的select語句,
flase 則返回空值;
索引;
unique(唯一索引),fulltext(全局索引),spatial(空間索引),index|key(普通索引)
添加索引:
eg1:create
[unique|fulltext|spatial] index|key
index_name on table_name (欄位名[(長度)] [asc|desc]);
eg2:alter table table_name
add [unique|fulltext|spatial] index|key index_name (欄位名[(長度)] [asc|desc]);
刪除索引:
drop index index_name on table_name;
unique:唯一索引的欄位不能重覆;
多列索引:給多個欄位添加索引 (field1,field2...)
事務;
start transaction; --開啟事務
Rollback; --回滾事務(撤銷)
Commit; --提交事務;
savepoint; 保留點,事務處理中的臨時占位符;
savepoint name;
rollback to svaepoint_name;
存儲過程;