Linux下MySQL数据库常用基本操作

1、显示数据库

show databases; 

2、选择数据库

use 数据库名; 

3、显示数据库中的表

show tables; 

4、显示数据表的结构

describe 表名; 

 5、显示表中记录

SELECT * FROM 表名 

 6、建库

create databse 库名;

CREATE DATABASE `itv_maopao` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci

 8、增加记录

insert into name(uname,gender,birthday) values('张三','男','1971-10-01'); 

 9、修改记录

update name set birthday='1971-01-10' where uname='张三'; 

 10、删除记录

delete from name where uname='张三'; 

 11、删除表

drop table 表名 

 12、删除库

drop database 库名;

13、备份数据库

mysqldump -u root -p--default-character-set=utf8数据库名>备份名; //进入到库目录

--default-character-set=utf8

mysqldump -u用户名 数据库  表名称 > /home/temp/aaa.sql 

mysqldump -u root -p --add-locks -q -d dbname > dbname.sql 

(导出表结构)

14、恢复

mysql -u root -p--default-character-set=utf8 数据库名<备份名; //恢复时数据库必须存在,可以为空数据库

 15、数据库授权

格式:grant select on 数据库.* to 用户名@登录主机 identified by "密码" 

例1、增加一个用户user001密码为123456,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MySQL,然后键入以下命令: 

mysql> grant select,insert,update,delete on *.* to user001@"%" Identified by "123456";

例2、增加一个用户user002密码为123456,让此用户只可以在localhost上登录,也可以设置指定IP,并可以对数据库test进行查询、插入、修改、删除的操作 (localhost指本地主机,即MySQL数据库所在的那台主机)

//这样用户即使用知道user_2的密码,他也无法从网上直接访问数据库,只能通过MYSQL主机来操作test库。 

 //首先用以root用户连入MySQL,然后键入以下命令:

mysql>grant select,insert,update,delete on test.* to user002@localhost identified by "123456";

查看列:desc 表名;

修改表名:alter table t_book rename to bbb;

添加列:alter table 表名 add column 列名 varchar(30);

删除列:alter table 表名 drop column 列名;

修改列名MySQL: alter table bbb change nnnnn hh int;

查询过去的数据:

今天

select * from 表名 where to_days(时间字段名) = to_days(now());

昨天

SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) – TO_DAYS( 时间字段名) <= 1

7天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)

近30天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)

本月

SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, ‘%Y%m’ ) = DATE_FORMAT( CURDATE( ) , ‘%Y%m’ )

上一月

SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , ‘%Y%m’ ) , date_format( 时间字段名, ‘%Y%m’ ) ) =1

#查询本季度数据

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());

#查询上季度数据

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

#查询本年数据

select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());

#查询上年数据

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));


查询当前这周的数据

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,’%Y-%m-%d’)) = YEARWEEK(now());

查询上周的数据

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,’%Y-%m-%d’)) = YEARWEEK(now())-1;

查询当前月份的数据

select name,submittime from enterprise where date_format(submittime,’%Y-%m’)=date_format(now(),’%Y-%m’)

查询距离当前现在6个月的数据

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

查询上个月的数据

select name,submittime from enterprise where date_format(submittime,’%Y-%m’)=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),’%Y-%m’)

select * from ` user ` where DATE_FORMAT(pudate, ‘ %Y%m ‘ ) = DATE_FORMAT(CURDATE(), ‘ %Y%m ‘ ) ;

select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,’%y-%m-%d’)) = WEEKOFYEAR(now())

select *

from user

where MONTH (FROM_UNIXTIME(pudate, ‘ %y-%m-%d ‘ )) = MONTH (now())

select *

from [ user ]

where YEAR (FROM_UNIXTIME(pudate, ‘ %y-%m-%d ‘ )) = YEAR (now())

and MONTH (FROM_UNIXTIME(pudate, ‘ %y-%m-%d ‘ )) = MONTH (now())

select *

from [ user ]

where pudate between 上月最后一天

and 下月第一天

where date(regdate) = curdate();

select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())

SELECT date( c_instime ) ,curdate( )

FROM `t_score`

WHERE 1

LIMIT 0 , 30

你可能感兴趣的:(Linux下MySQL数据库常用基本操作)