mysql基本语句

本文详细介绍了MySQL的基本操作,包括DDL(数据定义语言)如创建、删除和修改数据库及表格,DML(数据操作语言)如插入、删除和更新数据,以及如何进行筛选、排序。此外,还讲解了各种函数的使用,如比较函数、数学函数、日期时间函数、控制流程函数和字符串函数,并探讨了分组函数和嵌套查询的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sql查询语言(structred Query Langeuage)

一:DDL数据定义语言(data Definded Language)

用来定义数据库中的对象(database table…)

1创建、删除、展示一个数据库;
a:创建一个数据库;字母不区分大小写。

create database text

在这里插入图片描述
b:创建utf-8字符集数据库

create databse 表名字 deault charset ='utf-8'

c:展示所有数据库

show datbases;

d:删除一个数据库

drop databases 表名字。

e:切换数据库

use 数据库名字;

f:查看正在使用的数据库

select database();

2:创建、查询、删除表格

a :创建表

create table 表名字(
列名 类型(长度),
列名 类型(长度),
)character set utf8 排序滚则); 
(默认coollate usf8_general_ci;  usf8_unicode_ci

b :查询表 :

  • 查看一个数据库中所有的表
show tables;
  • 展示表的状态
show table status from 数据库名字
  • 查看表中的所有信息
select * from 表名字;

c:删除表

drop table 表名;

d:表中的存储类型
1)数值型

  • 整数:tinyint smallint mediumint int4 bigint
  • 小数:float4 double8 deciml numeric;

2)字符型
char字符串 varcahr 可变长字符串 varaiable可变的

f:通过DDL语句修改表格结构

  • 修改表格名字
  alter table 原表名 rename to 新名字;
  • 新增列
 alter table student add sage int(22);
  • 修改原有的列
alter table 表名字 change 原列名 新列名字 类型(长度);
  • 删除原有的列
 alter table 表名字 drop 列名字;

3:DML(数据操作语言)data Manipulation Labgeuage
用来操作数据库表格的信息,写入信息 新增信息insert 删除 deLete 修改upadte,读物信息: 查询select。

a:新增记录

insert into 表名(列名,列名,列名)values(值值值);

b:新增多条记录

 insert into student values(22,'大卫'),(99,'卢斯');

c:查询列

select 列名字,列名字  from 表名字;

d:删除所有列

delete from 表名字;

e:删除单个列

delete from 表名(where...)

f:修改单个列

update 表名 set 列=值,列=值【where...】

g创建数据库时候让每一个表到式utf8格式

create database 名字  default character set utf8则

创建表的时候

create table 名字(
列 类型 长度
)character set字符集 collate 排序规则

排序规则uft-8 general cli 默认 性能高 可能不精准(俄罗斯、越南等等会出现)
utf8 unicode ci 性能低 扩展性好

二、在寻找、删除、等操作数据的时候筛选

条件筛选where
1:除了insert其他语句都快可以做筛选
where这个关键字,可以拼接在delete select等后面。
delete from 表 where…
select from 表 where
upadate 表 set 列 = 值 where…;
2:筛选出来符合条件记录行数并不是列数。
3:按照某一个列或者条件进行筛选。
4:用法

例子:学生信息表格

--------+----------+-------+----------+------------+
| sname  | schinese | smath | senglish | sbirth     |
+--------+----------+-------+----------+------------+
| 李博涛 |    93.50 | 99.70 |    75.50 | 1997-05-16 |
| lucy   |    66.00 | 90.00 |    88.00 | 1998-06-16 |
| dawe   |    67.00 | 55.00 |    76.00 | 1999-08-16 |
| bob    |    60.00 | 91.00 |    82.00 | 1996-03-12 |
| leticy |    67.00 | 55.00 |    76.00 | 1999-08-16 |
| jony   |    60.00 | 91.00 |    82.00 | 1996-03-12 |
+--------+----------+-------+----------+------------+

