✨ 目录 🎈 初级语法之登录、库操作、表操作、数据操作,索引🎈 高级语法之主键、试图、变量、流程、函数、存储过程🎈 查询语法之联合查询、子查询、链接查询
Mysql –u用户名 [-h服务器IP地址] [-P端口] -p密码
show databases;
create database 数据库名;
use 数据库名称;
explain sql语句
drop database 数据库名;drop database if exists 数据库;
show tables;show tables in 数据库名;
create table 表名( -- code)engine=innodb comment '用户表';
drop table 表名;drop table if exists 表名;
describe 表名;describe 表名\G;-- 推荐简写方式desc 表名;
show create table 表名;show create table 表名\G;
alter table 表名 属性名=新属性值;alter table test engine=MyISAM;
rename table [库名.]旧表名 to [库名.]新表名;
alter table 表名 add 新字段名 字段类型 [字段属性列表];
alter table 表名 drop 字段名
alter table 表名 change 旧字段名 新字段名 新字段类型 [字段属性];
alter table 表名 modify 字段名 字段新类型 [字段新属性列表];
insert into 表名 values (字段1值, 字段2值, … 字段n值);insert into 表名 (字段1, 字段2, … 字段n) values (字段1值, 字段2值, … 字段n值);
select 字段列表 from [库名.]表名 [where 查询条件];
update 表名 set 字段1=字段1新值, 字段2=字段2新值, …. 字段n=字段n新值 where 条件语句;
delete from 表名 where 条件语句;
truncate [库名.]表名;
Alter table 表名 modify 字段名 字段类型 auto_increment
alter table 表名 add primary key(字段名称)
Alter table 表名 drop primary key;
alter table 索引名 modify id int unsigned;
create unique index 索引的名称 on 表名(字段名称)
alter table 表名 drop index 索引的名称
create index 索引的名称 on 表名(字段名称);
alter table 表名 drop index 索引的名称
alter table 表名 add index 索引名称(字段名称(长度))
alter table 表名 drop index 索引名称
Innodb
引擎的、无法删除
的NULL
值-- 创建外键create table tableName( foreign key (外键字段) references 主表 (主表关联字段) [删除时执行语句] [更新时执行语句];)engine=Innodb charset=utf-8;-- 删除外键alter table 表名 drop foreign key 外键标识;-- 删除索引alter table 表名 drop index 索引字段名;-- 添加外键alter table 表名 add foreign key (外键字段) references 主表 (主表关联字段) [删除时执行语句] [更新时执行语句];
select
语句,充当一个 变量
角色SQL
语句的形态存在的一般不会直接操作视图,保持视图数据的干净
mysql
后,在不同的黑窗口客户端中依然能够使用重启之前创建的视图可以
在多个客户端进程中通用,视图必须在 选择数据库之后
才能进行操作-- 创建视图create view 视图名称 as (查询SQL语句);-- 删除试图,删除的时候不会影响源表drop view 视图名称;drop view if exists 视图名称;-- 查看数据库中所有视图select * from infORMation_schema.views;
mysql
变量分类:系统变量
、用户变量
、局部变量
show global variables;
show session variables;
set @@global.变量名=变量值;
,重启数据库后,set
方式修改的全局变量将会被重新初始化set @@session.变量名=变量值;
,重启数据库之后,set
方式修改的会话变量的值将会被还原set @变量名=变量值;
declare 变量名 变量数据类型 [default 默认值];
mysql
结束符:delimiter 符号
-- 定义用户变量set @变量名=变量值;set @变量名=变量值,@变量名=变量值;-- 获取用户变量select @变量名,@变量名;-- 通过select语句定义用户变量,将查询结果中的最后一条数据的name,age,email分别赋值给不同的用户变量select @var1=name,@var2=age,@var3=email from table where 1;-- 定义局部变量示例declare var1 int default 0;
if分支
、while循环
if 条件语句 thenif结构体[elseif 条件语句 thenelseif结构体]elseelse结构体end if; -- if分支示例if @var > 100 then insert into values('');else insert into values('');end if;-- while循环while 条件 do循环结构体end while;
-- 定义函数create function 函数名(形参1 形参1数据类型, 形参2 形参2数据类型,…, 形参n 形参n数据类型)returns 预估的函数返回值数据类型[begin]函数的结构体[end]-- 使用函数select 函数名(实参列表);-- 删除函数drop function 函数名;
in
:只进不出(只传值不传名) 在存储过程中修改值将不影响外部变量out
:只出不进(只传名不传值) 在存储过程中无法获得值inout
:能够进也能够出(即传名也传值) 最常用的类型-- 创建存储过程create procedure 存储过程名(数据传递类型 形参1 形参1数据类型, 数据传递类型 形参2 形参2数据类型,…, 数据传递类型 形参n 形参n数据类型)begin存储过程结构体end-- 调用存储过程call 存储过程名(实参列表);-- 删除存储过程drop procedure 存储过程名;
联合查询
:联合两条查询数据-- 如果存在相同的数据,则保留其中一条(select * from table where id>200)uNIOn(select * from table where id>200);-- 如果存在相同的数据,则全部保留(select * from table where id>200)union all(select * from table where id>200);
标量子查询
:本条查询语句的查询条件 是 另一个查询语句查询出来的单个值-- 查询id为10的老师所带的所有班级信息select * from class where teacher_name = ( select name from teacher where id = 10);
列子查询
:本条查询语句的查询条件 是 另一个查询语句查询出来的一列结果-- 查询id为10和30的老师所带的所有班级select * from class where teacher_name in ( select name from teacher where id = 10 or id=30);
行子查询
:本条查询语句中的查询条件 是 另一个查询语句所查出来的一行记录select * from class where (teacher_name,teacher_num)=( select name,num from teacher where id = 10)
表子查询
:本条查询语句所查询的表 是 另一个查询语句查询出来的一个虚拟表结果集-- 蠕虫复制insert into table values(select * from teacher where 1);
exists
查询-- 查询出table1和table2表name字段同时存在相同值的数据select * from table1 where exists (select * from table2 where table1.name == table2.name);
select * from table1 inner join table2 on table1.name = table2.name;-- 内连接的简写方式可以省略inner关键字select * from table1 join table2 on table1.name = table2.name; -- 在内连接中,where关键字可以代替on关键字select * from table1 join table2 where table1.name = table2.name; -- using演示案例 两个表存在相同的name字段且值相同才连接select * from table1 join table2 on using(name);
-- 如果内连接不指定条件,就是交叉连接。select * from table1 inner join table2;
select * from table left outer join table2 on table.name = table2.name;-- 简写select * from table left join table2 on table.name = table2.name;
select * from table right outer join table2 on table.name = table2.name;-- 简写select * from table right join table2 on table.name = table2.name;
-- 左外连接 和 右外连接 联合起来(select * from table right join table2 on table.name = table2.name)union(select * from table right join table2 on table.name = table2.name);
-- 自然内连接 等同于 内连接使用using条件select * from table join table2 using(name);-- 等同于select * from table natural join table2; -- 自然左外连接select * from table natural left join table2;-- 自然右外连接select * from table natural right join table2;
来源地址:https://blog.csdn.net/weixin_41635750/article/details/126930768
--结束END--
本文标题: mysql 的语法操作命令从入门到精通
本文链接: https://www.lsjlt.com/news/374226.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0