关于数据库的一些常用命令(以命令行形式)

mysql -u账号 -p密码              -->命令行形式登录mysql ;例如;mysql -uroot -proot; 以账号root和密码root登录mysql.注意用户名前可以有空格也可以没有空格,但是密码前必须没有空格,否则让你重新输入密码。
show create database 库名;   -->查看建库语句;例如:show create database web_test;结果显示为create database 'web_test' /*!40100 default character set utf8 */
show databases;  -->显示所有的数据库名称;
show databases;  --> (空格)--(空格)注释说明 例如:show databases; -- 显示所有的数据库名称  
create database 库名 ;  -->创建库;例如:create database dd ; 表示创建了一个名叫dd的数据库
create database if not exists 库名;   -->如果不存在某数据库,就创建它(如果存在,就不创建。我们知道如果某个数据库已经存在,我们再去创建它的话,会报错,这种写法避免了报错情况的发生)
create database 库名 character set gbk;  -->创建一个字符集为gbk的数据库;例如:create database dd character set gbk;表示创建了一个字符集为gbk的数据库dd
create database if not exists 库名 character set gbk;  -->如果不存在字符集为gbk的某数据库,我们就创建它
alter database 库名 character set utf8;  -->将某数据库的字符集修改为utf8
drop database 库名;   -->删除某数据库 
drop database if exists 库名;   -->如果存在某数据库,就删除它(如果不存在,就不删除。我们知道如果某个数据库不存在的话,我们再去删除它,会报错,这种写法避免了报错情况的发生)
use 库名;  -->用于选择当前的任何现有数据库。一般在开始操作之前,需要选择一个将执行操作的数据库。
select database();  -->查询当前所使用的数据库;执行该语句后,会显示当前所使用的数据库名称

show tables;   -->显示当前数据库下所有的表;
create table 表名(

列名称1 数据类型,

列名称2 数据类型,

列名称3 数据类型,

......

);   -->创建数据库中的表
create table 表名 like 被复制的表名;  -->复制表结构,并未复制表中的数据
desc 表名;   -->查询表结构
drop table 表名;  -->删除整张表
drop table if exists 表名;   -->如果存在某表,就删除它(如果不存在,就不删除它,我们知道如果不存在某表,还要去删除它,会报错,这样写避免了报错情况的发生)
alter table 表名 rename to 新的表名;   -->修改表名 
alter table 表名 character set 字符集名称;  -->修改某表的字符集
alter table 表名 add 列名 数据类型;  -->添加一列,即在某表中添加一个字段;例如:altert table mm add cc varchar(10);在mm表中添加了一个字段cc,并设置字段类型为varchar
alter table 表名 change 列名 新列名 新数据类型;  -->更改表中的某个字段名
alter table 表名 modify 列名 新数据类型;  -->更改表中的某个字段的数据类型
alter table 表名 drop 列名;  -->删除表中的某一列

DML:增删改表中数据
insert into 表名(列名1,列名2,...列名n) values (值1,值2,...值n); 
-->往某表中插入数据(只是表中一部分字段有值) 
insert into 表名 values (值1,值2,...值n);  -->往某表中插入数据(每个字段都应该有值)
delete from 表名;  -->删除表中的所有数据
delete from 表名 where ...;  -->删除表中某一条的数据
truncate table 表名;  -->先删除表,再创建一张一样的空表;同语句:delete from 表名 相比,效率更高
update 表名 set 列名1 = 值1,列名2 = 值2 where...;  -->修改表中某一条数据的字段值,如果不加where,则修改整张表的一列的值

