数据库操作(增删改查)sql语句

1.什么是SQL?

Structured Query Language:结构化查询语言
其实就是定义了操作所有关系型数据库的规则。每一种数据库操作的方式存在不一样的地方,称为“方言”。

2.SQL通用语法
  • SQL 语句可以单行或多行书写,以分号结尾。

  • 可使用空格和缩进来增强语句的可读性。

  • MySQL 数据库的 SQL 语句不区分大小写,关键字建议使用大写。

  • 注释方法

     	单行注释: – 或 # (mysql 特有)
     	多行注释: /*  */
    
3.SQL分类

在这里插入图片描述

  • DDL(Data Definition Language)数据定义语言 用来定义数据库对象:数据库,表,列等。关键字:create, drop,alter 等
  • DML(Data Manipulation Language)数据操作语言 用来对数据库中表的数据进行增删改。关键字:insert, delete, update 等
  • DQL(Data Query Language)数据查询语言 用来查询数据库中表的记录(数据)。关键字:select, where 等
  • DCL(Data Control Language)数据控制语言(了解) 管理用户,授权 用来定义数据库的访问权限和安全级别,及创建用户。关键字:GRANT, REVOKE 等
4.DDL:操作数据库、表

操作数据库(database):CRUD


① C(create): 创建数据库

# 创建数据库:
create database 数据库名称;
#创建数据库,判断不存在,再创建:
create database if not exists 数据库名称;
#创建数据库,并指定字符集
create database 数据库名称 character set 字符集名;
#例如:
create database if not exists db4 character set gbk;

②R(Retrieve):查询数据库show

#查询所有数据库的名称:
show databases;
#查询某个数据库的字符集,查询某个数据库的创建语句
show create database 数据库名称;

③ U(update): 修改数据库alter

# 修改数据库的字符集
alter database 数据库名称 character set 字符集名称;
# 例如:
alter database mysql1 character set utf8;

④ D(delete): 删除数据库drop

# 删除数据库
drop database 数据库名称;
# 判断数据库存在,存在再删除
drop database if exists 数据库名称;

⑤ 使用数据库use

# 查询当前正在使用的数据库名称
select database();
# 使用数据库
use 数据库名称;

操作表(table)


① C(create): 创建表

create table 表名(
			列名1 数据类型1,
			列名2 数据类型2,
			....
			列名n 数据类型n
		);
# 例如:
create table student(
				id int,
				name varchar(32),
				age int ,
				score double(4,1),
				birthday date,
				insert_time timestamp
			);

② 复制已存在的表: like

create table 新表名 like 被复制的表名;

③ 数据类型

#--------------------------------------------------------
int:整数类型,例如,
age int,
#--------------------------------------------------------
double/float:小数类型,例如,
score double(5,2)//一共5位,小数点后2位,最大值999
#--------------------------------------------------------
decimal:数字型,高精度,银行账目计算
decimal(a,b)
a 指定指定小数点左边和右边可以存储的十进制数字的最大个数,最大精度38。
b 指定小数点右边可以存储的十进制数字的最大个数。小数位数必须是从 0 到 a之间的值。默认小数位数是 0#--------------------------------------------------------
date:日期,只包含年月日
yyyy-MM-dd
#--------------------------------------------------------
datetime:日期,包含年月日时分秒
yyyy-MM-dd HH:mm:ss
#--------------------------------------------------------
timestamp:时间戳类型 包含年月日时分秒
yyyy-MM-dd HH:mm:ss
如果将来不给这个字段赋值,或赋值为null,则默认使用当前的系统时间,来自动赋值
#--------------------------------------------------------
varchar:字符串
name varchar(20): 姓名最大20个字符
zhangsan 8个字符 张三 2个字符

④R(Retrieve): 查询表

#查询某个数据库中所有的表名称
show tables;
#查询表结构
desc 表名;

⑤ U(update): 修改表alter

# 修改表名rename to
alter table 表名 rename to 新的表名;
# 修改表的字符集
alter table 表名 character set 字符集名称;
# 添加字段
alter table 表名 add 列名 数据类型;
# 修改列名称和数据类型
alter table 表名 change 列名 新列别名 新数据类型;
# 修改数据类型
alter table 表名 modify 列名 新数据类型;
# 删除列
alter table 表名 drop 列名;

⑤ D(delete): 删除表

drop table if exists 表名 ;
5. DML: 增,删,改表中数据

①添加一行表数据:insert into

insert into 表名(列名1,列名2,…列名n) values(1,2,…值n);
# 列名和值要一一对应。
insert into 表名(列名) value();
# 如果不定义列名,则默认给所有列添加值
insert into 表名 values(1,2,…值n);
除了数字类型,其他类型需要使用引号(单双都可以)引起来

②删除表中数据:delete form

delete from 表名 [where 条件] #如果要删除所有记录 truncate
truncate table 表名; # 推荐使用,效率更高 先删除表,然后再创建一张一样的表
delete from 表名;  #不推荐使用。有多少条记录就会执行多少次删除操作

③ 修改数据:update 表名

update 表名 set 列名1 =1, 列名2 =2,[where 条件];
#注意:如果不加任何条件,则会将表中所有记录全部修改。
6.DQL:查询表中的记录

① 基础查询select … from

#所有字段的查询
select * from 表名;
#多个字段 
select 字段名1,字段名2from 表名;
#去除重复distinct
select distinct 字段名 form 表名
#计算列,算术运算符计算(一般只会进行数值型的计算)
select name,mathscore,englishscore,math+ifnull(english,0) form 表名
# null参与的运算,计算结果都为null

