mysql 测试语法学习_mysql语法总结和练习

#增加用户#格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by '密码'/*如,增加一个用户user1密码为password1,让其可以在本机上登录, 并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入mysql,然后键入以下命令: grant select,insert,update,delete on *.* to user1@localhost Identified by "password1"; 如果希望该用户能够在任何机器上登陆mysql,则将localhost改为"%"。 如果你不想user1有密码,可以再打一个命令将密码去掉。 grant select,insert,update,delete on mydb.* to user1@localhost identified by ""; */

grant all privileges on wpj1105.* to sunxiao@localhost identified by '123';   #all privileges 所有权限

show grants for user1@'localhost'; #查看用户具有哪些权限。

revoke select on ucyzw.* from user1@'localhost'; #取消用户授权。

#----------------------------#-----MySql数据库操作基础-----

#显示数据库show databases;

#判断是否存在数据库wpj1105,有的话先删除drop database if exists wpj1105;

#创建数据库create database wpj1105;

#删除数据库drop database wpj1105;

#使用该数据库use wpj1105;

#显示数据库中的表show tables;

#先判断表是否存在,存在先删除drop table if exists student;

#创建表create table student(id int auto_increment primary key,name varchar(50),sex varchar(20),date varchar(50),content varchar(100))default charset=utf8;

查看表结构:

desc:

#删除表drop table student;

#查看表的结构describe student;  #可以简写为desc student;

查看索引:

show indexes from student;

#插入数据insert into student values(null,'aa','男','1988-10-2','......');insert into student values(null,'bb','女','1889-03-6','......');insert into student values(null,'cc','男','1889-08-8','......');insert into student values(null,'dd','女','1889-12-8','......');insert into student values(null,'ee','女','1889-09-6','......');insert into student values(null,'ff','null','1889-09-6','......');#查询表中的数据select * from student;select id,name from student;

#修改某一条数据update student set sex='男' where id=4;

#补充update数据库多表查询的赋值(语句要掌握。有时候线上数据库要修复):

UPDATE Track,MV

SET Track.is_show=MV.is_show

WHERE Track.trkid=MV.mvid and trkid<6  #多表查询的时候注意也要找相等关系

#删除数据delete from student where id=5;

# and 且select * from student where date>'1988-1-2' and date

# or 或select * from student where date'1988-12-1';   #betweenselect * from student where date between '1988-1-2' and '1988-12-1';

#in 查询制定集合内的数据select * from student where id in (1,3,5);

#排序 asc 升序  desc 降序select * from student order by id asc;

#分组查询 #聚合函数 select max(id),name,sex from student group by sex;

select min(date) from student;

select avg(id) as '求平均' from student;

select count(*) from student;   #统计表中总数

select count(sex) from student;   #统计表中性别总数  若有一条数据中sex为空的话,就不予以统计~

select sum(id) from student;

#查询第i条以后到第j条的数据(不包括第i条)select * from student limit 2,5;  #显示3-5条数据

#巩固练习create table xiaoluo

(id int auto_increment primary key,

name varchar(50) not null,

sex varchar(50) not null,

age int unsigned,   #年龄不能为负值

sno int unique)default charset=utf8;

drop table xiaoluo;desc xiaoluo;

insert into xiaoluo (id,name,sex,age,sno) values (null,'涛哥','男',68,1);insert into xiaoluo(id,name,sex,age,sno) values (null,'aa','男',68,2);insert into xiaoluo(id,name,sex,age,sno) values (null,'平平','男',35,3);...

select * from xiaoluo;

#修改数据 update xiaoluo set age=66 where id=2;update xiaoluo set name='花花',age=21,sex='女' where id=2delete from xiaoluo where age=21;

#常用查询语句select name,age ,id from xiaoluoselect * from xiaoluo where age>40 and age<60;  #andselect * from xiaoluo where age<40 or age<60;  #orselect * from xiaoluo where age between 40 and 60 #betweenselect * from xiaoluo where age in (30,48,68,99);     #in 查询指定集合内的数据select * from xiaoluo order by age desc;      #order by (asc升序 des降序)

#分组查询select name,max(age) from xiaoluo group by sex;  #按性别分组查年龄最大值#聚合函数select min(age) from xiaoluo;select avg(age) as '平均年龄 ' from xiaoluo;select count(*) from xiaoluo;  #统计表中数据总数select sum(age) from xiaoluo;