DQL:查询表中记录
select * from 表名; 
-->查询表中的所有数据
select 字段1(,字段2) from 表名;  -->查询表中某字段的所有值,无论是否有重复值
select distinct 字段 from 表名;  -->查询表中某字段的值,去除重复值
select 字段1,字段2,字段1+字段2 ((as)字段3) from 表名;  
select 字段1,字段2,ifnull(字段1,0)+ifnull(字段2,0) from 表名; -- ifnull(表达式1,表达式2)  表达式1:哪个字段需要判断是否为null  如果该字段为null后的替换值;
select * from 表名 where 字段(判断符号 >=  <=  !=  <>  =)值; 
-->例如: select * from student where age=20;
select * from 表名 where 字段 between 值1 and 值2;  -->查询符合某个字段值在值1和值2之间的条件的每条数据
select * from 表名 where 字段 判断符 值 and 字段 判断符 值;  -->例如:select * from student where age >= 20 and age <= 30;
select * from 表名 where 字段 in(值,值,值);  -->例如:select * from 表名 where age =22 or age =18;
select * from 表名 where 字段 not in(值,值,值);
select * from 表名 where 字段 is null;
select * from 表名 where 字段 is not null;
select * from 表名 where 字段 like '%(_)字符%(_)';select * from 表名 where where name like '_化%';
selcet * from 表名 where name not like ...
select * from 表名 where name like '%德%';_ :单个任意字符  % :多个任意字符

order by 字段 排序方式(默认升序ASC),降序DESC 
order by 字段1 排序方式1(默认升序),字段2 排序方式2
select 分组字段1,分组字段2 from 表名 group by 分组字段1,分组字段2 having 判断

select * from 表名 limit 起始索引 ,数目;
alter table 表名 modify 字段 类型 not null; 表后添加非空约束
alter table 表名 modify 字段 类型; -- 删除非空约束
alter table 表名 modify 字段 类型 unique;表后添加唯一约束

alter tabel 表名 drop index 字段;  --  删除唯一约束
alter table 表名 modify 字段 类型 primary key   添加主键

alter table 表名 drop primary key  删除主键
alter table 表单 modify 字段 类型 auto_increment  添加自动增长

alter table 表名 modify 字段 类型;删除自动增长
delete from 表单 where ...
constraint 外键名称 foreign key (外键字段名称) references  主表名称(主表字段名称)   添加外键
alter table 表名 drop foreign key 外键名;
alter table 表名 add  constraint 外键名称 foreign key (外键字段名称) references  主表名称(主表字段名称) 
alter table 表名 add  constraint 外键名称 foreign key (外键字段名称) references  主表名称(主表字段名称)  on update  cascade  on delete cascade

内连接查询
隐式  使用where消除无用数据
显式  select 字段 from 表名1 inner join 表名2 on 条件

外连接查询
左外链接 select 字段 from 表名1 left [outer] join 表名2 on 条件   查询的是左表所有数据以及其交集部分
右外连接 select 字段 from 表名1 right [outer] join 表名2 on 条件
子查询

create user  '用户名'@'主机名'  identified by '密码'
drop user '用户名'@'主机名'
select * from user
use mysql
update user set password = password('新密码') where user = '用户名'
set password for '用户名'@'主机名' = password('新密码')

net stop mysql
mysqld --skip-grant-tables  使用无验证方式启动mysql服务(一窗口打开输入命令,这时亮标会暂停)
mysql(另一窗口打开输入命令,然后回登陆进去)
use mysql

改密码
关闭窗口
手动结束mysqld.exe 进程
重新登陆

show grants for '用户名'@'主机名'    查询权限
grant 权限列表  on 数据库名.表名  to  '用户名'@'主机名'    -- 授权
grant all on *.* to ...
revoke  权限列表 on 数据库名.'表名'  from '用户名'@'主机名'    -- 撤销权限

select ...  from ...  where ...  group by ... having ...  order by ...  limit...

如何查看mysql的安装目录

show variables like "%char%";

如何查看mysql数据库物理文件存放位置

show global variables like "%datadir%";

MySQL之mysqldump的使用

https://www.cnblogs.com/markLogZhu/p/11398028.html

显示表的相关信息:

show table  status like 'xxx' \G

 

























 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值