SQL增删改查常用语句

1、创建表
start

操作SQL
创建表create table 表名(字段…);
字段类型数值型(5种整数类型、三种浮点数类型)、字符串型(char、varchar)、日期型、二进制类型
键的类型主键(primary key)、外键(foreign key)
默认值default 默认值
是否为空is not null、is null
注释comment ‘’

例如:创建一个学生表

create table student(
	id int primary key comment '学号',
    name vachar(255) is not null comment '姓名',
    age int is not null default 18 comment '年龄',
    birth date
);

end



2、操作表
start

操作SQL 语法
更新表rename table 旧表名 to 新表名
更新字段名alter 表名 chenge 旧字段名 新字段名 字段类型
更新字段类型、null值、默认值alter table 表名 modify 字段名 新字段类型
删除字段默认值alter table 表名 alter column 字段名 drop default
设置主键alter table 表名 add primary key (主键字段列表)
删除主键drop primary key;
新增字段alter table 表名 add column 新字段名 字段类型以及其他属性
删除字段alter table 表名 drop column 字段名
删除表drop table 表名

end



3、插入数据
start

操作SQL
插入整行字段insert into 表名 values (?,?,?,…)
插入部分字段insert into 表名 (…,字段i, 字段j, … ) values (…,valuei,valuej,…)
一次插入多行记录insert into 表名 values (…, ?, ?, ?, …), (…, ?, ?, ?, …),…;
借助其他结果集插入insert into 表名 select * from 表名
克隆表select * into 表名1 from 表名2

end



4、更新数据
start

操作SQL
更改数据update 表名 set 更改的内容 where 约束条件

end



5、查询数据
start

操作SQL
查询全部字段select * from 表名
查询部分字段select 字段1,字段2,… from 表名
排序返回结果集select 字段1,字段2, … from 表名 order by [asc | desc]
限制返回结果集select 字段1,字段2, … from 表名 order by [asc | desc] limit m,n;

end



6、过滤查询结果集
start

select 字段1,字段2,... from 表名 where 过滤条件;

过滤操作符(1)

分类操作符说明适用范围
过滤单个值= (<>、!=)等于(不等于)字符型、数值型、日期和时间型
> (<)大于(小于)
>= (<=)大于等于(小于等于)
过滤null值is null为null值字符型、数值型、日期和时间型
is not null不为null值

过滤操作符(2)

分类操作符说明适用范围
过滤集合between指定的两个值之间字符型、数值型、日期和时间型
in包含
not in不包含
exists存在于
not exists不存在于
like匹配like关键字后的模式仅适用于字符型
not like不匹配like关键字后的模式

end

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值