標籤:
寫在前面
上篇文章學習了建立資料庫和資料表,這篇文章將學習對資料表的增刪改查操作。
系列文章
mysql之建立資料庫,建立資料表
一個例子
上篇文章中,建立了資料庫和資料表,資料表中還沒有資料,這裡我們為三張表中添加資料進行測試。
注意:為了避免欄位名或者表明與系統的某些關鍵字重複,可以使用``包裹字串,與sql server中的[]類似。``在鍵盤上方數字鍵最左邊的那個鍵(英文IME)
1、添加四個班級資訊
use school;-- 添加班級資訊insert into tb_class(`name`) values(‘信管01‘);insert into tb_class(`name`) values(‘信管02‘);insert into tb_class(`name`) values(‘信管03‘);insert into tb_class(`name`) values(‘信管04‘);
2、新增學生資訊
use school;-- 新增學生資訊insert into tb_student(`Name`,`phone`,`age`,`gender`,`classid`) values(‘張三‘,‘13810707322‘,20,1,1);insert into tb_student(`Name`,`phone`,`age`,`gender`,`classid`) values(‘李四‘,‘13810707324‘,19,1,2);-- 大量新增insert into tb_student(`Name`,`phone`,`age`,`gender`,`classid`) values(‘王二‘,‘13810707325‘,18,1,2),(‘麻子‘,‘13810707323‘,23,1,1),(‘張三丰‘,‘13810707321‘,22,1,3),(‘張無忌‘,‘13810707326‘,21,1,1),(‘孫悟飯‘,‘13810707328‘,24,1,1),(‘孫悟空‘,‘13810707327‘,23,1,4),(‘鳴人‘,‘13810707329‘,25,1,1),(‘路飛‘,‘13810707320‘,26,1,2);
3、新增學生成績資訊
-- 新增學生成績insert into tb_score(`course`,`score`,`stuid`) values(‘高數‘,89,1),(‘電腦‘,89,1),(‘java‘,89,1),(‘.net‘,89,1);
好了,有資料了,就可以學習查詢,刪除,修改等操作了。
4、查詢所有學生資訊
1 use school;2 -- 查詢所有的學生資訊3 select * from tb_student;4 -- 等價於,不過在資料量非常大的時候,推薦使用下面的這種查詢方式。5 select id,`Name`,`phone`,Age,gender,createdate,classid from tb_student;
結果集
你會發現,在上篇文章中,已經為createdate添加了預設約束,但這裡並沒有顯示結果。原來的欄位名稱為date,後來改為了createdate。預設約束並沒有保持。
alter table tb_student change createdate createdate datetime default now();
測試,添加一條資料
insert into tb_student(`Name`,`phone`,`age`,`gender`,`classid`) values(‘明哥‘,‘13810707322‘,20,1,1);
結果
5、查詢‘信管03’班的所有學生資訊。
-- 查詢信管03班的學生資訊select s.id ‘編號‘,s.`Name` ‘名字‘,s.`phone` ‘電話‘,s.Age ‘年齡‘,s.gender ‘性別‘,s.createdate as ‘入學時間‘,c.`name` as ‘班級名稱‘ from tb_student s inner join tb_class c on s.classid=c.id;
從上面的sql語句可以看出,可以為欄位名起別名,通過as 或者直接寫別名,這點與sqlserver中類似。inner join的用法也類似。
結果
上面的結果,看起來比較亂,可以按照id進行升序排序。
use school;-- 查詢信管03班的學生資訊select s.id ‘編號‘,s.`Name` ‘名字‘,s.`phone` ‘電話‘,s.Age ‘年齡‘,s.gender ‘性別‘,s.createdate as ‘入學時間‘,c.`name` as ‘班級名稱‘ from tb_student s inner join tb_class c on s.classid=c.id order by s.id;
6、取前3為學生的資訊。
select s.id ‘編號‘,s.`Name` ‘名字‘,s.`phone` ‘電話‘,s.Age ‘年齡‘,s.gender ‘性別‘,s.createdate as ‘入學時間‘,c.`name` as ‘班級名稱‘ from tb_student s inner join tb_class c on s.classid=c.id order by s.id limit 3;
注意:取前幾條資料,這裡與sql server中的用法不同,在sql server中取前幾條資料使用的是top,而mysql使用limit。
7、刪除id=1的學生資訊。
use school;-- 刪除id=1的學生資訊。delete from tb_student where id=1;
這樣直接刪除,mysql會報一個錯誤。
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`school`.`tb_score`, CONSTRAINT `FK_Stuid` FOREIGN KEY (`stuId`) REFERENCES `tb_student` (`id`))
可以先加上這句話,再進行刪除。
use school;-- 刪除id=1的學生資訊。set FOREIGN_KEY_CHECKS = 0;delete from tb_student where id=1;
註:set FOREIGN_KEY_CHECKS = 0;取消外鍵檢測。否則mysql會認為刪除是非安全的。
9、更新所有的入學時間為空白的學生資訊,並設定入學時間為目前時間。
use school;-- 更新所有的入學時間為空白的學生資訊,並設定入學時間為目前時間SET SQL_SAFE_UPDATES = 0 ; update tb_student set createdate=now() where isnull(createdate);select * from tb_student;
在使用mysql執行update的時候,如果不是用主鍵當where語句,會報如下錯誤,使用主鍵用於where語句中正常。
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.
註:在修改的時候,需加上SET SQL_SAFE_UPDATES = 0 ;取消安全更新模式。如果想要提高資料庫安全等級,可以在恢複回原有的設定,執行命令:SET SQL_SAFE_UPDATES = 1;
執行上面的語句,執行成功。
總結
好了,mysql中使用的增刪改查就總結到這裡,如果有sqlserver資料庫的基礎,學mysql還是很簡單的。 下篇文章將介紹order by,group by等的使用。
mysql之select,insert,delete,update