首页 > 代码库 > MySQL常用操作

MySQL常用操作

MySQL常用操作

授权超级用户:

grant all privileges on *.* to ‘tangnanbing‘@‘%‘ identified by ‘1qaz@WSX‘ with grant option;

查看库:

show databases;

查看都有哪些库  show databases;

查看某个库的表 use db; show tables \G; 

查看表的字段 desc tb;

查看建表语句 show create table tb;

当前是哪个用户  select user();

当前库 select database();

创建库 create database db1; 

创建表 create table t1 (id int, name char(40) adress varchar(30));  

char(10)              ‘aaa       ‘

varchar(10)          ‘aaa‘

查看数据库版本 select version(); 

查看mysql状态 show status;

修改mysql参数 show variables like ‘max_connect%‘; set global max_connect_errors = 1000; 

查看mysql队列 show processlist; 

select * from information_schema.processlist where info is not null;

sleep的可以忽略,qurey查询的才有

创建普通用户并授权 grant all on *.* to databases1.user1 identified by ‘123456‘; 

grant all on db1.* to ‘user2‘@‘10.0.2.100‘ identified by ‘111222‘; 

grant all on db1.* to ‘user3‘@‘%‘ identified by ‘231222‘;insert into tb1 (id,name) values(1,‘aming‘);

更改密码 UPDATE mysql.user SET password=PASSWORD("newpwd") WHERE user=‘username‘ ;   

查询 select count(*) from mysql.user; select * from mysql.db; select * from mysql.db where host like ‘10.0.%‘; 

插入 update db1.t1 set name=‘aaa‘ where id=1;  

清空表 truncate table db1.t1; 

删除表 drop table db1.t1; 

删除数据库 drop database db1; 

修复表 repair table tb1 [use frm];

查看权限show grants for root@‘localhost‘;

echo "select user,host,password from mysql.user" |mysql -uroot -plingxiangxiang

mysql -uroot -p1234556 -e "select user,host,password into outfile ‘/home/mysql/1.txt‘ from mysql.user;"

增:insert into test.test (id, name) values (123, ‘ling‘);

insert into test.test values (value1_1, value2_2), (value2_1,value2_2), (value3_1, value3_2);

删:delete from test.test where id in (123, 456);  

alter table test drop column dt;删除字段,test表,dt字段

改:update msyql.user set password = password(‘lingxiangxiang‘)

alter table employees add primary key (emp_no);       增加主键

alter table employees drop/add column salaries;         删除字段

create table blog_blogmodel as select * from book_blogmodel;   创建一样的新表

该表名:

查:select user, host, password from mysql.user where user = "root";

select * from msyql.user where conditions order by user [desc];

调整字段顺序:

  1. ALTER TABLE `user_movement_log` CHANGE `GatewayId` `GatewayId` int not null default 0 AFTER RegionID  

  2. alter table test2 drop column cj, drop column goushi;

排序:select * from test.test order by id asc/desc;

SELECT * FROM usersWHERE email NOT REGEXP ‘^[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$‘

show global variables like ‘%read_only%‘;

alter user root@localhost identified by ‘‘;

导出:

select * into outfile ‘/tmp/test/users.txt‘ fields terminated by ‘<[!]>‘ lines terminated by ‘<[end]>‘ from users;

导入:

load data infile ‘/tmp/test/users.txt‘ into table gamedb.users fields terminated by ‘<[!]>‘ lines terminated by ‘<[end]>‘;

create index idx_name on salaries(emp_no);     创建salaries表的emp_no字段的索引

show index from salaries\G;             查看索引

日期常用格式:‘year-month-day‘

复杂语句:

select * from (select * from employees order by emp_no desc) aa group by hire_date ;

先对emp_no 进行反向排序,然后在把hire_date分组

技术分享

内连接:join默认是inner

select * from employees inner join salaries on employees.emp_no = salaries.emp_no;

技术分享

左外链接:右外链:

select * from employees left join salaries on employees.emp_no = salaries.emp_no;

技术分享

select * from employees right join salaries on employees.emp_no = salaries.emp_no;  

技术分享

update的加强版

select * from employees;

技术分享

UPDATE `employees`

   SET emp_name = CASE emp_name

           WHEN ‘ling‘ THEN ‘lingjing‘

           WHEN ‘xiang‘ THEN ‘lingxiang‘

           ELSE ‘wang‘

           END

   WHERE gender=‘M‘;

技术分享

limit 1 :打印一行

max(emp_no):  最大

select max(emp_no) as emp_no from employees;       max(emp_no)改成名字emp_no

问题三:内联结 + 非关联子查询

SELECT s1.article, `dealer`, s1.price

   FROM `shop` AS s1

       JOIN (  SELECT `article`, MAX(price) AS price

                   FROM `shop`

                   GROUP BY `article`

           ) AS s2

       ON s1.article = s2.article

           AND s1.price = s2.price

;


本文出自 “12350027” 博客,谢绝转载!

MySQL常用操作