#修改表的名字#格式:alter table tbl_name rename to new_namealter table xiaoluo rename to a; #表结构修改create table test(id int not null auto_increment primary key, #设定主键name varchar(20) not null default 'NoName', #设定默认值department_id int not null,position_id int not null,unique (department_id,position_id) #设定唯一值);

#修改表的名字#格式:alter table tbl_name rename to new_namealter table test rename to test_rename;

#向表中增加一个字段(列)#格式:alter table tablename add columnname type;/alter table tablename add(columnname type);alter table test add  columnname varchar(20);

#修改表中某个字段的名字alter table tablename change columnname newcolumnname type;  #修改一个表的字段名alter table test change name uname varchar(50);

select * from test;

#表position 增加列testalter table position add(test char(10));#表position 修改列testalter table position modify test char(20) not null;#表position 修改列test 默认值alter table position alter test set default 'system';#表position 去掉test 默认值alter table position alter test drop default;#表position 去掉列testalter table position drop column test;#表depart_pos 删除主键alter table depart_pos drop primary key;#表depart_pos 增加主键alter table depart_pos add primary key PK_depart_pos(department_id,position_id);

#用文本方式将数据装入数据库表中(例如D:/mysql.txt)load data local infile "D:/mysql.txt" into table MYTABLE;

#导入.sql文件命令(例如D:/mysql.sql)source d:/mysql.sql;  #或者 mysql -uroot daname

二进制日志的用法:

备份二进制日志可以使用:

mysqlbinlog mysql-bin.xxx > xx.sql(起始点还原)

或者根据时间点:

mysqlbinlog --start-datetime="2014-12-13 17:00:00" --stop-datetime="2014-12-13 19:20:00" /home/mysql/data/mysql-bin.000008 >/home/test/uclog2.sql

当主从同步出现时间偏移报错可以根据上面步骤,找出出错点再同步:

MASTER_HOST='192.168.1.8',MASTER_USER='rsync',MASTER_PASSWORD='123456',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000008',MASTER_LOG_POS=272267720(找出错的时间点cat uclog2.sql)

#

#mysqlbinlog

--start-position    起始位置

--stop-position    结束位置

--start-datetim    起始时间

--stop-datetim    结束时间

mysql被设置无密码登录(find / -name my.cnf):

第一:配置文件或者启动文件里面有:--skip-grant-tables 选项。

第二:或者在配置文件里面加入了密码:

[client]

root=xiaoluo

password=xsdasy892

mysql客户端程序使用:

mysqladmin -uroot -p -h192.168.10.1 ping #测试对方是否在线

mysqladmin processlist  #查看用户并发线程

mysqladmin status #查看数据库状态,包括满查询

#shell 允许mysql语句的时候直接 mysql -e "xxx(mysql语句)" 就可以编写脚本。

事物:mysql有事务隔离级别,根据数据的一致性要求。可以对隔离级别优化,事物默认提交:

START TRANSACTION;  #启动事物;

ROLLBACK  ;#出错的时候事物回滚:已经提交了的事物是不能回滚的

COMMIT ;  #事务提交

变量查看事务是否默认提交,1是默认提交,为了减少iO性能的消耗,建议关闭自动提交。

set autocomit=0

mysql> select @@autocommit;

+--------------+

| @@autocommit |

+--------------+

|            1 |

+--------------+

1 row in set (0.00 sec)

锁机制和查看:

mysql>

show status like 'Table%'

+----------------------------+-------+

| Variable_name              | Value |

+----------------------------+-------+

| Table_locks_immediate      | 1147  |

| Table_locks_waited         | 0     |

| Table_open_cache_hits      | 0     |

| Table_open_cache_misses    | 0     |

| Table_open_cache_overflows | 0     |

+----------------------------+-------+

5 rows in set (0.00 sec)

如果Table_locks_waited值比较高,说明是已经有锁表。

二、通过show

processlist查看,要是存在比较多的锁,临时的解决方案是直接把idkill掉。但是不是最终办法,要查找到是什么原因引起的锁。是否是硬件问题。或者是程序本身问题,还有就是那条语句满查询引起的锁。然后慢慢优化。可以加缓存。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值