SQL基础
- 其他
- 2025-08-17 07:48:03

DDL:数据定义 DML:数据操纵 DCL:数据控制 DQL:数据查询
SQL不区分大小写,可以以多行书写,以 ; 结束
注释:单行注释 --空格 ,多行注释 /* */
[] :表示可选部分
DDL:数据定义 -- 我是注释 /* 多行注释 */ -- 查看数据库 show databases; show databases; -- 使用数据库 use 数据库名称; use test; -- 创建数据库 create database 数据库名称 [CHARSET UTF8]; create database test charset utf8; -- 删除数据库 drop database 数据库名称; drop database week; -- 查看当前使用的数据库 select database(); -- 查看有哪些表 show tables ; show tables ; -- 创建表 /* create table 表名称( 列名称 列类型, ) 列类型:int,float,varchar(长度):文本,data:日期,timestamp:时间戳 */ create table student( id int, name varchar(10), age int ); -- 删除表 drop table 表名称 drop table student DML:数据操纵 -- 创建数据库 create database an charset utf8; -- 使用数据库 use an; -- 创建表哥 create table student( id int, name varchar(10), age int ); -- 单条插入 insert into 表名(列1) value(值1),(值2),(值3); insert into student(id) value(1),(2),(3); -- 多条插入 insert into 表名(列1,列2...) value(值1,值2,值3),(值1,值2,值3)...; -- 注意:1) 字符串的值需要用 '' 2) 一一对应 insert into student(id,name,age) value(4,'周杰伦',31),(5,'林俊杰',32); -- 删除数据 delete from 表名 【where 条件判断】 delete from student where id < 3; -- 数据更新 update 表名 set 列=值 [where 条件判断] update student set name='小明' where id=4; -- 更新所有的列的值时就不需要加where update student set name='小红'; DQL:数据查询 -- 基础数据查询 select 字段列表|* from 表 【where 条件判断】 select id,name,age from student; -- 查询指定列 select * from singer; -- 查询全部列 select * from singer where age>25; -- 条件 select * from singer where gender='男' -- group by: 分组聚合 -- select 字段|聚合函数 from 表 [where 条件] group by 列 -- 聚合函数:sum(列),avg(列),min(列),max(列),count(列|*) select gender,avg(age),min(age),max(age),sum(age),count(*) from singer group by gender; -- 非聚合函数:group by出现哪个列,哪个列才能出现在select中的非聚合中 -- order by :结果排序 asc:升序 desc:降序 -- select 列|聚合函数|* from 表 where 条件 order by...[asc|desc] select * from singer where age>20 order by age asc; -- 结果分页限制 -- select 列|聚合函数|* from 表 limit n[,m] select * from singer limit 1; select * from singer limit 1,2; -- 前n条跳过,取m条 select age,count(*) from singer where age>20 group by age order by age limit 3; 使用python执行SQL # 1. 安装pymysql: pip install pymysql # 2. 创建到MySQL的数据库链接: from pymysql import Connection # 获取到MySQL数据亏的链接对象 conn = Connection( host='localhost', # 主机名 port=3306, # 端口 user='root', # 用户名 password='123456789' # 密码 ) # 打印MySQL数据库软件信息 print(conn.get_server_info()) # 关闭到数据库的链接 conn.close()下一篇
四.镜头知识之放大倍率