MySQL資料管理
前言
首先,新年快樂!牛年希望能更牛,學到更多知識~
這一節接著上一節,了解的是MySQL的資料管理有關知識點
以下是個人碼的筆記,供學習交流參考~
1、外鍵(了解即可)
自行百度,不建議使用
影響delete和update
2、DML語言(全部記住)
資料庫意義:資料存盤,資料管理
DML語言:資料操作語言
- insert
- update
- delete
3、添加(insert)
--insert into 表名([欄位名1,欄位2,欄位3]) values ('值1'),('值2'),('值3')
insert into `grade`(`gradename`) values ('大四')
--插入單個欄位多個值
insert into `grade`(`gradename`) values ('大二'),('大一')
--插入多個欄位多個值
insert into `student`(`name`,`pwd`,`sex`) values ('張三','123456','男'),('李四','123456','男')
注意事項:
- 欄位和欄位之間使用 英文逗號 隔開
- 欄位可以省略,只不過添加的值要一一對應
- 可以同時插入多條資料,values后面的值,需要使用 英文逗號 隔開并用 英文括號 框住
4、修改(update)
--修改學員名字
--帶where條件(id為1的同學姓名改為張三)
update `student` set `name` = '張三' where id = 1;
--不指定條件(所有的name都改為了李四)
update `student` set `name` = '李四';
--語法
--update 表名 set colnum_name = value , colnum_name = value...] where [條件]
--修改多個屬性用 英文逗號 隔開
update `student` set `name` = '王五' , `Email` = 'xxxx@qq.com' where id = 1;
--使用多個條件判斷
update `student` set `name` = '李四' where `name`='李四' and sex = '男';
運算子會回傳boolen值
| 運算子 | 含義 | 范圍 | 結果 |
|---|---|---|---|
| = | 等于 | 5=6 | false |
| <>或!= | 不等于 | 5<>6 | true |
| > | 大于 | 5>6 | false |
| < | 小于 | 5<6 | true |
| >= | 大于等于 | 5>=6 | false |
| <= | 小于等于 | 5<=6 | true |
| between x and y | 在x和y之間(包含x和y) | 11 between 1 and 10 | flase |
| and | && | 5>1 and 1>2 | false |
| or | || | 5>1 or 1>2 | true |
語法:
update 表名 set colnum_name = value , colnum_name = value…] where [條件]
注意:
-
colnum_name 是資料庫的列,盡量帶上``(tab鍵上方)
-
條件,篩選的條件,若無指定,則會修改所有的列
-
value,是一個具體的值,也可以是一個變數
current_time就是一個變數,表示當前時間update `student` set `birthday'`= current_time where `name` = '張三' and sex = '男' -
多個設定的屬性之間,用英文逗號隔開
5、洗掉
delete 命令
--洗掉資料
--語法: delete from 表名 [where 條件]
--洗掉指定資料(洗掉id=1的學生資料)
delete from `stduent` where id = 1;
truncate 命令
作用:完全情況一個資料庫表,表的結構和索引約束不會改變!
--完全清空一個表的資料
truncate `student`
delete 和 truncate 的區別
-
相同點:都可以洗掉資料,都不會洗掉表結構
-
不同點:
-
truncate 重新設定 自增列 計數器 會歸零
-
truncate 不會影響事務
-
--測驗delete和truncate的區別
--先創建test表
creat table `test`(
`id` int(4) not null auto_increment,
`coll` varchar(20) not null,
primary key ('id')
)engine=INNODB default charset=utf8
--給test添加coll的值
insert into `test`(`coll`) values('1'),('2'),('3')
delete from `test`
--雖然資料洗掉,但不會影響自增
truncate from `test`
--不僅資料洗掉,而且自增和計數器都清零了
delete洗掉的問題:重啟資料庫的現象
- InnoDB 自增會從1開始 (存在記憶體中,斷電即失)
- MyISAM 繼續從上一個自增量開始(存在檔案中,不會丟失)
后話
MySQL的學習仍在繼續,新的一年更加努力!
有任何問題或者錯誤可以在評論區提出!
ps:我的上一篇MySQL學習博客:MySQL學習之《操作資料庫》,我的下一篇MySQL學習博客:MySQL學習之《查詢資料》,
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/259541.html
標籤:其他
上一篇:資料庫連接池怎么用?
下一篇:操作資料庫的欄位案例
