您现在的位置是:首页> 网站开发> MySql

MySql常用命令语法总结

  • 3818人已阅读
  • 时间:2019-03-26 12:02:00
  • 分类:MySql
  • 作者:祥哥

#1.连接登陆数据库
mysql -h localhost -u root -p #主机,用户名,回车输入密码

#2.修改数据库密码方法一:
mysql -h localhost -u root -p
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

#3.修改数据库密码方法二:
mysql -h localhost -u root -p
use mysql
UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';

#4.忘记数据库密码方法三
mysqld_safe --skip-grant-tables&
mysql -u root mysql
UPDATE user SET password=PASSWORD("new password") WHERE user='root';
FLUSH PRIVILEGES;

#5.库级知识
#5.1查看库
show databases;
#5.2选择库
use dbname;
#5.3删除库
drop database dbname;
#5.4创建库
create database dbname charset utf8;

#6.表级知识
#6.1查看表
show tables;
#6.2查看表结构
desc tableName;
#6.3查看建表的过程
show create table tableName;
#6.4创建表
create table tbName(
列名称1 列类型 [列参数] [not null default],
....列2...
...
列名称N.......
)engine myisam/innodb charset utf8/gbk;

#例
create table eg_card (
cid smallint unsigned not null primary key auto_increment,
uid smallint unsigned not null,
card varchar(128) not null default '',
bank char(20) not null default '',
valid smallint(4) unsigned zerofill not null default 0,
three smallint(3) unsigned zerofill not null default 0,
state tinyint(2) unsigned zerofill not null default 0,
repay tinyint(2) unsigned zerofill not null default 0,
money char(10) not null default 0,
fixed char(10) not null default '',
cpwd varchar(64) not null default '',
jpwd varchar(64) not null default '',
wname char(20) not null default '',
wpwd varchar(128) not null default '',
yk enum('Y','N') not null default 'Y',
view enum('V','D') not null default 'V',
unique key card(card)
)engine=innodb charset=utf8;
#6.5删除表数据
delete from tablename where 条件;#如果不加where条件将会删除表中所有的数据
#6.6清空表数据与删除表数据有区别
truncate tableName; #与delete的区别是,这个是完全清空,自增长的会从头开始而delete不会
#6.7删除表
drop table tableName;
#6.8查询表信息也可以看出是否是视图
show table status ;
show table status where name='' \G;

#7.对表操作
#7.1修改表之增加列
alter table tbName
add 列名称 列类型 [列参数] after tbName
#add后与建表相同 如果不写after将会在表最后增加列,否则在tbName后

#7.2修改表之修改列
alter table tbName
change 旧列名 新列名 列类型 [列参数] 
#旧列名后的和建表声明列相同

#7.3修改表之减少列
alter table tbName
drop 列名称;

#7.4修改表之增加主键
alter table tbName add primary key(主键所在列名);

#例:修改goods表增加主键在id上
alter table goods add primary key(id);

#7.5修改表之删除主键
alter table tbName drop primary key;

#7.6修改表之增加索引
alter table tbName add [unique/fulltext] index 索引名(列名);

#7.7修改表之删除索引
alter table tbName drop index 索引名;

#8.插入数据
#插入指定列
insert into 表名(col1,col2,col3....) values(val1,val2,val3.....);

#插入所有列
insert into 表名 values(,,,,,,,);

#一次插入多行
insert into 表名 values 
(val1,val2...),
(val1,val2...),
(val1,val2...);

#9.修改数据
update tableName
set
col1=newval1,
col2=newval2,
...
...
coln=newvaln
where 条件;


#10 查询数据
select 哪些列 from 哪张表 where 什么条件;

#10.1条件查询
where 
a.条件为真取出
b.比较运算符 = , != , < , > , = , in(在某集合内) , between and (在某范围内)

例:in (1,4) 第1或第4 in和or是一样的
   between  1 and 4  大于等于1且小于等于4 和and一样

c.逻辑运算符 NOT 或 ! , OR 或 || , AND 或 &&
d.like , not like ('%'匹配任意多个字符,'_'匹配任意单个字符)
e. is null , is not null

#10.2分组查询
group by
一般是配合5个聚合函数使用:max(),min(),sum(),avg(),count()

#10.3筛选
having

#10.4排序

order by 列名 升序
order by 列名 desc 除序

#10.5限制

