MySQL学习总结之路(第二章:表)

Posted 随风去远方

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL学习总结之路(第二章:表)相关的知识,希望对你有一定的参考价值。

目录

MySQL学习总结之路(第一章:服务与数据库管理)

MySQL学习总结之路(第二章:表)

mysql学习总结之路(第三章:数据类型)

 ......

1、创建表

1.1、创建表基本语法:

CREATE TABLE tablename (column_name_1 column_type_1 constraints,
column_name_2 column_type_2 constraints , ……)
column_name 是列的名字
column_type 是列的数据类型
contraints 是这个列的约束条件

1.1.1、创建一张简单的表

mysql> create table orders (ordername varchar(10),createtime date,ordermoney decimal(10,2),ordernumber int(2));
Query OK, 0 rows affected (0.23 sec)

1.1.2、查看创建表定义

1.1.2.1、结构化定义

mysql> desc orders;
+-------------+---------------+------+-----+---------+-------+
| Field       | Type          | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| ordername   | varchar(10)   | YES  |     | NULL    |       |
| createtime  | date          | YES  |     | NULL    |       |
| ordermoney  | decimal(10,2) | YES  |     | NULL    |       |
| ordernumber | int(2)        | YES  |     | NULL    |       |
+-------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

1.1.2.2、表详细定义

查看详细的表定义

mysql> show create table orders \\G;
*************************** 1. row ***************************
       Table: orders
