mysql_study_5

代码

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));
# 插入数据
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);

使用max()计算最大值

mysql> SELECT MAX(article) as article FROM shop;

查询某列最大值所在的行

# 显示price这一列最大值对应的行
mysql> SELECT article, dealer, price
    -> FROM   shop
    -> WHERE  price=(SELECT MAX(price) FROM shop);
mysql> SELECT article, dealer, price
    -> FROM shop
    -> ORDER BY price DESC
    -> LIMIT 1;

 按组显示列的最大值

mysql> SELECT article, MAX(price) AS price
    -> FROM shop
    -> GROUP BY article;

通过使用用户变量找出价格最高或者最低的物品

mysql> select @min_price:=MIN(price),@max_price:=MAX(price) from shop;
+------------------------+------------------------+
| @min_price:=MIN(price) | @max_price:=MAX(price) |
+------------------------+------------------------+
| 1.25 | 19.95 |
+------------------------+------------------------+
mysql> select @min_price:=MIN(price),@max_price:=MAX(price) from shop;
+------------------------+------------------------+
| @min_price:=MIN(price) | @max_price:=MAX(price) |
+------------------------+------------------------+
|                   1.25 |                  19.95 |
+------------------------+------------------------+

使用两个关键字进行搜索

充分利用 OR 连接两个关键字(AND也是一样的道理)


# 这里面的test_table可以是任何一个表,关键词也是类似
SELECT field1_index, field2_index FROM test_table
WHERE field1_index = '1' OR  field2_index = '1'

还可以使用UNION将表里面的两个关键字一起使用进行搜索

SELECT field1_index, field2_index
    FROM test_table WHERE field1_index = '1'
    UNION
SELECT field1_index, field2_index
    FROM test_table WHERE field2_index = '1';

UNION 用于合并两个或多个 SELECT 语句的结果集,并消去表中任何重复行。

 

使用AUTO_INCREMENT语句

 

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
 );

INSERT INTO animals (name) VALUES 
    ('dog'),('cat'),('penguin'),
    ('lax'),('whale'),('ostrich');

SELECT * FROM animals;

自动升序编号

或者可以

AUTO_INCREMENT 默认从 1 开始递增,如果你想自定义开始的数字,可以在创建表的时候指定,如:

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
)AUTO_INCREMENT = 1000

或者表已经创建好了 直接指定修改下  ALTER TABLE tbl_name AUTO_INCREMENT = 100001;

创建表得时候也可以指定得

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
 )AUTO_INCREMENT=100001;

 

 

 

 

 

尝试建立一个图书馆数据库,任由发挥,但必须包含基本信息:图书信息,借书人的信息,出版社信息。并运用这一节课学到的索引知识对数据库进行索引。

create database Library;
use Library;

create table publish(
    pub_id int primary key,
    pub_name varchar(50)
);

create table books(
    book_id int primary key,
    book_name varchar(50) not null,
    book_pub int not null,
    foreign key(book_pub) references publish(pub_id)
);

create table reader(
    reader_id int primary key,
    reader_name varchar(50)
);

 

posted @ 2019-04-25 20:52  冰糖葫芦很乖  阅读(182)  评论(0编辑  收藏  举报