Mysql-Sql增删改查

1、查询

-- 查询
select * from user

 注释快捷键:ctrl+/

2、创建表

-- 格式

create table 表名(

  字段名 类型 约束,

   字段名 类型 约束

)

-- 举例

create table test3(
  id int unsigned primary key auto_increment,
  name varchar(5),
  age int unsigned,
  height decimal(5,2)
)

int unsigned:int类型、无符号

primary key:主键

auto_increment:自动递增

decimal:小数点类型,长度为5,小数点后面2位

3、删除表

-- 格式
drop table 表名
drop table if exists 表名

if exists:如果存在则删除,不存在也不报错

-- 举例

drop table if exists test3;

create table test3(
id int unsigned primary key auto_increment,
name varchar(5),
age int unsigned,
height decimal(5,2)
)

4、增加数据

-- 格式1 给所有字段设置数据
insert into 表名 values(...)

-- 举例
insert into test3 values(0,'阿三',18,160.5)

主键是自动递增的,需要用0、default、null进行占位

-- 格式2 给部分字段设置数据

insert into test3(name,age) values('小A',11)

-- 格式3 插入多行的数据

insert into test3(name,age) values('a',10),('b',11),('c',12)

5、修改数据

-- 格式
update 表名 set 列1=值1,列2=值2 where 条件

-- 举例

update test3 set name='阿四',age=11 where name='阿三'

5、删除数据

-- 格式
delete from 表名 where 条件

-- 举例

delete from test3 where id=2

6、逻辑删除
给要删除的数据打上一个删除标记,在逻辑上是数据是被删除的,但数据本身依然存在(这行记录还是存在的)

为数据增加字段为:isdelete,1与0代表删除与不删除

-- 格式

update test3 set isdelete=1 

update test3 set isdelete=0 where id = 1

猜你喜欢

转载自www.cnblogs.com/katyhudson/p/12603954.html