常用mysql命令

1.
mysql1 -p aaa
连接数据库mysql1

mysql1 -h 59.34.148.203 -u root -p
连接59.34.148.203上的数据库mysql1


2.
quit
退出数据库

 

3.
select version(),current_date,now(),user();
版本号和当前日期

 

4.
select sin(pi()/4),(4+1)*5;
计算数值

 

5.
select sin(pi()/4),(4+1)*5 /c;
取消命令执行

 

6.
show databases;
use xiuxian
显示并使用数据库

 

7.
grant all on xiuxian.* to
root@116.28.65.79;
这里root是分配给你的MySQL用户名,116.28.65.79是所连接的服务器所在的主机。

 

8.
select * from t_player where level > 87 /G;

select distinct owner from pet;

select name, birth from pet order by birth;

select name, birth from pet order by birth desc;

select name, species, birth from pet order by species, birth desc;

select name, birth, death,(year(death)-year(birth)) - (right(death,5)<right(birth,5)) as age from pet where death is not null order by age;
YEAR()提取日期的年部分,RIGHT()提取日期的MM-DD (日历年)部分的最右面5个字符。

select name, birth from pet where month(birth) = 5;


查询

 

9.
create database test;
创建数据库

 

10.
show tables;
显示表

 

11.
create table pet (name varchar(20), owner varchar(20),species varchar(20), sex char(1), birth date,death date);

create table shop (
    article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,
    dealer  CHAR(20)                 DEFAULT ''     NOT NULL,
    price   DOUBLE(16,2)             DEFAULT '0.00' NOT NULL,
    PRIMARY KEY(article, dealer));

insert into shop values
    (1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),
    (3,'C',1.69),(3,'D',1.25),(4,'D',19.95);


创建表

 

12.
drop table pet;
删除表

 

13.
describe pet;
描述表

 

14.
update pet set birth = '1989-08-31' where name = 'Bowser';
更新表

 

15.
select 1 = null, 1 <> null, 1 < null, 1 > null;

select 1 is null, 1 is not null;

select 0 is null, 0 is not null, '' is null, '' is not null;

可以在定义为NOT NULL的列内插入0或空字符串

 

16.
select * from t_player where name like 'b%';

select * from t_player where name like '%fy';

select * from t_player where name like '%f%';

select * from t_player where name like '_____';

SQL模式匹配允许你使用“_”匹配任何单个字符,而“%”匹配任意数目字符(包括零字符)。在 MySQL中,SQL的模式默认是忽略大小写的。下面给出一些例子。注意使用SQL模式时,不能使用=或!=;而应使用LIKE或NOT LIKE比较操作符。

 

17.
select * from t_player where name regexp '^b';
找出以“b”开头的名字,使用“^”匹配名字的开始:

select * from t_player where name regexp binary '^b';
强制使REGEXP比较区分大小写,使用BINARY关键字使其中一个字符串变为二进制字符串。该查询只匹配名称首字母的小写‘b’。

select * from t_player where name regexp 'fy$';
找出以“fy”结尾的名字,使用“$”匹配名字的结尾:

select * from t_player where name regexp 'w';
找出包含一个“w”的名字

select * from t_player where name regexp '^.....$';
select * from t_player where name regexp '^.{5}$';
找出包含正好5个字符的名字,使用“^”和“$”匹配名字的开始和结尾,和5个“.”实例在两者之间

(1)‘.’匹配任何单个的字符。

(2) 字符类“[...]”匹配在方括号内的任何字符。例如,“[abc]”匹配“a”、“b”或“c”。为了命名字符的范围,使用一个“-”。“[a-z]”匹配任何字母,而“[0-9]”匹配任何数字。

(3) “ * ”匹配零个或多个在它前面的字符。例如,“x*”匹配任何数量的“x”字符,“[0-9]*”匹配任何数量的数字,而“.*”匹配任何数量的任何字符。

如果REGEXP模式与被测试值的任何地方匹配,模式就匹配(这不同于LIKE模式匹配,只有与整个值匹配,模式才匹配)。
为了定位一个模式以便它必须匹配被测试值的开始或结尾,在模式开始处使用“^”或在模式的结尾用“$”。


18.
select count(*) from t_player;

select camp, COUNT(*) from t_player group by camp;

 

19.
select database();

 

20.
source filename;
/. filename
执行脚本

 

21.
select max(article) as article from shop;
列的最大值

 

22.
select article, dealer, price
from   shop
where  price=(select max(price) from shop);
拥有某个列的最大值的行

 

23.
select article, dealer, price
from shop
order by price desc
limit 1;
拥有某个列的最大值的行

 

24.
select article, max(price) as price
from   shop
group by article;
列的最大值:按组

 

25.
select article, dealer, price
from   shop s1
where  price=(select max(s2.price)
              from shop s2
              where s1.article = s2.article);
拥有某个字段的组间最大值的行

 

26.
select @min_price:=min(price),@max_price:=max(price) from shop;
select * from shop where
price=@min_price or price=@max_price;
使用用户变量

 

27.
create table person (
    id smallint unsigned not null auto_increment,
    name char(60) not null,
    primary key (id)
);

create table shirt (
    id smallint unsigned not null auto_increment,
    style enum('t-shirt', 'polo', 'dress') not null,
    color enum('red', 'blue', 'orange', 'white', 'black') not null,
    owner smallint unsigned not null references person(id),
    primary key (id)
);

insert into person values (null, 'Antonio Paz');
 
select @last := LAST_INSERT_ID();

insert into shirt values
(null, 'polo', 'blue', @last),
(null, 'dress', 'white', @last),
(null, 't-shirt', 'blue', @last);

insert into person values (null, 'Lilliana Angelovska');
 
select @last := LAST_INSERT_ID();
 
insert into shirt values
(null, 'dress', 'orange', @last),
(null, 'polo', 'red', @last),
(null, 'dress', 'blue', @last),
(null, 't-shirt', 'white', @last);

select * from person;

 

28.

select TABLE_NAME,DATA_LENGTH+INDEX_LENGTH,TABLE_ROWS

 from TABLES

where TABLE_SCHEMA='数据库名' '   and   TABLE_NAME='表名'';

 

29.

explain select name from t_player where name="lbs";

 

30.

mysql --help |grep Distrib

查看数据库版本

posted on 2013-07-20 12:06  树河田  阅读(211)  评论(0编辑  收藏  举报