mysql之select/update/alter篇

1)关联子查询:a:mysql> select name,address from customers where customerid = (select customerid from orders where amount = (select max(amount) from orders));

       b:select customerid from orders where amount = (select max(amount) from orders);       

1 mysql> select customerid from orders where amount = (select max(amount) from orders);
2 +------------+
3 | customerid |
4 +------------+
5 |          2 |
6 +------------+
7 1 row in set (0.00 sec)

                   b>select name,address from customers where customerid = 2;与a等价的操作;

2)表单更改:a:updated books set price = price*1.1;

      b:mysql> update customers set address = '250 Olsens Road' where customerid = 4;

之前

 1 mysql> describe customers;
 2 +------------+------------------+------+-----+---------+----------------+
 3 | Field      | Type             | Null | Key | Default | Extra          |
 4 +------------+------------------+------+-----+---------+----------------+
 5 | customerid | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
 6 | name       | char(50)         | NO   |     | NULL    |                |
 7 | address    | char(100)        | NO   |     | NULL    |                |
 8 | city       | char(30)         | NO   |     | NULL    |                |
 9 +------------+------------------+------+-----+---------+----------------+
10 4 rows in set (0.00 sec)

之后

 1 mysql> describe customers;
 2 +------------+------------------+------+-----+---------+----------------+
 3 | Field      | Type             | Null | Key | Default | Extra          |
 4 +------------+------------------+------+-----+---------+----------------+
 5 | customerid | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
 6 | name       | char(70)         | NO   |     | NULL    |                |
 7 | address    | char(100)        | NO   |     | NULL    |                |
 8 | city       | char(30)         | NO   |     | NULL    |                |
 9 +------------+------------------+------+-----+---------+----------------+
10 4 rows in set (0.00 sec)

      c:mysql> alter table customers modify name char(70),city char(40) not null;(更改数据类型)

      d:mysql> alter table orders add tax float(6,2) after amount;(添加列)

 1 mysql> select * from orders;
 2 +---------+------------+--------+------+------------+
 3 | orderid | customerid | amount | tax  | date       |
 4 +---------+------------+--------+------+------------+
 5 |       1 |          3 |  69.98 | NULL | 2007-04-02 |
 6 |       2 |          1 |  49.99 | NULL | 2007-04-15 |
 7 |       3 |          2 |  74.98 | NULL | 2007-04-19 |
 8 |       4 |          3 |  24.99 | NULL | 2007-05-01 |
 9 +---------+------------+--------+------+------------+
10 4 rows in set (0.01 sec)

      e:mysql> alter table orders drop tax;(删除列)

 1 mysql> select * from orders;
 2 +---------+------------+--------+------------+
 3 | orderid | customerid | amount | date       |
 4 +---------+------------+--------+------------+
 5 |       1 |          3 |  69.98 | 2007-04-02 |
 6 |       2 |          1 |  49.99 | 2007-04-15 |
 7 |       3 |          2 |  74.98 | 2007-04-19 |
 8 |       4 |          3 |  24.99 | 2007-05-01 |
 9 +---------+------------+--------+------------+
10 4 rows in set (0.00 sec)

 

转载于:https://www.cnblogs.com/maivecute/p/mysqldatabases-study.html

你可能感兴趣的:(mysql之select/update/alter篇)