获得数据库和表的信息一般正常的程序员或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>
以上就是总结和mysql相关的知识的详细内容。