經常使用mysql命令

1.
mysql1 -p aaa
鏈接數據庫mysql1
mysql

mysql1 -h 59.34.148.203 -u root -p
鏈接59.34.148.203上的數據庫mysql1
sql

 


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;
取消命令執行
ui

6.
show databases;
use xiuxian
顯示並使用數據庫
spa

7.
grant all on xiuxian.* to
root@116.28.65.79;
這裏root是分配給你的MySQL用戶名,116.28.65.79是所鏈接的服務器所在的主機。
regexp

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

select distinct owner from pet;rem

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";

相關文章
相關標籤/搜索