MySQL資料庫基礎(六)——SQL插入、更新、刪除操作

來源:互聯網
上載者:User

標籤:SQL

MySQL資料庫基礎(六)——SQL插入、更新、刪除操作一、插入資料1、為表的所有欄位插入資料

使用基本的INSERT語句插入資料要求指定表名稱和插入到新記錄中的值。

INSERT INTO table_name (column_list) VALUES (value_list);insert into `TSubject` (subjectID,subjectName,BookName,Publisher)values (‘0004‘,‘英語‘,‘大學英語走遍美國‘,‘清華出版社‘)可以不指定插入的列insert into `TSubject` values (‘0005‘,‘高等數學‘,‘高等數學教材‘,‘清華出版社‘)
2、為表的指定欄位插入資料

為表的指定欄位插入資料,就是在INSERT語句中只向部分欄位中插入值,而其他欄位的值為表定義時的預設值。
必須制定插入的列

insert into TSubject (subjectID,subjectName) values (‘0006‘,‘高等數學2‘)insert into TSubject (subjectName,subjectID) values (‘資料結構‘,‘0007‘)
3、同時插入多條記錄

INSERT語句可以同時向資料表中插入多條記錄,插入時指定多個值列表,每個值列表之間用逗號分隔開,基本文法格式如下:

INSERT INTO table_name (column_list) VALUES (value_list1),  (value_list2),..., (value_listn);

樣本:

insert into TSubject (subjectName,subjectID) values (‘C#開發‘,‘0008‘),(‘蘋果開發‘,‘0009‘)
4、將查詢結果插入到表中

INSERT語句和SELECT語句組成的組合語句即可快速地從一個或多個表中向一個表中插入多個行。

INSERT INTO  table_name1  (column_list1)    SELECT (column_list2) FROM table_name2           WHERE (condition)

樣本

insert into ST (subectid,subjectName) select  subjectid,subjectName from TSubject where `Publisher` is not null
二、更新資料1、根據本表的條件更改記錄

MySQL中使用UPDATE語句更新表中的記錄,可以更新特定的行或者同時更新所有的行。

UPDATE table_name      SET column_name1 = value1,     column_name2=value2,……,     column_namen=valuen     WHERE (condition);

樣本:

update `TStudent` set sname=concat(sname,‘net‘) where class=‘net‘update `TStudent` set sname=left(sname,3) where class=‘net‘

根據學生的生日 在學生的姓名後標記 1988大一 1987年大二 1986年大三 1985年大四

update TStudent set sname=CONCAT(sname,case year(birthday)%5   when 0 then ‘大四‘ when 1 then ‘大三‘ when 2 then ‘大二‘ else ‘大一‘ end)   where year(birthday)>=1985 and year(birthday)<=1988
2、根據另一張表的條件更改記錄
UPDATE table_nameA a join table_nameB b on a. column_name1=b. column_name1     SET a.column_name1 = value1,    a.column_name2=value2,……,     a.column_namen=valuen    WHERE b. column_name2>20

執行個體:將有不及格的學生姓名後加*標記

update TStudent a join TScore b on a.`StudentID`=b.`StudentID`set a.`Sname`=concat(sname,‘*‘) where b.mark<60

同時更改兩張表的列
執行個體:把分數低於60分的學生,加5分,並在學生姓名委任標記+

update TStudent a join TScore b on a.`StudentID`=b.`StudentID`set a.`Sname`=concat(sname,‘+‘), b.mark=b.`mark`+5 where b.mark<60;

子查詢也能實現相同功能
以下語句將分數有大於98分的學生姓名後加#號標記

update TStudent set Sname=concat(sname,‘#‘) where studentid in(select studentid from TScore where mark>98);
三、刪除資料1、根據本表的條件刪除記錄

從資料表中刪除資料使用DELETE語句,DELETE語句允許WHERE子句指定刪除條件。
DELETE FROM table_name [WHERE condition&gt;] ;
刪除學號小於00010的學生
delete from TStudent where studentid&lt;‘00010‘

2、根據另一張表的條件刪除記錄

DELETE a FROM table_a a join table_b b on a.column1=b.column1 [WHERE condition&gt;] ;
刪除分數小於60分的學生

delete a from TStudent a join TScore b on a.`StudentID`=b.`StudentID`where b.mark<60

也可以使用子查詢實現。
刪除分數表中分數大於90的學生
delete from TStudent where studentid in (select studentid from TScore where mark&gt;90);

MySQL資料庫基礎(六)——SQL插入、更新、刪除操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.