獲得資料庫和表的信息 一般正常的程式員或DBA都會在敲代碼的時候突然想到這樣的一系列問題:我是誰?我在哪?我在乾什麼? 我的資料庫呢?我的表呢?我表怎麼創建的?我該怎麼辦呢?你可能會想到SHOW DATABASES; 命令。But, 這個命令是列出由mysql管理的databases. 不是知道我再 ...
獲得資料庫和表的信息
一般正常的程式員或DBA都會在敲代碼的時候突然想到這樣的一系列問題:我是誰?我在哪?我在乾什麼? 我的資料庫呢?我的表呢?我表怎麼創建的?我該怎麼辦呢?你可能會想到SHOW DATABASES; 命令。But, 這個命令是列出由mysql管理的databases. 不是知道我再哪的命令。到底哪個命令是呢?
我左某人在翻閱上古的典籍的時候查到這樣的一個命令:
SELECT DATABASE();
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)
mysql>
顯而易見,這是一個告訴我我再哪個資料庫的命令。然後肯定會有一群二五仔問:我要是沒有進入任何資料庫那會顯示什麼呢?
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql>
當然是NULL了,還能有什麼?
現在,我們找到了正在使用的資料庫(test) 。然後,該找要找的表了,比如說(pet)。 根據古籍上面的記載應該使用如下的命令:
SHOW TABLES;
mysql> SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| event |
| pet |
+----------------+
2 rows in set (0.00 sec)
mysql>
And then I want to know 表的結構。What should I do?
DESCRIBE pet;
mysql> DESCRIBE pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql>
老司機一般都簡寫成
DESC pet;
Field 表示列名字
Type表示列的數據類型
Null表示能否為NULL
Key表示是否被索引
Default表示欄位的預設值
如果表有索引,SHOW INDEX FROM tbl_name 顯示索引的信息。
常用查詢的例子
在搞事情之前,肯定要先建一個表:假定有一個表(shop)來存儲某商人()的每件物品()的價格()。(物品、商人作為主鍵)
操作如下:
mysql> 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));
Query OK, 0 rows affected (0.56 sec)
mysql>
然後插入一些數據:
mysql> 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);
Query OK, 7 rows affected (0.24 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql>
查看一下表:
mysql> SELECT * FROM shop;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | A | 3.45 |
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | B | 1.45 |
| 0003 | C | 1.69 |
| 0003 | D | 1.25 |
| 0004 | D | 19.95 |
+---------+--------+-------+
7 rows in set (0.00 sec)
mysql>
然後我們就可以學習後面的內容了
列的最大值
舉例:在shop中的最大的物品號?
操作如下:
SELECT MAX(article) FROM shop;
mysql> SELECT MAX(article) FROM shop;
+--------------+
| MAX(article) |
+--------------+
| 4 |
+--------------+
1 row in set (0.00 sec)
mysql>
舉例:找最貴的商品
操作如下:
SELECT MAX(price) FROM shop;
mysql> SELECT MAX(price) FROM shop;
+------------+
| MAX(price) |
+------------+
| 19.95 |
+------------+
1 row in set (0.00 sec)
mysql>
知道MAX()函數是幹啥的了吧。
擁有某個列的最大值的行
慄子:查詢最貴的商品的信息
操作如下:
SELECT * FROM shop WHERE price = (SELECT MAX(price) FROM shop);
mysql> SELECT * FROM shop
-> WHERE price =
-> (SELECT MAX(price) FROM shop);
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0004 | D | 19.95 |
+---------+--------+-------+
1 row in set (0.00 sec)
mysql>
還有一種操作:
SELECT * FROM shop ORDER BY price DESC LIMIT 1;
mysql> SELECT * FROM shop
-> ORDER BY price DESC
-> LIMIT 1;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0004 | D | 19.95 |
+---------+--------+-------+
1 row in set (0.00 sec)
mysql>
前者是一個嵌套查詢,後者是根據價格排序只顯示一個。
列的最大值:按組
慄子:每項物品(article)的最高價格是多少?
操作如下:
SELECT article, MAX(price) AS price FROM shop GROUP BY article;
mysql> SELECT article, MAX(price) AS price
-> FROM shop
-> GROUP BY article;
+---------+-------+
| article | price |
+---------+-------+
| 0001 | 3.99 |
| 0002 | 10.99 |
| 0003 | 1.69 |
| 0004 | 19.95 |
+---------+-------+
4 rows in set (0.00 sec)
mysql>
擁有某個欄位的組間最大值的行
並不明白標題是啥意思。。。。
慄子:對每項物品,找出最貴價格的物品的經銷商。
操作如下:
SELECT article, dealer, price
FROM shop s1
WHERE price = (SELECT MAX(price)
FROM shop s2
WHERE s1.article = s2.article);
mysql> SELECT article, dealer, price
-> FROM shop s1
-> WHERE price = (SELECT MAX(s2.price)
-> FROM shop s2
-> WHERE s1.article = s2.article);
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | C | 1.69 |
| 0004 | D | 19.95 |
+---------+--------+-------+
4 rows in set (0.00 sec)
這裡書上沒寫為什麼,自己也不是十分理解。求懂的大佬在評論區講解●﹏●。
使用用戶變數
慄子:找出價格最高或最低的物品
操作如下:
SELECT @min_price:=MIN(price), @max_price:=MAX(price) FORM shop;
SELECT * FROM shop WHERE price = @min_price OR price = @max_price;
mysql> SELECT @min_price:=MIN(price), @max_price:=MAX(price) FROM shop;
+------------------------+------------------------+
| @min_price:=MIN(price) | @max_price:=MAX(price) |
+------------------------+------------------------+
| 1.25 | 19.95 |
+------------------------+------------------------+
1 row in set (0.13 sec)
mysql> SELECT * FROM shop WHERE price=@min_price OR price = @max_price;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0003 | D | 1.25 |
| 0004 | D | 19.95 |
+---------+--------+-------+
2 rows in set (0.09 sec)
mysql>
關於用戶變數後面會有的,好奇的鴇鴇可以百度。
使用外鍵
多說無意 直接上操作, 上面有一個傳送門,講的很不錯了。
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;
+----+---------------------+
| id | name |
+----+---------------------+
| 1 | Antonio Paz |
| 2 | Lilliana Angelovska |
+----+---------------------+
SELECT * FROM shirt;
+----+---------+--------+-------+
| id | style | color | owner |
+----+---------+--------+-------+
| 1 | polo | blue | 1 |
| 2 | dress | white | 1 |
| 3 | t-shirt | blue | 1 |
| 4 | dress | orange | 2 |
| 5 | polo | red | 2 |
| 6 | dress | blue | 2 |
| 7 | t-shirt | white | 2 |
+----+---------+--------+-------+
SELECT s.* FROM person p, shirt s
WHERE p.name LIKE 'Lilliana%'
AND s.owner = p.id
AND s.color <> 'white';
+----+-------+--------+-------+
| id | style | color | owner |
+----+-------+--------+-------+
| 4 | dress | orange | 2 |
| 5 | polo | red | 2 |
| 6 | dress | blue | 2 |
+----+-------+--------+-------+
我錯了,網斷了。只好拷貝書上的代碼了。
mysql> show create table shirt\G
*************************** 1. row ***************************
Table: shirt
Create Table: CREATE TABLE `shirt` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`style` enum('t-shirt','polo','dress') NOT NULL,
`color` enum('red','blue','orange','white','black') NOT NULL,
`owner` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1
1 row in set (0.01 sec)
mysql>
外鍵其實沒啥講的,也就一篇博客的事兒(手動滑稽)
to be continued...