MySQL基础DDL、DQL、DML、DCL(最基本必会的增删改查)

最基本必会的增删改查

一.增Insert
(特殊的如果id自动递增的话,就不需要插入id)
基本语法  insert into 表名(列1,列2,列3,列4,...)  values(值,值,值)
例子  insert into student(name,sex,age) values('张三',18,'男')


插入的另外一种形式:
insert into 表名 set 列=值,列=值,列=值,....
例子  insert into student Set name = '张三',age=18

二.删delete
基本语法 delete from 表名 where 列=值
例子  delete from student where id=1
#DELETE from 表名 一行行删除整张表
# TRUNCATE table 表名 就是清空表

三.改update
基本语法  update 表名 set 列=值,列=值,.... where...
列子  update student set name = '张三' where id=1

四.查select
1.基本的select查询语句
select * from student(查询student表中所有列)
*代表代表所有列,要查询哪一列就把*改成哪一列,可以查询一列,也可以查询多列,多列用逗号隔开
from后面跟的是表名
查询的结果包含列名和每一列的数据

2.条件查询where
基本条件查询语句  select * from student where id =1(表示查询student表中id=1的学生的所有列)
where后跟查询条件,查询的如果是字符串,要带引号

多条件查询   如果是并且条件用and 或者用or
多条件查询例子:
查询出班级号为20201001班的学生并且要求是男生,年龄大于20
SELECT * from student where class_num = '20201001' and sex = '男' and age > 20

查询出班级号为20201001班的学生或者性别为女的学生
SELECT * from student WHERE class_num = '20201001' or sex = '女'

3.#去重查询 DISTINCT
例子:查询出表当中有哪些性别   SELECT DISTINCT sex FROM student

4.模糊查询重点 模糊查询可以实现搜索功能
基本语法  like 通配符
例子  select * from student where name like '%王' 表示模糊查询name以王结束的
select * from student where name like '王%' 表示模糊查询name以王开头的
select * from student where name like '%王%' 表示模糊查询name中包含王字的

5.排序查询 ORDER BY 倒序关键字DESC
默认排序规则是根据id进行排序,并且是id从小到大    SELECT * FROM student


例子 按照年龄从低到高进行排序
SELECT * FROM student ORDER BY age

按照年龄从高到低进行排序
SELECT * FROM student ORDER BY age DESC

6.分页查询
limit关键字 限定查询多少条数据
OFFSET关键字 从第几条数据开始查询 默认第一条数据是0

查询出第二页的数据,这一页有3条数据
SELECT * FROM student LIMIT 3 OFFSET 3

知道页数和每页查询的数据(固定)
SELECT * FROM student LIMIT 数据量 OFFSET 数据量 * (页数-1)

SELECT * from student LIMIT 0,3
LIMIT 0,3 limit后一个数据表示从第几条数据查起,后面数据是查询的条数

7.聚合查询
count() :记录查询列有多少行
SUM() :求数值序列的和
AVG() :求平均数
MAX() :求最大值
MIN() :求最小值

#创建数据库并设置编码utf-8 多语言
create database `examples` default character set utf8 collate utf8_general_ci;
#删除数据库
drop database examples;
#创建表
create table test(
    id int(10) unsigned zerofill not null auto_increment,
    email varchar(40) not null,
    ip varchar(15) not null,
    state int(10) not null default '-1',
primary key (id)
)engine=InnoDB;
#显示表结构
describe 
#删除表
drop table test;
#重命名表
alter table test_old rename test_new;
#添加列
alter table test add cn int(4) not null;
#修改列
alter table test change id id1 varchar(10) not null;
#删除列 
alter table test drop cn;
#创建索引
alter table test add index (cn,id);
#删除索引
alter table test drop index cn
#插入数据
insert into test (id,email,ip,state) values(2,'qq@qq.com','127.0.0.1','0');
#删除数据 
delete from test where id = 1;
#修改数据
update test set id='1',email='q@qq.com' where id=1;
#查数据
select * from test;  #取所有数据
select * from test limit 0,2;  #取前两条数据 
select * from test email like '%qq%' #查含有qq字符 _表示一个 %表示多个
select * from test order by id asc;#降序desc
select * from test id not in('2','3');#id不含2,3或者去掉not表示含有
select * from test timer between 1 and 10;#数据在1,10之间

