在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、创建表1.1、创建表基本语法CREATE TABLE tablename (column_name_1 column_type_1 constraints, column_name_2 column_type_2 constraints , ……)
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、查看创建表定义结构化定义: 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) 表详细定义: 查看详细的表定义: 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 由此可以看到表的 “ 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] 例:修改表 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] 例:表 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)
3.3、增加表字段命令ALTER TABLE tablename ADD [COLUMN] column_definition [FIRST | AFTER col_name] 例:表 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 例:表 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 例:表 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 语句插入( 4.1、插入记录 命令INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,……valuesn); 例:表 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] 例:查看 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 例:查询非 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) 由此可以看到,将重复的一条时间数据 4.2.3、多条件查询关键字
例:查询非 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]] 例:把 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]
例如1:显示 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:如果要显示 mysql> select * from goodsorders order by createtime limit 2,3; +-----------+------------+------------+--------------+ | ordername | createtime | ordermoney | ordernumbers | +-----------+------------+------------+--------------+ | li | 2020-05-12 | 70.00 | 15 | | zhang | 2021-03-11 | 50.00 | 1 | | li | 2021-03-11 | 70.00 | 15 | +-----------+------------+------------+--------------+ 3 rows in set (0.00 sec) 4.2.6、统计数据,聚合指令SELECT [field1,field2,……fieldn] fun_name FROM tablename [WHERE where_contition] [GROUP BY field1,field2,……fieldn [WITH ROLLUP]] [HAVING where_contition] 参数说明:
例1:查询统计 mysql> select count(1) from goodsorders; +----------+ | count(1) | +----------+ | 5 | +----------+ 1 row in set (0.00 sec) 例2:在此基础上,按照创建日期( mysql> select createtime,count(1) from goodsorders group by createtime; +------------+----------+ | createtime | count(1) | +------------+----------+ | 2020-03-11 | 1 | | 2020-03-12 | 1 | | 2020-05-12 | 1 | | 2021-03-11 | 2 | +------------+----------+ 4 rows in set (0.00 sec) 例3:在此基础上,既要按照创建日期( mysql> select createtime,count(1) from goodsorders group by createtime with rollup; +------------+----------+ | createtime | count(1) | +------------+----------+ | 2020-03-11 | 1 | | 2020-03-12 | 1 | | 2020-05-12 | 1 | | 2021-03-11 | 2 | | NULL | 5 | +------------+----------+ 5 rows in set (0.02 sec) 最有一行, 例4:按照创建日期( mysql> select createtime,count(1) from goodsorders group by createtime having count(1)>1; +------------+----------+ | createtime | count(1) | +------------+----------+ | 2021-03-11 | 2 | +------------+----------+ 1 row in set (0.00 sec) 例5:查询 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 sum(ordermoney),max(ordermoney),min(ordermoney) from goodsorders; +-----------------+-----------------+-----------------+ | sum(ordermoney) | max(ordermoney) | min(ordermoney) | +-----------------+-----------------+-----------------+ | 330.00 | 70.00 | 50.00 | +-----------------+-----------------+-----------------+ 1 row in set (0.02 sec) 4.2.7、表连接
例1:现在我们又创建一张用户表( mysql> select * from member; +------+------------+ | id | membername | +------+------------+ | 15 | zhang | | 1 | li | | 13 | liss | +------+------------+ 3 rows in set (0.00 sec) mysql> select * from goodsorders; +-----------+------------+------------+--------------+----------+ | ordername | createtime | ordermoney | ordernumbers | memberid | +-----------+------------+------------+--------------+----------+ | zhang | 2021-03-11 | 50.00 | 1 | 15 | | li | 2020-05-12 | 70.00 | 15 | 1 | | li | 2020-03-12 | 70.00 | 15 | 1 | | li | 2020-03-11 | 70.00 | 15 | 3 | | li | 2021-03-11 | 70.00 | 15 | 1 | +-----------+------------+------------+--------------+----------+ 5 rows in set (0.00 sec) mysql> select * from goodsorders left join member on goodsorders.memberid = member.id; +-----------+------------+------------+--------------+----------+------+------------+ | ordername | createtime | ordermoney | ordernumbers | memberid | id | membername | +-----------+------------+------------+--------------+----------+------+------------+ | zhang | 2021-03-11 | 50.00 | 1 | 15 | 15 | zhang | | li | 2020-05-12 | 70.00 | 15 | 1 | 1 | li | | li | 2020-03-12 | 70.00 | 15 | 1 | 1 | li | | li | 2021-03-11 | 70.00 | 15 | 1 | 1 | li | | li | 2020-03-11 | 70.00 | 15 | 3 | NULL | NULL | +-----------+------------+------------+--------------+----------+------+------------+ 5 rows in set (0.00 sec) 例2: mysql> select * from goodsorders right join member on goodsorders.memberid = member.id; +-----------+------------+------------+--------------+----------+------+------------+ | ordername | createtime | ordermoney | ordernumbers | memberid | id | membername | +-----------+------------+------------+--------------+----------+------+------------+ | zhang | 2021-03-11 | 50.00 | 1 | 15 | 15 | zhang | | li | 2020-05-12 | 70.00 | 15 | 1 | 1 | li | | li | 2020-03-12 | 70.00 | 15 | 1 | 1 | li | | li | 2021-03-11 | 70.00 | 15 | 1 | 1 | li | | NULL | NULL | NULL | NULL | NULL | 13 | liss | +-----------+------------+------------+--------------+----------+------+------------+ 5 rows in set (0.00 sec) 这里发生了翻转,变为左侧 4.2.8、子查询,相关关键字主要包括 例:从 mysql> select * from member; +------+------------+ | id | membername | +------+------------+ | 15 | zhang | | 1 | li | | 13 | liss | +------+------------+ 3 rows in set (0.00 sec) mysql> select * from goodsorders; +-----------+------------+------------+--------------+----------+ | ordername | createtime | ordermoney | ordernumbers | memberid | +-----------+------------+------------+--------------+----------+ | zhang | 2021-03-11 | 50.00 | 1 | 15 | | li | 2020-05-12 | 70.00 | 15 | 1 | | li | 2020-03-12 | 70.00 | 15 | 1 | | li | 2020-03-11 | 70.00 | 15 | 3 | | li | 2021-03-11 | 70.00 | 15 | 1 | +-----------+------------+------------+--------------+----------+ 5 rows in set (0.00 sec) mysql> select * from goodsorders where memberid in(select id from member); +-----------+------------+------------+--------------+----------+ | ordername | createtime | ordermoney | ordernumbers | memberid | +-----------+------------+------------+--------------+----------+ | zhang | 2021-03-11 | 50.00 | 1 | 15 | | li | 2020-05-12 | 70.00 | 15 | 1 | | li | 2020-03-12 | 70.00 | 15 | 1 | | li | 2021-03-11 | 70.00 | 15 | 1 | +-----------+------------+------------+--------------+----------+ 4 rows in set (0.05 sec) 4.2.9、记录联合,指令SELECT * FROM t1 UNION|UNION ALL SELECT * FROM t2 …… UNION|UNION ALL SELECT * FROM tn;
例1:将 mysql> select memberid from goodsorders union all select id from member; +----------+ | memberid | +----------+ | 15 | | 1 | | 1 | | 3 | | 1 | | 15 | | 1 | | 13 | +----------+ 8 rows in set (0.00 sec) 例2:如果希望将上面的结果去掉重复记录后显示 mysql> select memberid from goodsorders union select id from member; +----------+ | memberid | +----------+ | 15 | | 1 | | 3 | | 13 | +----------+ 4 rows in set (0.00 sec) 4.3、更新记录命令UPDATE tablename SET field1=value1,field2.=value2,……fieldn=valuen [WHERE CONDITION] 例:将表 mysql> update goodsorders set ordermoney=50.00 where ordername='zhang'; Query OK, 1 row affected (0.09 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from goodsorders; +-----------+------------+------------+--------------+ | ordername | createtime | ordermoney | ordernumbers | +-----------+------------+------------+--------------+ | zhang | 2021-05-12 | 50.00 | 1 | | zhang1 | 2021-05-12 | 1001.00 | 11 | +-----------+------------+------------+--------------+ 2 rows in set (0.00 sec) 更新时,如遇到错误代码1175:
解决方法: 1、先进行状体查询: show variables like 'SQL_SAFE_UPDATES'; 2、执行下面的sql,关闭safe-updates模式: SET SQL_SAFE_UPDATES = 0; 或者 SET SQL_SAFE_UPDATES = false; 4.4、删除记录命名DELETE FROM tablename [WHERE CONDITION] 例:将表 mysql> delete from goodsorders where ordername = 'zhang1'; Query OK, 1 row affected (0.06 sec) mysql> select * from goodsorders; +-----------+------------+------------+--------------+ | ordername | createtime | ordermoney | ordernumbers | +-----------+------------+------------+--------------+ | zhang | 2021-05-12 | 50.00 | 1 | +-----------+------------+------------+--------------+ 1 row in set (0.02 sec) 4.5、初始化表例:将表中的所有数据清空 mysql> select * from varc; +------+------+ | v | c | +------+------+ | abc | abc | +------+------+ 1 row in set (0.03 sec) mysql> truncate table varc; Query OK, 0 rows affected (0.25 sec) mysql> select * from varc; Empty set (0.00 sec) 5、DCL 语句
5.1创建数据库用户例:创建一个数据库用户 mysql> grant select,insert on ordermanage.* to 'user1'@'localhost' identified by '123'; Query OK, 0 rows affected, 1 warning (0.06 sec) mysql> exit Bye C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uuser1 -p123 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 82 Server version: 5.7.17-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | ordermanage | +--------------------+ 2 rows in set (0.00 sec) 在此基础上,将此用户( mysql> revoke insert on ordermanage.* from 'user1'@'localhost'; Query OK, 0 rows affected (0.02 sec) mysql> exit Bye C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -uuser1 -p123 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 84 Server version: 5.7.17-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use ordermanage; Database changed mysql> insert into member values('11','ss'); ERROR 1142 (42000): INSERT command denied to user 'user1'@'localhost' for table 'member' mysql> 由此可以看出插入权限不足,插入失败 到此这篇关于MYSQL表的介绍的文章就介绍到这了,更多相关MYSQL表内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论