limit 跳过几行, 取几行

#11连接查询

#11.1左连接
left join ... on
#左连接以左表数据为准,左表全取出,没有匹配的补NULL
#得到的结果为左表+左右的交集
#用法举例
select a.id,a.name,b.id,b.name from a left join b on a.id=b.id

#11.2 内连接
inner join ...on
#得到左表和右表的交集
select a.id,a.name,b.id,b.name from a inner join b a.id=b.id

#11.3 右连接
right join ...on
#得到左右的交集+右表
select a.id,a.name,b.id,b.name from a right join b a.id=b.id


#左右连接都是以在左边的数据为准,沿着左表查右表
#内连接是以两张表共同数据为准,也就是左右连接之交集

#11.4 union 查询
# union 把2条或多条合并成一个结果集 并集
# union查询语句必需满足1个条件:各语句取出的列数相同
# 列名称未必要一致,列名称会使用第1条SQL的列名称.
# 注意:使用union时,完全相等的行,将会被合并.
# 合并是比较耗时的操作,一般不让union进行合并,使用union all可以避免合并
# union的子句中,不用写order by,失云意义.可以在最终结果集order by
# 举个例子,假设A表和B表列相等
select * from a union select * from b;
select * from a union all select * from b;

#114.5

#12.子查询
#12.1where子查询
#where子查询是在where后作为条件表达式一部分
#例 
select * from tablea where cola = (select colb from tableb where ....);

#12.2from子查询
#from型子查询是内层sql查询结果,作为一张表,供外层的sql语句再次查询
#例
select * from (select * from ...) as tableName where ....

#12.3 exists子查询

#13字符集问题

客户端sql编码 character_set_client
服务器转化后的sql编码 character_set_connection
服务器返回给客户端的结果集编码 character_set_results
快速把上述三个变量设为相同值 : set names 字符集;

#14.存储引擎
1.myisam 速度快 不支持事务 回滚
2.Innodb 速度慢 支持事务 回滚

1)开启事务
start transaction

2)运行SQL

3)提交,同时生效/回滚
commit/rollback


#15索引类型
primary key 主键索引
index 普通索引
unique index 唯一索引
fulltext index 全文索引


#16视图
view 
#16.1 建视图
create viewname view as SQL语句
#16.2 PHP中如果视图不存在建视图
CREATE OR REPLACE VIEW dz as  SQL语句


#17数据库备份与恢愎
#17.1备份数据库egxt到D盘目录egxt.sql文件
mysqldump -hlocalhost -uroot --default-character-set=utf8 -p egxt > d:/egxt.sql

#17.2备份到当前目录指BIN 备份egxt数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
mysqldump --add-drop-table -hlocalhost -uroot --default-character-set=utf8 -p egxt > egxt.sql

#17.3 直接将egxt数据库压缩备份,备份到d盘目录
mysqldump -hlocalhost -uroot --default-character-set=utf8 -p egxt | gzip > d:/egxt.sql.gz

#17.4 备份mysql数据库某个表,此例备份table1表和table2表.备份到linux主机的/home下
mysqldump -hlocalhost -uroot --default-character-set=utf8 -p egxt table1 table2 > /home/egxt.sql

#17.5同时备份多个mysql数据库
mysqldump -hlocalhost -uroot --default-character-set=utf8 -p --databases egxt egxt1 > mysql.sql

#17.6仅仅备份数据结构,同时备份myweb数据库和myweb2数据库
mysqldump --no-data -hlocalhost -uroot --default-character-set=utf8 -p --databases myweb myweb2 > mysql.sql

#17.7备份服务器上所有数据库
mysqldump --all-databases -hlocalhost -uroot --default-character-set=utf8 -p > mysql.sql

#18还原数据库
#18.1还原egxt.sql的数据库
mysql -hlocalhost -uroot --default-character-set=utf8 -p egxt < egxt.sql

#18.2还原压缩的mysql数据库
gunzip < egxt.sql.gz | mysql -hlocalhost -uroot --default-character-set=utf8 -p egxt

#18.3 将数据库转移到新服务器,此例为将本地数据库myweb复制到远程数据库名为serweb中,其中远程数据库必须有名为serweb的数据库
mysqldump -hlocalhost -uroot --default-character-set=utf8 -p myweb | mysql --host=***.***.***.*** -u 数据库用户名 -p -C serweb


Top