MySQL之數據操作


邏輯刪除

保護重要數據。

1. 在數據庫中新增一個字段 isDelete, bit類型, 默認爲0,表示沒有刪除。
2. 若要刪除改爲1,獲取 isDelete=0 的數據。

查看更多mysql命令:

mysql --help

查詢當前所選的數據庫:

show databases;     查看所有數據庫
select database();   查詢當前所選的數據庫
create database dimples charset=utf8;   創建數據庫,指定字符編碼

show tables;    查看當前所有表
desc students;   查看錶結構
創建表
create table students( 
    id int auto_increment primary key not null,
    name varchar(10) not null, 
    gender bit default 1, 
    birthday datetime);
修改表
alter table 表名 add|change|drop 列名 類型;

(增加,修改,刪除列)如:
alter table students add isDelete bit default 0;
刪除表
drop table 表名;
rename table 原表名 to 新表名;   更改表名稱
show create table 表名;       查看錶的創建語句

數據操作

查詢
select * from 表名;
增加
全列插入:insert into 表名 values(0,'huahua',...);

缺省插入:insert into 表名(字段) values(值);
     例如:insert into students(name) values('羅晉');
     
插入多條數據:
insert into students(name) values('楊過'),('小龍女');
修改
update 表名 set1=值1,... where 條件
 例如:
update students set birthday='1990-2-2' where id=3;
刪除
delete from 表名 where 條件;

邏輯刪除:

update students set isDelete=1 where id=7;

select * from students where isDelete=0;


發佈了86 篇原創文章 · 獲贊 33 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章