在这里插入图片描述
② 条件查询 where

  • where子句后跟条件,括号可加
  • 运算符 关系运算符> 、< 、<= 、>= 、= 、<> 逻辑运算符 and 或 &&
    where between…and… 在…和…之间
    in( 条件集合)
    like:模糊查询
    判断值是否为null(is null ,is not null)
select * from student where age >= 20 and age <=30;
select * from student where age >= 20 && age <=30;
select * from student where age between 20 and 30;
select * from student where age in (22,18,25);
select * from student where age = 22 or age = 18 or age = 25
# 1. 单个任意占位符: _
# 2. 多个任意占位符:%
– 查询姓马的有哪些? like"马%"
select * from student where name like ‘马%;
– 查询姓名第二个字是化的人like"_马%"
select * from student where name like “_化%;
– 查询姓名是3个字的人like"___"
select * from student where name like ‘___’;
– 查询姓名中包含德的人like"%德%"
select * from student where name like%%;
注意:null值不能使用 =null!=null判断
is not null --查询英语成绩为 null
select * from student where english is null;
is not null --查询英语成绩不为null
select * from student where english is not null;

③ 排序查询order by

order by math asc,englishScore desc
# asc:升序,默认的; desc:降序。
select * from Studentinfo order by age;
# 多个排序条件,优先第一条件
select * from Studentinfo order by id desc,age desc;

④聚合函数:列数据计算,排除null值的个数

注意:聚合函数的计算,排除值为null的记录。 解决方案:

  1. 选择不包含非空的列进行计算
  2. ifnull(null字段,0)函数
count:计算某个字段有几条数据
# 一般选择非空的列:主键 count(*): 计算表中一共有几条纪录,也就是所有字段有几条纪录
select count(name) from studentinfo; # name字段有几条数据记录
select count(ifnull(english,0)) from stuinfo
max:计算最大值
min:计算最小值
sum:计算和
avg:计算平均值

⑤分页查询 limit 分页限定

语法:limit 开始的索引,每页查询的条数;
     每页显示3条记录
     计算公式:开始的索引 = (当前的页码(第几页) - 1* 每页显示的条数
     limit关键字是一个MySQL"方言",只在mysql中用
select * from student limit 0,3; – 第1select * from student limit 3,3; – 第2select * from student limit 6,3; – 第3

⑥约束(字段数据值约束)

概念: 对表中的数据进行限定,保证数据的正确性、有效性和完整性。 分类:

  1. 主键约束:primary key
  2. 非空约束:not null
  3. 唯一约束:unique
  4. 默认值约束: default
  5. 外键约束:foreign key

非空约束:值不能为null

# 创建表时,添加约束
create table stu(
			id int,
			name varchar(20) not null  # name为非空
       );
# 创建表后,modify添加或删除约束
alter table stu modify name varchar(20) not null;
alter table stu modify name varchar(20);

唯一约束:unique,非null值不能重复

# 创建表时,添加唯一约束
create table stu(
			id int,
			number varchar(20) unique
			);
# 创建表后,modify添加唯一约束
alter table stu modify phone_number varchar(20) unique;
# 删除唯一约束:drop index
alter table stu drop index phone_number;

注意: mysql中,唯一约束限定的列的值可以有多个null

默认值约束: default “值”

# 创建表时添加默认值约束
sex char(1) default’男’
# 删除约束
alter table studentinfo modify gender varchar(1);

主键约束:primary key(主键组)

注意:

  1. 含义:非空且唯一
  2. 一张表只有一个主键
  3. 主键就是表中记录的唯一标识
# 在创建表时,添加主键约束
create table stu(
		id int primary key,  # 给id添加主键约束
		name varchar(20)
	);
# 创建完表后,modify添加主键
alter table stu modify id int primary key;
# 删除主键drop primary key
alter table stu drop primary key;
#错误 alter table stu modify id int ;

自动增长:auto_increment

概念:数值类型列值的自动增长

在创建表时,添加主键约束,并且完成主键自增长
create table stu(     # 给id添加主键约束
		id int primary key auto_increment ,
		name varchar(20)
	);
# 删除自动增长,modify
alter table stu modify id int;
# 添加自动增长,modify … auto_increment;
alter table stu modify id int AUTO_inCREMENT;

外键约束:foreign key

概念:让表于表产生关系,保证数据的正确性。

#在创建表时,可以添加外键 语法:
create table 表名(
			....
			外键列 constraint 外键名称 foreign key (外键列名称) references 主表名称(主表列名称)
		);
# 删除外键
drop foreign key 外键名
alter table 表名 drop foreign key 外键名称;
# 创建表之后,添加外键add
alter table 表名 add constraint 外键名称 foreign key (外键字段名称) references 主表名称(主表列名称);
# 父子表级联操作,添加级联操作语法
alter table 表名 add constraint 外键名称 foreign key (外键字段名) references 主表名(主表列名) on update cascade on delete cascade ;
# 分类:
①级联更新:on update cascade
②级联删除:on delete cascade
③外键定制三种约束模式
	restrictno action:父表更新或者删除时,子表有匹配记录,则禁止父表的更新和删除。默认选项
	cascade:父表更新或者删除时,子表有匹配记录,则父表操作成功,同时更新或删除子表匹配项
	set null:父表更新或者删除时,子表有匹配记录,则父表操作成功,同时将子表的匹配项设置为null(前提能设置为null)
alter table t2 add constraint c_fk foreign key t_id references t1(id) [on {delete | update} { restrict | no action | cascade | set null}];

详情请参考
02_SQL语句对数据库和表的增,删,改,查,约束操作

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值