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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章