mysql簡單命令操作
create database testDataBase;
建立資料庫(注意要以;結尾)
show databases;
檢視所有的資料庫
use test;
選擇test資料庫
drop database test;
刪除test資料庫
show engines;
檢視mysql支援的引擎
show tables;
檢視資料庫中所有的表
describe t_test;
檢視t_test表的結構(desc t_test;簡寫形式);
show create table t_test;
查看錶的詳細定義
drop table t_test;
刪除t_test表
alter table t_test rename t_table;
把t_test表重新命名為t_table表
alter table t_test add loving char(20);
給t_test表新增一個loving欄位,資料型別和長度為char(20)
alter table t_test add friends char(125) first;
在表的第一個欄位新增friends列;
alter table t_test add trouble char(125) after name;
在指定欄位後面新增欄位
alter table t_test drop trouble;
刪除表字段
alter table t_test modify name integer;
修改name欄位型別為integer型別
alter table t_test change name newname integer;
修改欄位name列名為newname,資料型別保持integer不變(可以同時把資料型別改變,實現列名和資料型別同時改變);
alter table t_test modify newname int(11) after gender;
修改列的位置,把newname列放到gender列之後;
create table t_test (
id integer ,
name char (20),
gender char(3),
constraint pk_name primary key(name) //新增主鍵約束,設定主鍵
);
create table t_test (
id integer primary key auto_increment , //設定主鍵以及自動增長;
name char(20),
gender char(3));
create table t_test ( id integer primary key auto_increment, name char(20) not null ,age integer unique );
設定主鍵自動增長,不為空和唯一約束
show tables;
檢視所有的表
delete from t_stu where stuid=1;
刪除一行資料
insert into t_stu values (1,’sdongwan’,1,1);
新增一行資料
update t_stu set cname=’sdongwan’ where cid = 1;
更新一行資料
select * from t_stu where stuid=1;
查詢一行資料
create table t_test ( id integer primary key auto_increment , brith datetime not null , age enum(‘18’,’19’,’20’) );
列舉欄位的使用
select id , id*2 ,id*4 from t_test;
mysql算術運算子
select name from t_test where name like ‘sd%’;
like粗略選擇
select abs(-2) ,abs(-55);
使用mysql內建的函式
select encode(‘kill’,’doublekill’);
使用doublekill作為密碼對kill進行加密
select decode(encode(‘kill’,’doublekill’),’doublekill’);
使用doublekill作為密碼對kill進行解密
select * from t_test where id in (4,5) order by id;
查詢id為4或者5的行
新增外來鍵約束:
外來鍵所在的表是子表,外來鍵參考的列所在的表是父表;
在插入資料時,要先插入父表資料,才能插入子表資料,否則直接插入子表資料會不成功;
alter table t_stu add constraint FK_CID foreign key(cid) references t_classroom(cid);
新增外來鍵約束,外來鍵約束名字為FK_CID ,外來鍵列cid, 參考列t_classroom(cid);
alter table t_stu drop foreign key FK_CID;
刪除外來鍵名為FK_CID的外來鍵約束
alter table t_stu add constraint PK_CID foreign key(cid) references t_classroom(cid) on delete cascade on update restrict;
新增外來鍵約束,並且設定刪除和更新的方式
CASCADE
在父表上update/delete記錄時,同步update/delete掉子表的匹配記錄SET NULL
在父表上update/delete記錄時,將子表上匹配記錄的列設為null (要注意子表的外來鍵列不能為not null)NO ACTION
如果子表中有匹配的記錄,則不允許對父表對應候選鍵進行update/delete操作RESTRICT
同no action, 都是立即檢查外來鍵約束SET NULL
父表有變更時,子表將外來鍵列設定成一個預設的值 但Innodb不能識別
連線
內連線(兩個表有同名的欄位):
select stuid,name,gender,t_stu.cid,cname from t_stu inner join t_classroom ;
查詢兩個表內連線後欄位值
select stuid,name,gender,t_stu.cid,cname from t_stu inner join t_classroom where t_stu.cid=t_classroom.cid;
跟上面的sql語句一樣
左連線的結果集包括指定的左表全部資料與匹配的右表資料,右表中沒匹配的全為空值.關鍵字 left join
select stuid,name,gender,t_stu.cid,cname from t_stu left join t_classroom on t_stu.cid=t_classroom.cid;
表t_stu左連線t_classroom;
結果:
+——-+———-+——–+—–+———-+
| stuid | name | gender | cid | cname |
+——-+———-+——–+—–+———-+
| 1 | zbo | boy | 1 | 高三十班 |
| 2 | sdongwan | boy | 1 | 高三十班 |
| 3 | nn | girl | 3 | NULL |
+——-+———-+——–+—–+———-+
右連線的結果集包含指定的右表全部資料與匹配的左邊資料,左邊中沒匹配的全為空值.關鍵字 right join
select stuid,name,gender,t_stu.cid,cname from t_stu right join t_classroom on t_stu.cid=t_classroom.cid;
表t_stu右連線t_classroom;
結果:
+——-+———-+——–+——+———-+
| stuid | name | gender | cid | cname |
+——-+———-+——–+——+———-+
| 1 | zbo | boy | 1 | 高三十班 |
| 2 | sdongwan | boy | 1 | 高三十班 |
| NULL | NULL | NULL | NULL | 網工 |
+——-+———-+——–+——+———-+
having和where使用的區別:
where針對表中的列發揮作用,查詢資料
having對查詢結果中的列發揮作用,篩選資料
having使用在order by之前,group by之後
select name from t_stu group by name having name in (‘sdongwan’,’za’) order by name asc;
查詢 name,並且按照name升序排序
索引
create index index_name on t_stu(name);
在表t_stu的name列上面建立一個索引name;
show index from t_stu;
檢視t_stu表中的全部索引
drop index index_name on t_stu;
刪除t_stu表上的index_name索引
檢視(類似銀行的服務視窗,把指定資料拿出來建立檢視,這樣就不用直接操作資料庫,提高安全性):
create view stuView(name,gender) as select name,gender from t_stu;
從表select出name,gender來建立檢視view
desc stuView;
檢視檢視結構
對檢視的操作同直接操作資料庫一樣:
select * from stuView;
查詢檢視的全部資料
drop view if exists stuView;
刪除檢視stuView;
create or replace view stuView as select name,gender,cid from t_stu ;
更新檢視結構
update stuView set name = ‘dengchaori’ where cid=1;
更新檢視資料