Create Table: CREATE TABLE `orders` (
  `ordername` varchar(10) DEFAULT NULL,
  `createtime` date DEFAULT NULL,
  `ordermoney` decimal(10,2) DEFAULT NULL,
  `ordernumber` int(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR:
No query specified

由此可以看到表的  ENGINE(存储引擎)是InnoDB 

         CHARSET(字符集)是Latin1

“\\G”选项的含义是使得记录能够按照字段竖着排列,对于内容比较长的记录更易于显示。

 

2、删除表

命令:

DROP TABLE tablename

删除orders:

mysql> drop table orders
    -> ;
Query OK, 0 rows affected (0.14 sec)

 

3、修改表

3.1、修改表类型命令:

ALTER TABLE tablename MODIFY [COLUMN] column_definition [FIRST | AFTER col_name]
例:修改表 orders 的 name 字段定义,将 varchar(10)改为 varchar(20):
mysql> alter table orders modify ordername varchar(20);
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc orders;
+-------------+---------------+------+-----+---------+-------+
| Field       | Type          | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| ordername   | varchar(20)   | YES  |     | NULL    |       |
| createtime  | date          | YES  |     | NULL    |       |
| ordermoney  | decimal(10,2) | YES  |     | NULL    |       |
| ordernumber | int(2)        | YES  |     | NULL    |       |
+-------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

3.2、字段改名命令:

ALTER TABLE tablename CHANGE [COLUMN] old_col_name column_definition
[FIRST|AFTER col_name]

例:表 orders 上将ordernumber修改为ordernumbers

mysql> alter table orders change column ordernumber ordernumbers int(4);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc orders;
+--------------+---------------+------+-----+---------+-------+
| Field        | Type          | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| ordername    | varchar(20)   | YES  |     | NULL    |       |
| createtime   | date          | YES  |     | NULL    |       |
| ordermoney   | decimal(10,2) | YES  |     | NULL    |       |
| ordernumbers | int(4)        | YES  |     | NULL    |       |
+--------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

特别说明:change 和 modify 都可以修改表的定义,不同的是 change 后面需要写两次列名,不方便。但是 change 的优点是可以修改列名称,modify 则不能。

3.3、增加表字段命令:

ALTER TABLE tablename ADD [COLUMN] column_definition [FIRST | AFTER col_name]
例:表 orders 上新增加字段 username,类型为 varchar(3):
mysql> alter table orders add column username varchar(30);
Query OK, 0 rows affected (0.39 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc orders;
+-------------+---------------+------+-----+---------+-------+
| Field       | Type          | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| ordername   | varchar(20)   | YES  |     | NULL    |       |
| createtime  | date          | YES  |     | NULL    |       |
| ordermoney  | decimal(10,2) | YES  |     | NULL    |       |
| ordernumber | int(2)        | YES  |     | NULL    |       |
| username    | varchar(30)   | YES  |     | NULL    |       |
+-------------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

3.4、删除表列字段命令

ALTER TABLE tablename DROP [COLUMN] col_name

例:表 orders 上删除字段 username:

mysql> alter table orders drop column username;
Query OK, 0 rows affected (0.53 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc orders;
+-------------+---------------+------+-----+---------+-------+
| Field       | Type          | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| ordername   | varchar(20)   | YES  |     | NULL    |       |
| createtime  | date          | YES  |     | NULL    |       |
| ordermoney  | decimal(10,2) | YES  |     | NULL    |       |
| ordernumber | int(2)        | YES  |     | NULL    |       |
+-------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

3.5、表改名命令

ALTER TABLE tablename RENAME [TO] new_tablename

例:表 orders 名字改为goodsorders

mysql> alter table orders rename goodsorders;
Query OK, 0 rows affected (0.16 sec)

mysql> desc orders;
ERROR 1146 (42S02): Table \'ordermanage.orders\' doesn\'t exist
mysql> desc goodsorders;
+--------------+---------------+------+-----+---------+-------+
| Field        | Type          | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| ordername    | varchar(20)   | YES  |     | NULL    |       |
| createtime   | date          | YES  |     | NULL    |       |
| ordermoney   | decimal(10,2) | YES  |     | NULL    |       |
| ordernumbers | int(4)        | YES  |     | NULL    |       |
+--------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

 

4、DML 语句

插入(insert)、查询(select)、更新(update)、删除(delete)

4.1、插入记录 命令

INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,……valuesn);

例:表 goodsorders 中插入一条记录,ordername 为zhang,createtime为2021-05-12,ordermoney为100.00,ordernumbers为:1

mysql> insert into goodsorders (ordername,createtime,ordermoney,ordernumbers) values(\'zhang\',\'2021-05-12\',100.00,1);
Query OK, 1 row affected (0.03 sec)

也可以省略(field1,field2,……fieldn)这一部分

mysql> insert into goodsorders  values(\'zhang1\',\'2021-05-12\',1001.00,11);
Query OK, 1 row affected (0.05 sec)

4.2、查看插入数据命名

4.2.1、查询全部

SELECT * FROM tablename [WHERE CONDITION]

例:查看goodsorders中所有插入数据

mysql> select * from goodsorders;
+-----------+------------+------------+--------------+
| ordername | createtime | ordermoney | ordernumbers |
+-----------+------------+------------+--------------+
| zhang     | 2021-05-12 |     100.00 |            1 |
| zhang1    | 2021-05-12 |    1001.00 |           11 |
+-----------+------------+------------+--------------+
2 rows in set (0.00 sec)
其中“*”表示要将所有的记录都选出来

4.2.2、查询不重复记录命令关键字

distinct

例:查询非goodsorders中非重复创建时间(createtime)的数据

mysql> select  * from goodsorders;
+-----------+------------+------------+--------------+
| ordername | createtime | ordermoney | ordernumbers |
+-----------+------------+------------+--------------+
| zhang     | 2021-03-11 |      50.00 |            1 |
| li        | 2020-05-12 |      70.00 |           15 |
| li        | 2020-03-12 |      70.00 |           15 |
| li        | 2020-03-11 |      70.00 |           15 |
| li        | 2021-03-11 |      70.00 |           15 |
+-----------+------------+------------+--------------+
5 rows in set (0.00 sec)

mysql> select distinct createtime from goodsorders;
+------------+
| createtime |
+------------+
| 2021-03-11 |
| 2020-05-12 |
| 2020-03-12 |
| 2020-03-11 |
+------------+
4 rows in set (0.00 sec)

由此可以看到,将重复的一条时间数据2021-03-11去掉了

4.2.3、多条件查询关键字

where 后面的条件是一个字段的‘=’比较,还可以使用><>=<=!=等比较运算符;
多个条件之间还可以使用 orand 等逻辑运算符进行多条件联合查询,

例:查询非goodsorders中 ordername=\'li\'并且createtime为2020-03-11

mysql> select * from goodsorders where ordername=\'li\'and createtime =\'2020-03-11\';
+-----------+------------+------------+--------------+
| ordername | createtime | ordermoney | ordernumbers |
+-----------+------------+------------+--------------+
| li        | 2020-03-11 |      70.00 |           15 |
+-----------+------------+------------+--------------+
1 row in set (0.00 sec)

4.2.4、排序查询命名

SELECT * FROM tablename [WHERE CONDITION] [ORDER BY field1 [DESC|ASC] , field2 
[DESC|ASC],……fieldn [DESC|ASC]]

例:把 goodsorders表中的记录按照创建时间高低进行排序显示

mysql> select * from goodsorders order by createtime;
+-----------+------------+------------+--------------+
| ordername | createtime | ordermoney | ordernumbers |
+-----------+------------+------------+--------------+
| li        | 2020-03-11 |      70.00 |           15 |
| li        | 2020-03-12 |      70.00 |           15 |
| li        | 2020-05-12 |      70.00 |           15 |
| zhang     | 2021-03-11 |      50.00 |            1 |
| li        | 2021-03-11 |      70.00 |           15 |
+-----------+------------+------------+--------------+
5 rows in set (0.01 sec)

4.2.5、显示一部分,而不是全部,指令

SELECT ……[LIMIT offset_start,row_count]
offset_start 表示记录的起始偏移量
row_count 表示显示的行数
例如1:显示 goodsorders表中按照 createtiem 排序后的前 3 条记录:
mysql> select * from goodsorders order by createtime limit 3;
+-----------+------------+------------+--------------+
| ordername | createtime | ordermoney | ordernumbers |
+-----------+------------+------------+--------------+
| li        | 2020-03-11 |      70.00 |           15 |
| li        | 2020-03-12 |      70.00 |           15 |
| li        | 2020-05-12 |      70.00 |           15 |
+-----------+------------+------------+--------------+
3 rows in set (0.00 sec)

例如2:如果要显示 goodsorders表中按照 createtiem 排序后 从第二条记录开始,显示3条数据:

mysql> select * from goodsorders order by createtime limit 2,3;
+-----------+------------+------------+--------------+
| ordername | createtime | ordermoney | ordernumbers |
+-----------+------------+------------+--------------+
| li        | 2020-05-12 |      70.00 MySQL学习总结之路(第三章:数据类型)

DB | MySQL核心知识学习之路

[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段

Python之路第二十篇--MySQL

MySql 学习之路-高级1

Mysql 学习目录