上图是已经创建好的表格;

  • 1:根据比较运算符查找(= > < != )
select * from student where schinese>94;

返回值:

+--------+----------+-------+----------+------------+
| sname  | schinese | smath | senglish | sbirth     |
+--------+----------+-------+----------+------------+
| 李博涛 |    93.50 | 99.70 |    75.50 | 1997-05-16 |
+--------+----------+-------+----------+------------+
  • 2:利用算术运算符比较(+ - * /)
select * from student where  senglish +10> 90

结果:

+-------+----------+-------+----------+------------+
| sname | schinese | smath | senglish | sbirth     |
+-------+----------+-------+----------+------------+
| lucy  |    66.00 | 90.00 |    88.00 | 1998-06-16 |
| bob   |    60.00 | 91.00 |    82.00 | 1996-03-12 |
| jony  |    60.00 | 91.00 |    82.00 | 1996-03-12 |
+-------+----------+-------+----------+------------+
  • 3:逻辑运算符(and or not)
select * from student where smath > 90.00 and senglish>80;类似&
结果:
+-------+----------+-------+----------+------------+
| sname | schinese | smath | senglish | sbirth     |
+-------+----------+-------+----------+------------+
| bob   |    60.00 | 91.00 |    82.00 | 1996-03-12 |
| jony  |    60.00 | 91.00 |    82.00 | 1996-03-12 |
+-------+----------+-------+----------+------------+

发送一个指令到数据库发生了什么

  • 解析sql

  • 从表格中全部数据读取出来,放在数据库缓存,集合list.

  • 将集合list做遍历,每次拿到一个元素。
    假设又7个数据
    那么他会按照第一个条件筛选,选出5个正确,还会根据第二个规则,5次的循环。所以性能很低,尽量将条件苛刻的放在前面

  • [not]between and(包含临界值 ex: 2<=x<=24)

select * from student where senglish between 60 and 80;

±-------±---------±------±---------±-----------+
| sname | schinese | smath | senglish | sbirth |
±-------±---------±------±---------±-----------+
| 李博涛 | 93.50 | 99.70 | 75.50 | 1997-05-16 |
| dawe | 67.00 | 55.00 | 76.00 | 1999-08-16 |
| leticy | 67.00 | 55.00 | 76.00 | 1999-08-16 |
±-------±---------±------±---------±-----------+
他就只需要遍历7次

  • 【not】in
select * from student where senglish in (60,75.50);
  • ** 5:like模糊查询**
select * from student where sname like 'l%';
+--------+----------+-------+----------+------------+
| sname  | schinese | smath | senglish | sbirth     |
+--------+----------+-------+----------+------------+
| lucy   |    66.00 | 90.00 |    88.00 | 1998-06-16 |
| leticy |    67.00 | 55.00 |    76.00 | 1999-08-16 |
+--------+----------+-------+----------+------------+

删除操作

delete from student where sname like 'l%';

更新操作

update student set sname = 'myjah' where sname = 'lucy';
+--------+----------+-------+----------+------------+
| sname  | schinese | smath | senglish | sbirth     |
+--------+----------+-------+----------+------------+
| 李博涛 |    93.50 | 99.70 |    75.50 | 1997-05-16 |
| dawe   |    67.00 | 55.00 |    76.00 | 1999-08-16 |
| bob    |    60.00 | 91.00 |    82.00 | 1996-03-12 |
| jony   |    60.00 | 91.00 |    82.00 | 1996-03-12 |
+--------+----------+-------+----------+------------+

排序

order by
升序排列 asc(默认的)
降低排序 desc

select * from student order by smath desc;

三、函数

1:mysql函数是已经定义好的Api.
2:函数需要调用,直接放在语句中相当于调用;函数可以放在select后面当作一个显示内容,可以放在where后面,当作条件
3:函数都有返回值
4:

放在查询中 select 函数(列)from 表格
条件筛选 select 列 from 表格 where sal> 函数(值)

4:按照功能来划分函数