#---------------------------表连接知识------------------------------
#等值连接又叫内链接 inner join 只返回两个表中连接字段相等的行
select * from A inner join B on A.id = B.id; #写法1
select * from A,B where A.id = B.id; #写法2
select a.id,a.title from A a inner join B b on a.id=b.id and a.id=1;#写法3 表的临时名称
select a.id as ID,a.title as 标题 from A inner join B on A.id=B.id;#添加as字句

#左连接又叫外连接 left join 返回左表中所有记录和右表中连接字段相等的记录
select * from A left join B on A.id = B.id;

select * from A left join (B,C,D) on (B.i1=A.i1 and C.i2=A.i2 and D.i3 = A.i3);#复杂连接

#右连接又叫外连接 right join 返回右表中所有记录和左表中连接字段相等的记录
select * from A right join B on A.id = B.id;

#完整外部链接 full join 返回左右表中所有数据
select * from A full join B on A.id = B.id;

#交叉连接 没有where字句 返回卡迪尔积
select * from A cross join B;
-------------------------表连接结束------------------------------------------------------------
-----------------索引创建------------------------------------------------
show index from A #查看索引
alter table A add primary key(id) #主键索引
alter table A add unique(name) #唯一索引
alter table A add index name(name) #普通索引
alter table A add fulltext(name) #全文索引
alter table A add index name(id,name) #多列索引

#常用函数
abs(-1)#绝对值
pi()#pi值
sqrt(2)#平方根
mod(-5,3)#取余-2
ceil(10.6)#进位+1 结果11 ceil(10.0)结果10
floor(10.6)#取整 10
round(2.5)#四舍五入到整数 结果3
round(2.5,2)#保留两位小数 结果2.50
truncate(2.5234,3)#取小数后3位不四舍五入 2.523
sign(-2);#符号函数 返回-1 0还是0 正数返回1
pow(2,3),exp(2);#2的3次幂 或e的2次幂
log(2),log10(2);#求对数
radians(180),degrees(0.618);#角度弧度转换
sin(0.5),asin(0.5)#正弦和反正弦 类似cos acos tan atan
length('hi')#计算字符长度
concat('1',1,'hi')#合并字符串
insert('12345',1,0,'7890');#从开头第1个字符开始到0个结束,替换成后边字符串,0表示在最前边插入
ucase('a'),lcase('A')#转成大写和小写
left('abcd',2),right('abcd',2);#返回前两个字符和后两个字符
ltrim('  0  '),rtrim(' 0 '),trim('  0  ')#删除空格
replace('1234567890','345678','0');#替换输出12090
substring('12345',1,2)#取字符 输出12 1是位置 2是长度
instr('1234','234');#取得234位置是2
reverse('1234');#反序输出4321
current()#返回日期
curtime()#返回时间
now()#返回日期时间
month(now())#当前月份 monthname 英文月份
dayname(now())#星期英文 dayofweek()1是星期天 weekday()1是星期二
week(now())#本年第多少周
dayofyear(now()),dayofmonth(now())#今天是本年第多少天 今天是本月第多少天
year(now()),month(now()),day(now()),hour(now()),minute(now()),second(now())#返回年月日 时分秒
time_to_sec(now()),sec_to_time(3600*8);#转换时间为秒和还原
version()#mysql版本
database()#当前连接的数据库 没有为null
user()#获取用户名
md5('a')#加密字符串
ascii('a')#ascii值97
bin(100),hex(100),oct(100)#返回二进制 十六进制 八进制
conv(10001,2,8);#各种进制相互转换
rand()#生成0到1之间随机数
sleep(0.02)#暂停秒数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值