mysql专业语句详解
下文内容主要给大家带来mysql专业语句详解,所讲到的知识,与书籍略有不同,都是专业技术人员在与用户接触过程中,总结出来的,具有一定的经验分享价值,希望给广大读者带来帮助。
一、字符集
1.列出可用字符集
show character set;
2.列出一个字符集的校对规则
show collation like 'latin1%'
3.数据库字符集和校对规则
create database li default character set latin1 collate latin1_swedish_ci;
4.云服务器字符集和校对规则
(1)编译时指定参数设置
./configure --with-charset=latin1 --with-collation=latin1_german1_ci
(2)启动mysql时初始选项设置
mysqld --default-character-set=latin1 --default-collation=latin1_swedish_ci
5.表字符集和校对规则
create table test1 (id int,name char(20)) default character set latin1 collate latin1_danish_ci;
6.列字符集和校对规则
create table test2 (coll1 varchar(5) character set latin1 collate latin1_german1_ci);
7显示全部可用的字符集
show character set like 'latin1%';
8.显示创建数据库语句
show create database li;
9.显示用户的连接数
show processlist(默认列出100条)
show full processlist(列出全部的)
二、sql语句详解
1.alter语句
(1)更改列名
alter table test change id number integer; 将一个int类型列的id重命名为number
(2)更改列类型
alter table test change number a bigint;
2.drop语句
(1)drop数据库
drop database li;
(2)drop索引
drop index test3;
(3)drop表
drop table test;
3.rename语句
(1)rename 表
rename table test1 to test;
4.delete语句
delete from test;
5.insert语句
insert into test (id,name) values (1,'lisi')
6.load data file语句
LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;
三、用户管理
1.创建用户
create user wang@localhost identified by '123456';
2.删除用户
drop user wang@localhost;
3.创建授权用户
grant create,delete on test.* to wang@'192.168.10.%' identified by '123456'
4.查看用户
select user,host from mysql.user;
5.刷新授权表
flush privileges;
6.查看用户权限
show grants for wang;
7.更改密码
如果是初始登陆的话,是空密码,需要设置
mysqladmin -uroot -password '123456'
如果已知原密码再改密码
mysqladmin -uroot -p123456 password '1234567'
如果是已经改了但是又忘了,并且已经退出了Mysql的登录,则需要跳过授权表启动mysql
首先停掉正在运行的mysql
然后mysqld_safe --skip-grant-tables &
mysql -uroot
update mysql.user set password=password('123456') where user='root';
flush privileges;
查看用户
select user();
四、mysql监控
1.查看某个数据库的磁盘使用空间
select concat(truncate(sum(data_length)/1024/1024,2),'MB') as data_size,
concat(truncate(sum(max_data_length)/1024/1024,2),'MB') as max_data_size,
concat(truncate(sum(data_free)/1024/1024,2),'MB') as data_free,
concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
from information_schema.tables where TABLE_SCHEMA = 'databasename';
2.查看所有的数据库的磁盘空间
select TABLE_SCHEMA,concat(truncate(sum(data_length)/1024/1024,2),'MB') as data_size, concat(truncate(sum(max_data_length)/1024/1024,2),'MB') as max_data_size,
concat(truncate(sum(data_free)/1024/1024,2),'MB') as data_free,
concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
from information_schema.tables group by TABLE_SCHEMA;
3.查询某张表的空间
select concat(truncate(sum(data_length)/1024/1024,2),'MB') as data_size,
concat(truncate(sum(max_data_length)/1024/1024,2),'MB') as max_data_size,
concat(truncate(sum(data_free)/1024/1024,2),'MB') as data_free,
concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
from information_schema.tables
where TABLE_NAME = 'tablename';
对于以上关于mysql专业语句详解,如果大家还有更多需要了解的可以持续关注我们的行业推新,如需获取专业解答,可在官网联系售前售后的,希望该文章可给大家带来一定的知识更新。