mysql基本语句操作照片_MySQL基本操作语句 た 入场券 2022-10-27 05:12 214阅读 0赞 \[ 数据库 database \] 1.创建数据库: create database 数据库名 ; 2.删除数据库: drop database 数据库名 ; 3.使用数据库: use 数据库名 ; 4.查看所有库: show databases ; 5.查看库中表: show tables ; 6.查看表结构: desc 或 describe 表名 ; 7.创建表: create table 表名( 列名 类型 约束,... ) ; 8.删除表: drop table 表名 ; \[ 可以加判断:if exists 或 if not exists \] 9.创建临时表: create temporary table 表名 ( 查询语句 ) ; \[ 更改 alter \] 1.修改表名:alter table 原表名 rename 新表名 ; 2.添加新列:alter table 表名 add 列名 数据类型 \[ 约束 \] ; 3.删除旧列:alter table 表名 drop 列名 ; 4.修改旧列:alter table 表名 change 原列名 新列名 数据类型 \[ 约束 \] ; 5.添加主键:alter table 表名 add primary key( 列名 ) ; 6.删除主键:alter table 表名 drop primary key ;\[ 自增主键不能删除 \] 7.添加外键:alter table 外表名 add constraint 外键约束名 foreign key (外键列名) references 主表名 (主键列名) ; 8.删除外键:alter table 外表名 drop foreign key 外键约束名 ; 9.添加复合主键:alter table 表名 add primary key( 列名一,列名二 ... ) ; \[ 插入 insert \] 1.插入单条数据:insert into 表名 \[ ( 列名... ) \] values ( 值... ) ; 2.插入多条数据:insert into 表名 \[ ( 列名... ) \] values ( 值... ),( 值... ) ... ; 3.将查询结果添加到新表中:create table 表名 ( 查询语句 ); \[ 删除 delete \] 1.选择性删除: delete from 表名 where 条件 ;\[ 标识列不会重新编号 \] 2.无条件删除: truncate table 表名 ;\[ 标识列会重新编号 \] \[ 更新 update \] 1.无条件修改: update 表名 set 列名=值,列名=值... ;\[ 整个列都会被修改 \] 2.少条件修改: update 表名 set 列名=值,列名=值... where 条件 ; 3.多条件修改: update 表名 set 列名=值,列名=值... where 条件一 or 或 and 条件二 ... ; 4.数值型修改: update 表名 set 列名=列名+ 或 - 值 where 条件... ;\[ 在xml中,小于< 大于> \] \[ 查询 select \] 1.查询所有列: select \* from 表名 条件... ; 2.查询部分列: select 列名一,列名二... from 表名 条件... ; 3.综合查询: select 列名/表达式/函数/常量 from 表名 where 条件 order by 列名 asc /desc limit ... ;\[ 不全面 \] Tips1:group by 字句可以与 where 条件语句结合使用。当及结合使用时,where 在前,group by 在后。 Tips2:having 只能用在group by 后面,即对分组后的结果进行筛选。(即使用having的前提是分组) Tips3:where 后的条件表达式里不允许使用聚合函数,二having 可以。 Tips4:当一个查询语句同时出现了where, group by, having, order by 的时候,执行顺序和编写顺序是: 1.执行 where... 对全表数据进行筛选,返回第1个结果集。 2.针对第1个结果集使用 group by... 进行分组,返回第2个结果集。 3.针对第2个结果集中的每一组数据进行 select... ,有几组就执行几次,之后返回第3个结果集。 4.针对第3个结果集执行 having... 进行筛选,返回第4个结果集。 5.针对第4个结果集使用 order by... 进行排序。 4.别名查询: select 列名 as 别名 from 表名 where 条件... ; 5.in子查询: select 列名... from 表名 where in (select查询语句 或 值一,值二... ); 6.区间查询: select 列名... from 表名 where 字段名 between 值一 and 值二 ; 7.数值型合并查询: select 列名一 + 列名二 ... as 新列名 from 表名 where 条件... ; 8.空值/非空值查询: select 列名... from 表名 where 列名 is null 或 is not null ; \[ 事务 transaction \] 1.开始事务: start transaction; 或 begin; 2.提交事务: commit; 3.回滚事务: rollback; Tips1:使用事务应该先将自动提交关闭:set autocommit=0; Tips2:查看当前sql语句是否为自动提交:show variables like 'autocommit'; \[ 视图 view \] 1.创建视图: create view 视图名 as select查询语句; 2.删除视图: drop view if exists 视图名; 3.查看视图: select 列名... from 视图名; \[ 索引 index \] 1.创建索引: create index 索引名 on 表名(列名); 2.删除索引: drop index 索引名 on 表名; 3.查看索引: explain select 列名 from 表名; \[ 备份和恢复 \] 1.备份数据库: mysqldump -u 用户名 -p 数据库名 \[表名\] >路径d:\\myschool.sql\[ 在dos窗口中操作且不需要登录数据库\] 2.恢复数据库: mysql -u 用户名 -p 数据库名 \[表名\] 3.source恢复:source 路径; Tips:创建数据库,使用数据库,恢复数据库 4.导出数据:select \* from into outfile '路径'; 5.导入数据:load data infile '路径' into table 表名;
还没有评论,来说两句吧...