MySQL一些学生常用命令操作
1.Win+R 打开运行窗口,输入cmd,敲回车-----------------------------------------------------mysql登陆
2.net start mysql 打开mysql服务器
3.mysql -h localhost -u root -p
exit---------------------------------------------------------------------------------退出
Exit 或 quit------------------------------------------------------------------------------------mysql退出
set names set names utf8------------------------------------------------------------------------中文
create database 库名; ------------------------------------------------------------------------ 建立数据库
show databases; ----------------------------------------------------------------------------查看数据库
show create database 库名; ---------------------------------- ------------------------------ 查看单个数据库
alter database 库名 def ault character set 编码 collate 编码_bin; ---------------------------修改数据库
drop database 库名; -------------------------------------------------------------------------删除数据库
use 库名--------------------------------------------------------------------------------------- 打开数据库进行操作
create table 表名(字段名1 数据类型1 [约束条件],字段名2 数据类型2 [约束条件]);----------- 加你一个新的数据表
show create table 表名;/desc 表名;show tables查看当前库下所有表-------------------------------- 查看数据表
alter table 旧表名 rename to 新表名;---------------------------------------------------------修改表名
alter table 表名 change 旧字段名 新字段名 新数据类型;--------------------------------------修改字段名
alter table 表名 modify 字段名 数据类型;-----------------------------------------------------修改字段的数据和类型
alter table 表名 add 新字段名 新数据类型 [约束条件] [first/after 已存在字段名]--------------添加字段
alter table 表名 drop 字段名;------------------------------------------------------------------删除字段
alter table 表名 modify 字段名1 数据类型 first/after 字段名2;----- ---------------------------修改字段排列位置
drop table 表名;-------------------------------------------------------------------------------删除数据表
字段名 数据类型 primary key;-----------------------------------------------------------------单子段主键
primary key (字段名1,字段名2,------字段名n);-----------------------------------------------多字段主键
字段名 数据类型 not null;- ---------------------------------------------------------------------非空约束
字段名 数据类型 unique;------------------------------------------------------------------------唯一约束
字段名 数据类型 default 默认值;----------------------------------------------------------------默认约束
字段名 数据类型 auto_increment;--------------------------------------------------------------字段值自动增加
create [nuique/fulltext/spatial] index 索引名 on 数据表名 字段名 [长度]([asc/desc]);--------建表以后创建索引
create table 表名(字段1 字段类型 [unique/fulltext/spatial] index/key [别名](字段名 [长度]) [asc/desc];--建表时建立索引
show create table 数据表名;--------------------------------------------------------------------查看索引
alter table 表名 drop index 索引名/;drop index 索引名 no 表名;------------------------------删除索引
insert into 表名 (字段名1字段名2,----) values (值1,值2,---);------------------------------------添加数据
insert into 表名values(值一,之二,);---------------------添加数据
select * from 数据表名;-------------------------------------------------------------------------查询表数据记录
update 数据表 set 需更改字段记录名='更改内容' where 字段名='满足条件' ;----------------对指定内容数据进行修改
delete from 表名 where 需删除内容表达式;----------------------------------------------------删除部分数据
delete from 数据表名;--------------------------------------------------------------------------删除所有数据
16、给xscj表创建外键约束,外键名为fk_id,主表为xsxx.
alter table 表名 add constraint 外键名 foreign key(外键字段名) references 外表表名(主键字段名);
17,Create [nuique | fulltext | spatial ] index 索引名 on 数据表名(字段名 [(长度)]) [asc |desc] );-------------------------------------------建立索引
5、给xsxx表中电话列前5个字符创建降序索引,索引名为tel.将命令截图并粘到题目下方
create index tel on xsxx (dianhua(5) desc);
18,alter table 数据表名 drop index 索引名;------------------------------------------------------删除索引;
drop index 索引名 on 数据表名;
19。 Show create table 数据表名;----------------------------------------------------查看索引
v
20,Update 数据表名 set 字段1=值1 [,字段名2=值2……]where(条件)---------------------------更新数据;
主键约束 primary key 可以唯一标识表中的记录。
非空约束 not null 设置了非空约束以后,该列的值不能为空。
唯一约束 unique 设置了唯一约束以后,该列不能有重复值。
默认约束 default 默认值 某列设置默认约束后,该列如果没有插入值,系统会自动填充默认值
auto_increment 自增
单表查询:
select * from 表名 as 别名;-----------------------------------------------------------建立别名
select [distinct] * | 字段名1,字段名2…… from 数据表名 [where 条件表达式] [group by 字段名 [having 条件表达式2]] [order by 字段名 [asc | desc] ]
count() 计数,sum() 求和,avg() 平均值,max() 求最大值,min() 求最小值------------------------函数
select * from 表名 [as] 别名; -------------------------------------------------------------------表的别名;
select 字段名 as 别名 from 表名;--------------------------------------------------------------字段别名
alter table 表名 add constraint fk_id foreign key(外键字段名) references 外表表名 (主键字段名);------表添加主键约束
alter table 表名 drop foreign key 外键名;----------------------------------------删除外键
delete from 表名 where 需删除记录条件;------------------------------------------------------------定向删除记录;
delete from 表名;----------------------------------------------------------------------------------------删除表中的记录但不删除表
select * from 表1 cross join 表2;---------------------------------------------------------------交叉链接;
select 查询字段 from 表1 [inner] join 表2 on 表1.关系字段=表2.关系字段;------------------------------内连接
select 所查字段 from 表1 left/right [outer] join 表2 on 表1.关系字段=表2.关系字段 where 条件;-----------左右连接
set session transaction isolation level 隔离级别;--------------------------------------------------事务的四种隔离级别
Delimiter // ------------------delimiter 将mysql 结束符改 // delimiter 与结束符用空格间隔;
Create procedure 过程名( )----------------------过程中的SQL语句 begin开始 用end 结束
begin------------------------------------开始
sql语句1;
sql语句2; 存储过程的创建
sql语句3;
end //-------------------------结束
delimiter ; -------------------------将结束符改为;
call 过程名( );---------------------------------------------------------------------调用存储过程
create [or replace] view 视图名 [(字段名列表)] as select 语句;----------------------创建视图;
or replace作用 替换原有的视图;
desc 视图名;/show create view 视图名---------------------------------------------------查看视图
alter view 视图名 as select 语句;-------------------------------------------------修改试图
update 视图名 set 字段名=值 where 条件表达式-------------------------------------------更新视图
insert into 视图名 valuses (值1,值2,-),(值1,值2,-);--------------------------------添加视图中的记录
delete from 视图名 where 条件表达式--------------------------------------------------------对视图的指定内容删除
drop view 视图名;---------------------------------------------------------------------------删除视图
mysqldump -u 用户名 -p 密码 数据库名[数据表名1 [数据表名2]…… ]>路径\备份文件名.sql-------备份单个数据库
mysqldump -u用户名 –p密码 --database 数据库1 [数据库2 ……]>路径\备份文件名.sql-------备份多个数据库
mysqldump –u用户名 –p密码 --all-databases>路径\备份文件名.sql---------------------------备份所有的数据库
mysql -u用户名 –p密码 [数据库名] <路径\备份文件名.sql ------------------------------还原数据库
grant 权限列表 on 数据库.数据表 to '用户名'@'主机名' identified by '密码';------创建新的用户
主机名 一般写 'localhost'
drop user '用户名'@'主机名';------------------------------------------- 删除用户
update mysql.user set password=password('新密码') where user='用户名' and host='主机名';--------给普通用户更改密码
revoke 权限列表 on 数据库.数据表 from '用户名'@'主机名';------------------------------收回权限
Revoke all privileges , grant option from '用户名'@'主机名';-----------------------------收回所有的权限

浙公网安备 33010602011771号