- 比较函数
ifnull(val)真返回1假返回0

  • 数学函数
    MATH(val)
    ABS(val)
    floor向下取整
    pow类
    round()
  • 日期 和时间
    Date类 now(0) year(val) Calendar(val) month(val) val为是时间格式
select ename,year(hiredate),month(hiredate),day(hiredate) from emp;

- 控制流程函数
if() ----类似三目运算符

select sal, if(sal>1000,'大于1000','小于1000') as result from emp;
+---------+----------+
| sal     | result   |
+---------+----------+
|   800.2 | 小于1000 |
| 1600.02 | 大于1000 |
|    1250 | 大于1000 |
|    2975 | 大于1000 |
|    1250 | 大于1000 |
|    2850 | 大于1000 |
|    2450 | 大于1000 |
|    3000 | 大于1000 |
|    5000 | 大于1000 |
|    1500 | 大于1000 |
|    1100 | 大于1000 |
|     950 | 小于1000 |
+---------+----------+

isnull

elect ename,sal + ifnull(comm,0) from emp;
  • 字符串函数
    length() substring indexof() concat() reeplace() upper() trime()
select ename,substr(ename,1,3) from emp;

———————————————————————————————————
- 分组函数

1:distinct(去重复 )
distinct列 如果有一样的信息 将一样的信息合并,行数会减少

2:分组函数+分组条件

分组函数
根据工资查找个数

select count(salary) from person;

查询 工资最高的

elect max(salary) from person;
+-------------+
| max(salary) |
+-------------+
|     8000.00 |
+-------------+

求和

select sum(salary) from person;
+-------------+
| sum(salary) |
+-------------+
|    36000.00 |
+-------------+

平均数

select avg(salary) from person;
+-------------+
| avg(salary) |
+-------------+
| 6000.000000 |
+-------------+

分组函数 groub by 列

select department ,count(salary) from person group by department;
+------------+---------------+
| department | count(salary) |
+------------+---------------+
| 事业部     |             3 |
| 产业部     |             1 |
| 产业部门   |             1 |
| 保洁部     |             1 |
+------------+---------------+

有着统计的作用

嵌套
一个完整的sql语句中,嵌套了完整的sql语句
可以将一个查询的结果放在一个查找范围
ysql> select * from (select username,address from person) new;
±---------±--------+
| username | address |
±---------±--------+
| 张三 | 武当 |
| 张无忌 | 魔教 |
| 张三丰 | 武当 |
| 周芷若 | 峨眉 |
| 谢逊 | 魔教 |
| 杨逍 | 魔教 |
| lucy | 魔教 |
±---------±--------+

select * from (select username,address from person) new where address ='魔教'
    -> ;
+----------+---------+
| username | address |
+----------+---------+
| 张无忌   | 魔教    |
| 谢逊     | 魔教    |
| 杨逍     | 魔教    |
| lucy     | 魔教    |
+----------+---------+

注意要起一个别名字


几个关键词的适使用

  • in(not in )
    满足括号的一个条件即可
    括号里也可以是另一条sql语句查询的语句


    下面的关键词后面只能是sql语句

  • any(!any >any <any )

select salary from person where salary > any(select salary from person where salary =7000
  • some与any一致
select salary from person where salary   >some(select salary from person where salary <7000);
  • all(满足条件中的所有子集)
select salary from person where salary   >all(select salary from person where salary <7000);

集合操作
union(两个集合的并集,会去重)
uniall(不做任何处理)
要求两个查询列的个数一样

select tid,tage from teacher union select username,address from person
    -> ;
+--------+------+
| tid    | tage |
+--------+------+
| 1      | 22   |
| 2      | 29   |
| 张三   | 武当 |
| 张无忌 | 魔教 |
| 张三丰 | 武当 |
| 周芷若 | 峨眉 |
| 谢逊   | 魔教 |
| 杨逍   | 魔教 |
| lucy   | 魔教 |
+--------+------+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值