第四模块:网络编程进阶&数据库开发 第2章·MySQL数据库开发
- 01-MySQL开篇
- 02-MySQL简单介绍
- 03-不同平台下安装MySQL
- 04-Windows平台MySQL密码设置与破解
- 05-Linux平台MySQL密码设置与破解
- 06-Mac平台MySQL密码设置与破解
- 07-统一字符编码
- 08-初识SQL语句
- 09-库操作
- 10-存储引擎介绍
- 11-表的增删改查
- 12-整数类型
- 13-日期类型
- 14-字符类型
- 15-枚举类型与集合类型
- 16-约束条件not null与default
- 17-约束条件unique key
- 18-约束条件promary key
- 19-约束条件auto_increment
- 20-约束条件之foreign key
- 21-表关系之多对一
- 22-表关系之多对多
- 23-表关系之一对一
- 24-记录的增删改查
- 25-简单查询
- 26-where约束
- 27-group by分组
- 28-having过滤
- 29-order by排序
- 30-limit限制条数
- 31-单表查询的语法顺序与执行顺序总结
- 32-正则查询
- 33-连表操作
- 34-select语句关键字执行优先级
- 35-子查询
- 36-多表查询练习
- 37-权限管理
- 38-Navicat工具的使用
- 39-pymysql模块之基本使用
- 40-pymysql模块之sql注入
- 41-pymysql模块之增删改查
- 42-MySQL内置功能介绍
- 43-视图
- 44-触发器
- 45-存储过程
- 46-应用程序与数据库结合使用的三种方式
- 47-事务
- 48-函数与流程控制
- 49-索引原理
01-MySQL开篇
1、不再直接进行文件操作;
2、MySQL数据库管理软件本质是C/S架构的socket;
3、使用别人的软件,按照数据库的规范进行,即引出SQL语句(structure query language)
02-MySQL简单介绍
1、数据库相关概念初识;
1)数据库服务器:一台计算机,用途非常专一,运行数据库管理软件(MySQL、Oracle、OceanBase)的计算机;
2)数据库管理软件:MySQL、Oracle、SQLServer、MariaDB、DB2、SQLite;
3)库:文件夹,我们组织文件的方式不应该放在同一个文件夹,而应该通过文件夹规范、层级、划分区域地进行管理;
4)表:文件,类似于Excel表;
5)记录:事物一系列典型的特征,比如age、name、sex、from、hobby、tel、position;
6)数据:描述事物特征的符号;
2、MySQL介绍
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司。MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。
1)MySQL是什么?
mysql就是一个基于socket编写的C/S架构的软件;
客户端软件
mysql自带:如mysql命令,mysqldump命令等;
python模块:如pymysql;
2)数据库管理软件分类
- 关系型(SQL):MySQL、Oracle、DB2、SQLServer
- 非关系型(No SQL-not only sql):Redis、Memcache、MongoDB
03-不同平台下安装MySQL
1、Windows平台安装教程;
1)下载链接:
msi版本:https://cdn.mysql.com//Downloads/MySQLInstaller/mysql-installer-community-5.7.22.1.msi (推荐使用)
zip免安装版本:https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.21-winx64.zip
2)安装教程:
https://www.linuxidc.com/Linux/2017-11/148521.htm?hmsr=toutiao.io
https://www.2cto.com/database/201803/732822.html
2、Linux平台下安装教程;
https://www.cnblogs.com/tqtl911/p/8453609.html
04-Windows平台MySQL密码设置与破解
1、参考博文;
https://www.cnblogs.com/yuwentims/p/9172463.html
https://www.2cto.com/database/201803/732822.html
05-Linux平台MySQL密码设置与破解
1、CentOS7下,忘记MySQL密码的修改方式教程:
https://www.cnblogs.com/shea/p/8075012.html
06-Mac平台MySQL密码设置与破解
1、MacOS平台下破解密码的方式;
07-统一字符编码
强调:配置文件中的注释可以有中文,但是配置项中不能出现中文;
1、Windows平台下(MySQL5.7在Windows平台下,默认就是统一为utf8):
2、Linux平台下;
解决办法:
1、先备份my.cnf文件;
cp -a /etc/my.cnf{,.ori}
2、添加记录;
vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0 log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character_set_server=utf8
3、重启mysqld服务;
systemctl restart mysqld
systemctl status mysqld
4、进行验证;
[root@iZ2ze2m3z176dpbiaolifiZ ~]# mysql -uroot -p -hlocalhost -P 3306
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22 MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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> \s
--------------
mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper Connection id: 3
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.22 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 35 sec Threads: 1 Questions: 15 Slow queries: 0 Opens: 105 Flush tables: 1 Open tables: 98 Queries per second avg: 0.428
-------------- mysql>
3、MacOS平台;
参考博文:https://www.jb51.net/article/136343.htm
08-初识SQL语句
1、操作文件夹(库);
1)增;
创建数据库:
create database 数据库名(Luffycity) charset utf8;
本质是在数据库的data目录下,创建一个名称为“数据库名”的文件夹;
2)查;
在创建的前面加上关键字show:
show create database 数据库名(luffycity);
3)改(没有数据库改名那么一说);
alter database 数据库名(luffycity) charset gbk;
4)删;
drop database 数据库名(luffycity);
show databases;
PS:在Windows平台下,使用.msi安装的数据库的数据存储目录如下:
C:\ProgramData\MySQL\MySQL Server 5.7\Data
2、操作文件(表);
1)首先要有这个文件夹(即数据库名);
2)本质是切换文件夹;
use 数据库名(luffycity);
3)查看当前所在文件夹;
select database();
4)新增一个表(文件);
create table 表名(userinfo)(id int,
name char
);
5)查询表;
show create table userinfo;
show tables;
6)改表结构;
alter table userinfo modify name char(30);
show create table userinfo;
alter table userinfo change name NAME char(7);
7) 查看表结构;
desc userinfo;
8) 删除表;
drop table userinfo;
3、操作文件内容(记录);
1)增;
insert into userinfo(id,name) values(1,'cuixiaozhao'),(2,'lijingping'),(3,'cuitianqing');
2)查;
select id,name from luffycity.userinfo;
select * from luffycity.userinfo#不推荐使用通配符*;
3)改
update luffycity.userinfo set name = '天晴天朗' where id =1;
update luffycity.userinfo set name = 'PythonFullStack';
4)删
delete from userinfo;
delete from userinfo where id =3;
操作日志:
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.7.20-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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 |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.00 sec) mysql> create database luffycity charset utf8;
Query OK, 1 row affected (0.00 sec) mysql> show create database luffycity;
+-----------+--------------------------------------------------------------------+
| Database | Create Database |
+-----------+--------------------------------------------------------------------+
| luffycity | CREATE DATABASE `luffycity` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+-----------+--------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| luffycity |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec) mysql> use luffycity;
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| luffycity |
+------------+
1 row in set (0.00 sec) mysql> create table userinfo(id int,name char);
Query OK, 0 rows affected (0.01 sec) mysql> show create table userinfo;
+----------+---------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+---------------------------------------------------------------------------------------------------------------------------+
| userinfo | CREATE TABLE `userinfo` (
`id` int(11) DEFAULT NULL,
`name` char(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+---------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec) mysql> show tables;
+---------------------+
| Tables_in_luffycity |
+---------------------+
| userinfo |
+---------------------+
1 row in set (0.00 sec) mysql> desc userinfo;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> alter table userinfo modify name char(11);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc userinfo;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> alter table userinfo change name NAME char(7);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc userinfo;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| NAME | char(7) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> drop table userinfo;
Query OK, 0 rows affected (0.01 sec) mysql> show tables;
Empty set (0.00 sec) mysql> create table userinfo(id int,name char);
Query OK, 0 rows affected (0.01 sec) mysql> show tables;
+---------------------+
| Tables_in_luffycity |
+---------------------+
| userinfo |
+---------------------+
1 row in set (0.00 sec) mysql> desc userinfo;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(1) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> insert into userinfo(id,name) values(1,'cuixiaozhao'),(2,'lijingping'),(3,'cuitianqing');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> alter table userinfo modify name char(30);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc userinfo;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(30) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> insert into userinfo(id,name) values(1,'cuixiaozhao'),(2,'lijingping'),(3,'cuitianqing');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> select id,name from userinfo;
+------+-------------+
| id | name |
+------+-------------+
| 1 | cuixiaozhao |
| 2 | lijingping |
| 3 | cuitianqing |
+------+-------------+
3 rows in set (0.00 sec) mysql> select id,name from luffycity.userinfo;
+------+-------------+
| id | name |
+------+-------------+
| 1 | cuixiaozhao |
| 2 | lijingping |
| 3 | cuitianqing |
+------+-------------+
3 rows in set (0.00 sec) mysql> select * from luffycity.userinfo;
+------+-------------+
| id | name |
+------+-------------+
| 1 | cuixiaozhao |
| 2 | lijingping |
| 3 | cuitianqing |
+------+-------------+
3 rows in set (0.00 sec) mysql> select * from userinfo;
+------+-------------+
| id | name |
+------+-------------+
| 1 | cuixiaozhao |
| 2 | lijingping |
| 3 | cuitianqing |
+------+-------------+
3 rows in set (0.00 sec) mysql> desc userinfo;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(30) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> select * from userinfo;
+------+-------------+
| id | name |
+------+-------------+
| 1 | cuixiaozhao |
| 2 | lijingping |
| 3 | cuitianqing |
+------+-------------+
3 rows in set (0.00 sec) mysql> update luffycity.userinfo set name = '天晴天朗' where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> update luffycity.userinfo set name = '小可爱·李静瓶' where id = 2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select id,name from userinfo;
+------+----------------------+
| id | name |
+------+----------------------+
| 1 | 天晴天朗 |
| 2 | 小可爱·李静瓶 |
| 3 | cuitianqing |
+------+----------------------+
3 rows in set (0.00 sec) mysql> select name,id from userinfo;
+----------------------+------+
| name | id |
+----------------------+------+
| 天晴天朗 | 1 |
| 小可爱·李静瓶 | 2 |
| cuitianqing | 3 |
+----------------------+------+
3 rows in set (0.00 sec) mysql> update luffycity.userinfo set name = 'PythonFullstack';
Query OK, 3 rows affected (0.07 sec)
Rows matched: 3 Changed: 3 Warnings: 0 mysql> select * from userinfo;
+------+-----------------+
| id | name |
+------+-----------------+
| 1 | PythonFullstack |
| 2 | PythonFullstack |
| 3 | PythonFullstack |
+------+-----------------+
3 rows in set (0.00 sec) mysql> delete from userinfo where id = 1;
Query OK, 1 row affected (0.00 sec) mysql> select * from userinfo;
+------+-----------------+
| id | name |
+------+-----------------+
| 2 | PythonFullstack |
| 3 | PythonFullstack |
+------+-----------------+
2 rows in set (0.00 sec) mysql> delete from userinfo where id = 3;
Query OK, 1 row affected (0.00 sec) mysql> delete from userinfo where id = 33;
Query OK, 0 rows affected (0.00 sec) mysql> select * from userinfo;
+------+-----------------+
| id | name |
+------+-----------------+
| 2 | PythonFullstack |
+------+-----------------+
1 row in set (0.00 sec) mysql> delete from userinfo;
Query OK, 1 row affected (0.00 sec) mysql> show create table userinfo;
+----------+----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+----------------------------------------------------------------------------------------------------------------------------+
| userinfo | CREATE TABLE `userinfo` (
`id` int(11) DEFAULT NULL,
`name` char(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> show tables;
+---------------------+
| Tables_in_luffycity |
+---------------------+
| userinfo |
+---------------------+
1 row in set (0.00 sec) mysql>
09-库操作
1、SQL语句;
对数据库进行查询和修改的操作语言叫做SQL。SQL的含义是“结构化查询语言”-structured Query Language。
SQL包含如下4个部分:
- 数据定义语言(DDL):DROP 、CREATE 、ALTER等语句;
- 数据操作语言(DML):INSERT(插入)、 UPDATE(修改)、 DELETE(删除)等语句;
- 数据查询语言(DQL):SELECT语句;
- 数据控制语言(DCL):GRANT、 REVOKE 、COMMIT、 RELLBACK等语句;
2、系统数据库介绍;
- mysql
- performance_schema
- sakila
- sys
- world
博文参考:https://blog.csdn.net/cainiao000001/article/details/80502549
3、创建数据库;
1)语法;
CREATE DATABASE luffycity CHARSET utf8;
2)数据名的命名规则;
可以由字母、数字、下划线、@、#、$但不推荐使用呢!;
区分大小写;
唯一性;
不能使用关键字如 create select;
不能单独使用数字;
最长128位;
博文参考:https://blog.csdn.net/java_mdzy/article/details/75304297
3)数据库操作;
查看数据库
show databases;
show create database db1;
select database();
选择数据库
USE 数据库名
删除数据库
DROP DATABASE 数据库名;
修改数据库
alter database db1 charset utf8;
4)数据库帮助help;
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.7.20-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> help For information about MySQL products and services, visit:
http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
https://shop.mysql.com/ List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for `help'.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
notee (\t) Don't write into outfile.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context. For server side help, type 'help contents' mysql> \h For information about MySQL products and services, visit:
http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
https://shop.mysql.com/ List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for `help'.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
notee (\t) Don't write into outfile.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context. For server side help, type 'help contents' mysql> \c
mysql> help create;
Many help items for your request exist.
To make a more specific request, please type 'help <item>',
where <item> is one of the following
topics:
CREATE DATABASE
CREATE EVENT
CREATE FUNCTION
CREATE FUNCTION UDF
CREATE INDEX
CREATE LOGFILE GROUP
CREATE PROCEDURE
CREATE SERVER
CREATE TABLE
CREATE TABLESPACE
CREATE TRIGGER
CREATE USER
CREATE VIEW
SHOW
SHOW CREATE DATABASE
SHOW CREATE EVENT
SHOW CREATE FUNCTION
SHOW CREATE PROCEDURE
SHOW CREATE TABLE
SHOW CREATE USER
SPATIAL mysql> help use;
Name: 'USE'
Description:
Syntax:
USE db_name The USE db_name statement tells MySQL to use the db_name database as
the default (current) database for subsequent statements. The database
remains the default until the end of the session or another USE
statement is issued: USE db1;
SELECT COUNT(*) FROM mytable; # selects from db1.mytable
USE db2;
SELECT COUNT(*) FROM mytable; # selects from db2.mytable URL: http://dev.mysql.com/doc/refman/5.7/en/use.html mysql> help insert;
Name: 'INSERT'
Description:
Syntax:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[(col_name [, col_name] ...)]
{VALUES | VALUE} (value_list) [, (value_list)] ...
[ON DUPLICATE KEY UPDATE assignment_list] INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
[PARTITION (partition_name [, partition_name] ...)]
SET assignment_list
[ON DUPLICATE KEY UPDATE assignment_list] INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[(col_name [, col_name] ...)]
SELECT ...
[ON DUPLICATE KEY UPDATE assignment_list] value:
{expr | DEFAULT} value_list:
value [, value] ... assignment:
col_name = value assignment_list:
assignment [, assignment] ... INSERT inserts new rows into an existing table. The INSERT ... VALUES
and INSERT ... SET forms of the statement insert rows based on
explicitly specified values. The INSERT ... SELECT form inserts rows
selected from another table or tables. INSERT with an ON DUPLICATE KEY
UPDATE clause enables existing rows to be updated if a row to be
inserted would cause a duplicate value in a UNIQUE index or PRIMARY
KEY. For additional information about INSERT ... SELECT and INSERT ... ON
DUPLICATE KEY UPDATE, see [HELP INSERT SELECT], and
http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html. In MySQL 5.7, the DELAYED keyword is accepted but ignored by the
server. For the reasons for this, see [HELP INSERT DELAYED], Inserting into a table requires the INSERT privilege for the table. If
the ON DUPLICATE KEY UPDATE clause is used and a duplicate key causes
an UPDATE to be performed instead, the statement requires the UPDATE
privilege for the columns to be updated. For columns that are read but
not modified you need only the SELECT privilege (such as for a column
referenced only on the right hand side of an col_name=expr assignment
in an ON DUPLICATE KEY UPDATE clause). When inserting into a partitioned table, you can control which
partitions and subpartitions accept new rows. The PARTITION option
takes a list of the comma-separated names of one or more partitions or
subpartitions (or both) of the table. If any of the rows to be inserted
by a given INSERT statement do not match one of the partitions listed,
the INSERT statement fails with the error Found a row not matching the
given partition set. For more information and examples, see
http://dev.mysql.com/doc/refman/5.7/en/partitioning-selection.html. URL: http://dev.mysql.com/doc/refman/5.7/en/insert.html mysql>
10-存储引擎介绍
1、什么是存储引擎?
- MySQL中创建的库==文件夹
- MySQL库中创建的表==文件
1)我们知道,现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处理文本用txt类型,处理表格用excel,处理图片用png等;
2)数据库中的表也应该有不同的类型,表的类型不同,会对应mysql不同的存取机制,表类型又称为存储引擎;
3)存储引擎说白了就是如何存储数据、如何为存储的数据建立索引和如何更新、查询数据等技术的实现方
法。因为在关系数据库中数据的存储是以表的形式存储的,所以存储引擎也可以称为表类型(即存储和
操作此表的类型)
4)在Oracle 和SQL Server等数据库中只有一种存储引擎,所有数据存储管理机制都是一样的。而MySql
数据库提供了多种存储引擎。用户可以根据不同的需求为数据表选择不同的存储引擎,用户也可以根据
自己的需要编写自己的存储引擎
SQL 解析器、SQL 优化器、缓冲池、存储引擎等组件在每个数据库中都存在,但不是每 个数据库都有这么多存储引擎。MySQL 的插件式存储引擎可以让存储引擎层的开发人员设 计他们希望的存储层,例如,有的应用需要满足事务的要求,有的应用则不需要对事务有这 么强的要求 ;有的希望数据能持久存储,有的只希望放在内存中,临时并快速地提供对数据 的查询。
小结:MySQL的存储引擎即”表的类型“,表即文件,也就是文件的类型,比如常用文件有txt、mp3、jpg;那么在MySQL中指不同的表类型;
2、MySQL支持的存储引擎;
show engines;
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec) mysql>
1)、InnoDB 存储引擎
支持事务,其设计目标主要面向联机事务处理(OLTP)的应用。其
特点是行锁设计、支持外键,并支持类似 Oracle 的非锁定读,即默认读取操作不会产生锁。 从 MySQL 5.5.8 版本开始是默认的存储引擎。
InnoDB 存储引擎将数据放在一个逻辑的表空间中,这个表空间就像黑盒一样由 InnoDB 存储引擎自身来管理。从 MySQL 4.1(包括 4.1)版本开始,可以将每个 InnoDB 存储引擎的 表单独存放到一个独立的 ibd 文件中。此外,InnoDB 存储引擎支持将裸设备(row disk)用 于建立其表空间。
InnoDB 通过使用多版本并发控制(MVCC)来获得高并发性,并且实现了 SQL 标准 的 4 种隔离级别,默认为 REPEATABLE 级别,同时使用一种称为 netx-key locking 的策略来 避免幻读(phantom)现象的产生。除此之外,InnoDB 存储引擎还提供了插入缓冲(insert buffer)、二次写(double write)、自适应哈希索引(adaptive hash index)、预读(read ahead) 等高性能和高可用的功能。
对于表中数据的存储,InnoDB 存储引擎采用了聚集(clustered)的方式,每张表都是按 主键的顺序进行存储的,如果没有显式地在表定义时指定主键,InnoDB 存储引擎会为每一 行生成一个 6 字节的 ROWID,并以此作为主键。
InnoDB 存储引擎是 MySQL 数据库最为常用的一种引擎,Facebook、Google、Yahoo 等 公司的成功应用已经证明了 InnoDB 存储引擎具备高可用性、高性能以及高可扩展性。对其 底层实现的掌握和理解也需要时间和技术的积累。如果想深入了解 InnoDB 存储引擎的工作 原理、实现和应用,可以参考《MySQL 技术内幕:InnoDB 存储引擎》一书。
2)、MyISAM 存储引擎
不支持事务、表锁设计、支持全文索引,主要面向一些 OLAP 数 据库应用,在 MySQL 5.5.8 版本之前是默认的存储引擎(除 Windows 版本外)。数据库系统 与文件系统一个很大的不同在于对事务的支持,MyISAM 存储引擎是不支持事务的。究其根 本,这也并不难理解。用户在所有的应用中是否都需要事务呢?在数据仓库中,如果没有 ETL 这些操作,只是简单地通过报表查询还需要事务的支持吗?此外,MyISAM 存储引擎的 另一个与众不同的地方是,它的缓冲池只缓存(cache)索引文件,而不缓存数据文件,这与 大多数的数据库都不相同。
3)、NDB 存储引擎
年,MySQL AB 公司从 Sony Ericsson 公司收购了 NDB 存储引擎。 NDB 存储引擎是一个集群存储引擎,类似于 Oracle 的 RAC 集群,不过与 Oracle RAC 的 share everything 结构不同的是,其结构是 share nothing 的集群架构,因此能提供更高级别的 高可用性。NDB 存储引擎的特点是数据全部放在内存中(从 5.1 版本开始,可以将非索引数 据放在磁盘上),因此主键查找(primary key lookups)的速度极快,并且能够在线添加 NDB 数据存储节点(data node)以便线性地提高数据库性能。由此可见,NDB 存储引擎是高可用、 高性能、高可扩展性的数据库集群系统,其面向的也是 OLTP 的数据库应用类型。
4)、Memory 存储引擎
正如其名,Memory 存储引擎中的数据都存放在内存中,数据库重 启或发生崩溃,表中的数据都将消失。它非常适合于存储 OLTP 数据库应用中临时数据的临时表,也可以作为 OLAP 数据库应用中数据仓库的维度表。Memory 存储引擎默认使用哈希 索引,而不是通常熟悉的 B+ 树索引。
5)、Infobright 存储引擎
第三方的存储引擎。其特点是存储是按照列而非行的,因此非常 适合 OLAP 的数据库应用。其官方网站是 http://www.infobright.org/,上面有不少成功的数据 仓库案例可供分析。
6)、NTSE 存储引擎
网易公司开发的面向其内部使用的存储引擎。目前的版本不支持事务, 但提供压缩、行级缓存等特性,不久的将来会实现面向内存的事务支持。
7)、BLACKHOLE
黑洞存储引擎,可以应用于主备复制中的分发主库。
MySQL 数据库还有很多其他存储引擎,上述只是列举了最为常用的一些引擎。如果 你喜欢,完全可以编写专属于自己的引擎,这就是开源赋予我们的能力,也是开源的魅 力所在。
3、使用存储引擎;
方法一:建表时候指定;
create table tb1(id int,name varchar(30))engine = InnoDB charset utf8;
create table tb2(id int,name varchar(30))engine = Memory charset utf8;
create table tb3(id int,name varchar(30))engine = Blackhole charset utf8;
create table tb4(id int,name varchar(30))engine = MyISAM charset utf8;
方法二:在配置文件中指定默认的存储引擎(编辑/etc/my.cnf)
[root@iZ2ze2m3z176dpbiaolifiZ ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0 log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
character_set_server=utf8
default-storage-engine=INNODB
innodb_file_per_table=1
[root@iZ2ze2m3z176dpbiaolifiZ ~]#
操作日志;
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.7.20-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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 |
| luffycity |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec) mysql> create database engines charset utf8;
Query OK, 1 row affected (0.00 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| engines |
| luffycity |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
8 rows in set (0.00 sec) mysql> use engines;
Database changed
mysql> creata table tb1(id int,name varchar(30))engine = InnoDB charset utf8;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'creata table tb1(id int,name varchar(30))engine = InnoDB charset utf8' at line 1
mysql> create table tb1(id int,name varchar(30))engine = InnoDB charset utf8;
Query OK, 0 rows affected (0.01 sec) mysql> create table tb2(id int,name varchar(30))engine = Memory charset utf8;
Query OK, 0 rows affected (0.00 sec) mysql> create table tb3(id int,name varchar(30))engine = Blackhole charset utf8;
Query OK, 0 rows affected (0.00 sec) mysql> create table tb4(id int,name varchar(30))engine = MyISAM charset utf8;
Query OK, 0 rows affected (0.00 sec) mysql> show tables;
+-------------------+
| Tables_in_engines |
+-------------------+
| tb1 |
| tb2 |
| tb3 |
| tb4 |
+-------------------+
4 rows in set (0.00 sec) mysql> show engines \G;
*************************** 1. row ***************************
Engine: InnoDB
Support: DEFAULT
Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
XA: YES
Savepoints: YES
*************************** 2. row ***************************
Engine: MRG_MYISAM
Support: YES
Comment: Collection of identical MyISAM tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 3. row ***************************
Engine: MEMORY
Support: YES
Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 4. row ***************************
Engine: BLACKHOLE
Support: YES
Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
XA: NO
Savepoints: NO
*************************** 5. row ***************************
Engine: MyISAM
Support: YES
Comment: MyISAM storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 6. row ***************************
Engine: CSV
Support: YES
Comment: CSV storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 7. row ***************************
Engine: ARCHIVE
Support: YES
Comment: Archive storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 8. row ***************************
Engine: PERFORMANCE_SCHEMA
Support: YES
Comment: Performance Schema
Transactions: NO
XA: NO
Savepoints: NO
*************************** 9. row ***************************
Engine: FEDERATED
Support: NO
Comment: Federated MySQL storage engine
Transactions: NULL
XA: NULL
Savepoints: NULL
9 rows in set (0.00 sec) ERROR:
No query specified mysql> show engines ;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec) mysql> show variables like 'storage_engine%';
Empty set, 1 warning (0.00 sec) mysql> show variables like 'storage_engine';
Empty set, 1 warning (0.00 sec) mysql> show variables like 'storage_engine%';
Empty set, 1 warning (0.00 sec) mysql> show variables like 'storage_engine';;
Empty set, 1 warning (0.00 sec) ERROR:
No query specified mysql> show variables like 'storage_engine';
Empty set, 1 warning (0.00 sec) mysql>
11-表的增删改查
一、表介绍;
表相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段;
总结:
- 行称之为记录
- 列称之为字段
二、创建表;
语法:
CREATE TABLE 表名(
字段名1,数据类型 [列级别约束条件] [默认值],
字段名2,数据类型 [列级别约束条件] [默认值],
......
[表级别约束条件]
);
示例:
CREATE DATABASE luffycity CHARSET utf8; USE luffycity; CREATE TABLE userinfo(
id int,
name varchar(50),
sex enum('男','女'),
age int(3)
); SHOW TABLES;
三、查看表结构;
DESCRIBE userinfo;
DESC userinfo;
SHOW CREATE TABLE userinfo\G;
四、修改表结构(修改表的结构而不是记录);
语法:
1、修改表名:
ALTER TABLE 表名 RENAME 新表名; 2、增加字段
ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件...],
ADD 字段名 数据类型 [完整性约束条件...],
ADD 字段名 数据类型 [完整性约束条件...]; ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件...] FIRST;
ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件...] AFTER 字段名; 3、删除字段
ALTER TABLE 表名 DROP 字段名; 4、修改字段 ALTER TABLE 表名 MODIFY 字段名 数据类型 [完整性约束条件...];
ALTER TABLE 表名 CHANGE 旧字段名 旧数据类型 [完整性约束条件...];
ALTER TABLE 表名 CHANGE 新字段名 新数据类型 [完整性约束条件...];
五、复制表;
复制表结构+记录 (key不会复制: 主键、外键和索引)
1、复制表结构和记录;
SELECT * FORM userinfo WHERE 1=2;#条件为假,查不到任何记录
CREATE TABLE copy_user SELECT host,user FROM mysql.user;#复制表结构和记录;
CREATE TABLE copy_user SELECT host,user FROM mysql.user WHERE 1=2;#条件为假,仅复制表结构;
2、like复制;
CREATE TABLE new_user LIKE mysql.user;
六、删除表;
DROP TABLE 表名;
操作日志:
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.7.20-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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 |
| engines |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec) mysql> CREATE DATABASE luffycity CHARSET utf8;
Query OK, 1 row affected (0.00 sec) mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| engines |
| luffycity |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
8 rows in set (0.00 sec) mysql> USE luffycity;
Database changed
mysql> SHOW TABLES;
Empty set (0.00 sec) mysql> CREATE TABLE userinfo();
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
mysql> CREATE TABLE userinfo(
-> id int,
-> name varchar(50),
-> sex enum('male','female'),
-> age int(3)
-> );
Query OK, 0 rows affected (0.02 sec) mysql> SHOW CREATE TABLE userinfo;
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| userinfo | CREATE TABLE `userinfo` (
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`sex` enum('male','female') DEFAULT NULL,
`age` int(3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> SHOW CREATE TABLE userinfo\G;
*************************** 1. row ***************************
Table: userinfo
Create Table: CREATE TABLE `userinfo` (
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`sex` enum('male','female') DEFAULT NULL,
`age` int(3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec) ERROR:
No query specified mysql> DESCRIBE userinfo;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec) mysql> DESC userinfo;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec) mysql> SELECT id,name,sex,age FROM userinfo;
Empty set (0.00 sec) mysql> SELECT * FORM userinfo;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FORM userinfo' at line 1
mysql> SELECT * FROM userinfo;
Empty set (0.00 sec) mysql> SELECT id,age,sex FROM userinfo;
Empty set (0.00 sec) mysql> INSERT INTO userinfo VALUES
-> (1,'cuixiaozhao',26,'male'),
-> (2,'lijingping',22,'male'),
-> (3,'gaozhifen',50,'female'),
-> (4,'cuiqingliang',50,'female');
ERROR 1265 (01000): Data truncated for column 'sex' at row 1
mysql> INSERT INTO userinfo VALUES
-> (1,'cuixiaozhao',26,'male'),
-> (2,'lijingping',22,'male');
ERROR 1265 (01000): Data truncated for column 'sex' at row 1
mysql> INSERT INTO userinfo VALUES
-> (1,'cuixiaozhao','male,26'),
-> (2,'lijingping','male,22'),
-> (3,'gaozhifen','female',50),
-> (4,'cuiqingliang','female',50);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> SHOW CREATE TABLE userinfo;
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| userinfo | CREATE TABLE `userinfo` (
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`sex` enum('male','female') DEFAULT NULL,
`age` int(3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> INSERT INTO userinfo VALUES
-> (1,'cuixiaozhao','male',26),
-> (2,'lijingping','female',22),
-> (3,'gaozhifen','female',50),
-> (4,'cuiqingliang',50,'male');
ERROR 1265 (01000): Data truncated for column 'sex' at row 4
mysql> INSERT INTO userinfo VALUES
-> (1,'cuixiaozhao','male',26),
-> (2,'lijingping','female',22),
-> (3,'gaozhifen','female',50),
-> (4,'cuiqingliang','male',50);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM userinfo;
+------+--------------+--------+------+
| id | name | sex | age |
+------+--------------+--------+------+
| 1 | cuixiaozhao | male | 26 |
| 2 | lijingping | female | 22 |
| 3 | gaozhifen | female | 50 |
| 4 | cuiqingliang | male | 50 |
+------+--------------+--------+------+
4 rows in set (0.00 sec) mysql> ALTER TABLE userinfo RENAME familyinfo;
Query OK, 0 rows affected (0.01 sec) mysql> SHOW TABLES;
+---------------------+
| Tables_in_luffycity |
+---------------------+
| familyinfo |
+---------------------+
1 row in set (0.00 sec) mysql> SELECT * FROM familyinfo;
+------+--------------+--------+------+
| id | name | sex | age |
+------+--------------+--------+------+
| 1 | cuixiaozhao | male | 26 |
| 2 | lijingping | female | 22 |
| 3 | gaozhifen | female | 50 |
| 4 | cuiqingliang | male | 50 |
+------+--------------+--------+------+
4 rows in set (0.00 sec) mysql> ALTER TABLE familyinfo ADD hometown varchar(50),tel int(11);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tel int(11)' at line 1
mysql> ALTER TABLE familyinfo ADD hometown varchar(50),ADD tel int(11);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> SHOW CRATE TABLE familyinfo;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CRATE TABLE familyinfo' at line 1
mysql> SHOW CREATE TABLE familyinfo;
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| familyinfo | CREATE TABLE `familyinfo` (
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`sex` enum('male','female') DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`hometown` varchar(50) DEFAULT NULL,
`tel` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> DESC familyinfo;
+----------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| hometown | varchar(50) | YES | | NULL | |
| tel | int(11) | YES | | NULL | |
+----------+-----------------------+------+-----+---------+-------+
6 rows in set (0.00 sec) mysql> SELECT * FORM familyinfo;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FORM familyinfo' at line 1
mysql> SELECT * FROM familyinfo;
+------+--------------+--------+------+----------+------+
| id | name | sex | age | hometown | tel |
+------+--------------+--------+------+----------+------+
| 1 | cuixiaozhao | male | 26 | NULL | NULL |
| 2 | lijingping | female | 22 | NULL | NULL |
| 3 | gaozhifen | female | 50 | NULL | NULL |
| 4 | cuiqingliang | male | 50 | NULL | NULL |
+------+--------------+--------+------+----------+------+
4 rows in set (0.00 sec) mysql> ALTER TABLE familyinfo ADD birth int(20) FIRST;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> SELECT * FORM familyinfo\G;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FORM familyinfo' at line 1
ERROR:
No query specified mysql> SELECT * FROM familyinfo\G;
*************************** 1. row ***************************
birth: NULL
id: 1
name: cuixiaozhao
sex: male
age: 26
hometown: NULL
tel: NULL
*************************** 2. row ***************************
birth: NULL
id: 2
name: lijingping
sex: female
age: 22
hometown: NULL
tel: NULL
*************************** 3. row ***************************
birth: NULL
id: 3
name: gaozhifen
sex: female
age: 50
hometown: NULL
tel: NULL
*************************** 4. row ***************************
birth: NULL
id: 4
name: cuiqingliang
sex: male
age: 50
hometown: NULL
tel: NULL
4 rows in set (0.00 sec) ERROR:
No query specified mysql> ALTER TABLE familyinfo ADD job varchar(40) AFTER hometown;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> ALTER TABLE familyinfo MODIFY job char(20);
Query OK, 4 rows affected (0.04 sec)
Records: 4 Duplicates: 0 Warnings: 0 mysql> ALTER TABLE familyinfo CHANGE tel telnum int(11);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> ALTER TABLE familyinfo CHANGE telnum telphone int(14);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> SHOW CREATE TABLE familyinfo;
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| familyinfo | CREATE TABLE `familyinfo` (
`birth` int(20) DEFAULT NULL,
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`sex` enum('male','female') DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`hometown` varchar(50) DEFAULT NULL,
`job` char(20) DEFAULT NULL,
`telphone` int(14) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> SHOW CREATE TABLE familyinfo\G;
*************************** 1. row ***************************
Table: familyinfo
Create Table: CREATE TABLE `familyinfo` (
`birth` int(20) DEFAULT NULL,
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`sex` enum('male','female') DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`hometown` varchar(50) DEFAULT NULL,
`job` char(20) DEFAULT NULL,
`telphone` int(14) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec) ERROR:
No query specified mysql> ALTER TABLE familyinfo ENGINE = MyISAM;
Query OK, 4 rows affected (0.02 sec)
Records: 4 Duplicates: 0 Warnings: 0 mysql> SHOW CREATE TABLE familyinfo\G;
*************************** 1. row ***************************
Table: familyinfo
Create Table: CREATE TABLE `familyinfo` (
`birth` int(20) DEFAULT NULL,
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`sex` enum('male','female') DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`hometown` varchar(50) DEFAULT NULL,
`job` char(20) DEFAULT NULL,
`telphone` int(14) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec) ERROR:
No query specified mysql> ALTER TABLE familyinfo ENGINE = InnoDB;
Query OK, 4 rows affected (0.03 sec)
Records: 4 Duplicates: 0 Warnings: 0 mysql> ALTER TABLE familyinfo MODIFY id int(11) not null PRIMARY KEY;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> SHOW CREATE TABLE familyinfo\G;
*************************** 1. row ***************************
Table: familyinfo
Create Table: CREATE TABLE `familyinfo` (
`birth` int(20) DEFAULT NULL,
`id` int(11) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`sex` enum('male','female') DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`hometown` varchar(50) DEFAULT NULL,
`job` char(20) DEFAULT NULL,
`telphone` int(14) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec) ERROR:
No query specified mysql> ALTER TABLE familyinfo MODIFY id int(11) not null PRIMARY KEY AUTO INCREMENT;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AUTO INCREMENT' at line 1
mysql> ALTER TABLE familyinfo MODIFY id int(11) not null PRIMARY KEY AUTO_INCREMENT;
ERROR 1068 (42000): Multiple primary key defined
mysql> ALTER TABLE familyinfo MODIFY id int(11) not null AUTO_INCREMENT;
Query OK, 4 rows affected (0.03 sec)
Records: 4 Duplicates: 0 Warnings: 0 mysql> SHOW CREATE TABLE familyinfo;
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| familyinfo | CREATE TABLE `familyinfo` (
`birth` int(20) DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`sex` enum('male','female') DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`hometown` varchar(50) DEFAULT NULL,
`job` char(20) DEFAULT NULL,
`telphone` int(14) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 |
+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> ALTER TABLE familyinfo DROP PRIMARY KEY;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
mysql> ALTER TABLE familyinfo DROP telphone;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> SHOW CTEATE TABLE familyinfo;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CTEATE TABLE familyinfo' at line 1
mysql> SHOW CREATE TABLE familyinfo;
+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| familyinfo | CREATE TABLE `familyinfo` (
`birth` int(20) DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`sex` enum('male','female') DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`hometown` varchar(50) DEFAULT NULL,
`job` char(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 |
+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql>
12-整数类型;
1、数值类型;
1)整数类型
- TINYINT 很小的整数~1个字节;
tinyint[(m)] [unsigned] [zerofill]
- SMALLINT 小的整数~2个字节;
int[(m)][unsigned][zerofill]
- MEDIUMINT 中等大小的整数~3个字节;
- INT 普通大小的整数~4个字节;
int[(m)][unsigned][zerofill]
- BIGINT 大整数~8个字节;
bigint[(m)][unsigned][zerofill]
用途:作用:存储年龄,等级,id,各种号码等;
2)浮点小数数据类型
- FLOAT
定义:
单精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。m最大值为255,d最大值为30
精确度:
**** 随着小数的增多,精度变得不准确 ****
用法:FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
- DOUBLE
定义:双精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。m最大值为255,d最大值为30
精确度:****随着小数的增多,精度比float要高,但也会变得不准确 ****
用法:DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]
作用:存储薪资、身高、体重、体质参数等
3)定点小数类型
- DECIMAL
定义:准确的小数值,m是数字总个数(负号不算),d是小数点后个数。 m最大值为65,d最大值为30。
精确度:**** 随着小数的增多,精度始终准确 ****,
对于精确数值计算时需要用此类型
decaimal能够存储精确值的原因在于其内部按照字符串存储。
注意:为该类型指定宽度时,仅仅只是指定查询结果的显示宽度,与存储范围无关,存储范围如下:
其实我们完全没必要为整数类型指定显示宽度,使用默认的就可以了;
默认的显示宽度,都是在最大值的基础上加1
有符号和无符号的最大数字需要的显示宽度均为10,而针对有符号的最小值则需要11位才能显示完全,所以int类型默认的显示宽度为11是非常合理的(因为加上1位符号)
13-日期类型
1、MySQL中表示日期的数据类型主要有;
- YEAR ~YYYY(1901/2155)
- TIME~YYYY--MM--DD(1000-01-01/9999-12-31)
- DATE~HH:MM:SS('-838:59:59/838:59:59')
- DATETIME~YYYY-MM-DD HH:MM:SS (1000-01-01 00:00:00/9999-12-31 23:59:59)
- TIMESTAMP~YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某时)
2、注意啦;
============注意啦,注意啦,注意啦===========
1. 单独插入时间时,需要以字符串的形式,按照对应的格式插入;如'2018'
2. 插入年份时,尽量使用4位值;
3. 插入两位年份时,<=69,以20开头,比如50, 结果2050
>=70,以19开头,比如71,结果1971
3、datetime和timestamp的区别;
1.DATETIME的日期范围是1001——9999年,TIMESTAMP的时间范围是1970——2038年。
2.DATETIME存储时间与时区无关,TIMESTAMP存储时间与时区有关,显示的值也依赖于时区。在mysql服务器,
操作系统以及客户端连接都有时区的设置。
3.DATETIME使用8字节的存储空间,TIMESTAMP的存储空间为4字节。因此,TIMESTAMP比DATETIME的空间利用率更高。
4.DATETIME的默认值为null;TIMESTAMP的字段默认不为空(not null),默认值为当前时间(CURRENT_TIMESTAMP),
如果不做特殊处理,并且update语句中没有指定该列的更新值,则默认更新为当前时间。
14-字符类型
1、char(定长)与varchar(变长);
#char类型:定长,简单粗暴,浪费空间,存取速度快
字符长度范围:0-255(一个中文是一个字符,是utf8编码的3个字节)
存储:
存储char类型的值时,会往右填充空格来满足长度
例如:指定长度为10,存>10个字符则报错,存<10个字符则用空格填充直到凑够10个字符存储 检索:
在检索或者说查询时,查出的结果会自动删除尾部的空格,除非我们打开pad_char_to_full_length SQL模式(SET sql_mode = 'PAD_CHAR_TO_FULL_LENGTH';) #varchar类型:变长,精准,节省空间,存取速度慢
字符长度范围:0-65535(如果大于21845会提示用其他类型 。mysql行最大限制为65535字节,字符编码为utf-8:https://dev.mysql.com/doc/refman/5.7/en/column-count-limit.html)
存储:
varchar类型存储数据的真实内容,不会用空格填充,如果'ab ',尾部的空格也会被存起来
强调:varchar类型会在真实数据前加1-2Bytes的前缀,该前缀用来表示真实数据的bytes字节数(1-2Bytes最大表示65535个数字,正好符合mysql对row的最大字节限制,即已经足够使用)
如果真实的数据<255bytes则需要1Bytes的前缀(1Bytes=8bit 2**8最大表示的数字为255)
如果真实的数据>255bytes则需要2Bytes的前缀(2Bytes=16bit 2**16最大表示的数字为65535) 检索:
尾部有空格会保存下来,在检索或者说查询时,也会正常显示包含空格在内的内容
- 1. char填充空格来满足固定长度,但是在查询时却会很不要脸地删除尾部的空格(装作自己好像没有浪费过空间一样),然后修改sql_mode让其现出原形;
- 2. 虽然 CHAR 和 VARCHAR 的存储方式不太相同,但是对于两个字符串的比较,都只比 较其值,忽略 CHAR 值存在的右填充,即使将 SQL _MODE 设置为 PAD_CHAR_TO_FULL_ LENGTH 也一样,,但这不适用于like;
小结:
1、大多数情况下,一般用char;
2、同一张表,不应该char与varchar共存,要么都是前者,要么都是后者;
3、在一张表中,char的数据类型应该靠前存放;
4、char和varchar括号内的参数指的是字符的长度;
#常用字符串系列:char与varchar
注:虽然varchar使用起来较为灵活,但是从整个系统的性能角度来说,char数据类型的处理速度更快,有时甚至可以超出varchar处理速度的50%。因此,用户在设计数据库时应当综合考虑各方面的因素,以求达到最佳的平衡 #其他字符串系列(效率:char>varchar>text)
TEXT系列 TINYTEXT TEXT MEDIUMTEXT LONGTEXT
BLOB 系列 TINYBLOB BLOB MEDIUMBLOB LONGBLOB
BINARY系列 BINARY VARBINARY text:text数据类型用于保存变长的大字符串,可以组多到65535 (2**16 − 1)个字符。
mediumtext:A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters.
longtext:A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1) characters.
15-枚举类型与集合类型
1、枚举类型与集合类型;
说明:字段的值,只能给给定范围中进行选择,如单选框,多选框;
- enum 单选,只能在给定的范围内选择一个值,如性别sex:男male,女female;
- set 多选,在给定的范围内,可以选择一个或一个以上的值,如(爱好1,爱好2,爱好3);
操作日志:
mysql> create table consumer();
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
mysql> create table consumer(
-> id int,
-> name char(16),
-> sex enum('male','female','other'),
-> level enum('vip1','vip2','vip3'),#指定范围内,多选一;
-> hobbies set('play','music','read','run')#在指定范围内,多选多,
-> );
Query OK, 0 rows affected (0.01 sec) mysql> show create table consumer;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| consumer | CREATE TABLE `consumer` (
`id` int(11) DEFAULT NULL,
`name` char(16) DEFAULT NULL,
`sex` enum('male','female','other') DEFAULT NULL,
`level` enum('vip1','vip2','vip3') DEFAULT NULL,
`hobbies` set('play','music','read','run') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> insert into consumer values (1,'egon','male','vip2','music,read');
Query OK, 1 row affected (0.00 sec) mysql> select * from consumer;
+------+------+------+-------+------------+
| id | name | sex | level | hobbies |
+------+------+------+-------+------------+
| 1 | egon | male | vip2 | music,read |
+------+------+------+-------+------------+
1 row in set (0.00 sec) mysql> insert into consumer values (1,'egon','xxxx','vip2','music,read');
ERROR 1265 (01000): Data truncated for column 'sex' at row 1
mysql>
16-约束条件not null与default
1、not null与default;
约束条件与数据类型的宽度一样,都是可选参数
作用:用于保证数据的完整性和一致性
主要分为:
PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录
FOREIGN KEY (FK) 标识该字段为该表的外键
NOT NULL 标识该字段不能为空
UNIQUE KEY (UK) 标识该字段的值是唯一的
AUTO_INCREMENT 标识该字段的值自动增长(整数类型,而且为主键)
DEFAULT 为该字段设置默认值 UNSIGNED 无符号
ZEROFILL 使用0填充
1. 是否允许为空,默认NULL,可设置NOT NULL,字段不允许为空,必须赋值
2. 字段是否有默认值,缺省的默认值是NULL,如果插入记录时不给字段赋值,此字段使用默认值
sex enum('male','female') not null default 'male'
age int unsigned NOT NULL default 20 必须为正值(无符号) 不允许为空 默认是20
3. 是否是key
主键 primary key
外键 foreign key
索引 (index,unique...)
是否可空,null表示空,非字符串;
not null - 不可空
null - 可空
默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值;
mysql> create table tb16(
-> id int,
-> name char(6),
-> sex enum('male','female')not null default 'male'
-> );
Query OK, 0 rows affected (0.01 sec) mysql> insert into t16(id,name) values(1,'egon');
ERROR 1146 (42S02): Table 'luffycity.t16' doesn't exist
mysql> insert into tb16(id,name) values(1,'egon');
Query OK, 1 row affected (0.00 sec) mysql> select * from tb16;
+------+------+------+
| id | name | sex |
+------+------+------+
| 1 | egon | male |
+------+------+------+
1 row in set (0.00 sec) mysql> insert into tb16(id,name) values (2,'alex');
Query OK, 1 row affected (0.00 sec) mysql> select * from tb16;
+------+------+------+
| id | name | sex |
+------+------+------+
| 1 | egon | male |
| 2 | alex | male |
+------+------+------+
2 rows in set (0.00 sec) mysql>
17-约束条件unique key
1、唯一约束unique;
- 方法一
create table department1(
id int,
name varchar(20) unique,
comment varchar(100)
);
- 方法二
create table department2(
id int,
name varchar(20),
comment varchar(100),
constraint uk_name unique(name)
);
- 特殊-联合唯一
mysql> create table services(
-> id int,
-> ip char(15),
-> port int,
-> unique(id),
-> unique(ip,port)
-> );
Query OK, 0 rows affected (0.02 sec) mysql> insert into services values
-> (1,'192.168.11.10',80),
-> (2,'192.168.11.11',81),
-> (3,'192.168.11.13',80);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> select 8 from services;
+---+
| 8 |
+---+
| 8 |
| 8 |
| 8 |
+---+
3 rows in set (0.00 sec) mysql> select * from services;
+------+---------------+------+
| id | ip | port |
+------+---------------+------+
| 1 | 192.168.11.10 | 80 |
| 2 | 192.168.11.11 | 81 |
| 3 | 192.168.11.13 | 80 |
+------+---------------+------+
3 rows in set (0.00 sec) mysql> insert into services values(4,'192.168.11.10',80);
ERROR 1062 (23000): Duplicate entry '192.168.11.10-80' for key 'ip'
mysql>
18-约束条件primary key
1、主键primary key(primary key字段的值不为空且唯一);
- 单列主键
mysql> create table tb17(
-> id int primary key,
-> name char(16)
-> );
Query OK, 0 rows affected (0.01 sec) mysql> insert into tb17 vlaues
-> (1,'egon'),
-> (2,'alex');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'vlaues
(1,'egon'),
(2,'alex')' at line 1
mysql> insert into tb17 vlaues
-> insert into tb17 values
-> (1,'egon'),
-> (2,'alex');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'vlaues
insert into tb17 values
(1,'egon'),
(2,'alex')' at line 1
mysql> insert into tb17 values
-> (1,'egon'),
-> (2,'alex');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from tb17;
+----+------+
| id | name |
+----+------+
| 1 | egon |
| 2 | alex |
+----+------+
2 rows in set (0.00 sec) mysql> insert into tb17 values (2,'cuixiaozhao');
ERROR 1062 (23000): Duplicate entry '' for key 'PRIMARY'
mysql> insert into tb17 values (1,'cuixiaozhao');
ERROR 1062 (23000): Duplicate entry '' for key 'PRIMARY'
mysql> insert into tb17 values (3,'cuixiaozhao');
Query OK, 1 row affected (0.00 sec) mysql> select * from tb17;
+----+-------------+
| id | name |
+----+-------------+
| 1 | egon |
| 2 | alex |
| 3 | cuixiaozhao |
+----+-------------+
3 rows in set (0.00 sec) mysql>
- 多列联合主键(复合主键)
mysql> create table tb19(
-> ip char(16),
-> port int,
-> primary key(ip,port)
-> );
Query OK, 0 rows affected (0.01 sec) mysql> insert into tb19 values('192.168.10.11',80);
Query OK, 1 row affected (0.00 sec) mysql> insert into tb19 values('192.168.10.11',81);
Query OK, 1 row affected (0.00 sec) mysql> insert into tb19 values('192.168.10.12',81);
Query OK, 1 row affected (0.00 sec) mysql> insert into tb19 values('192.168.10.13',81);
Query OK, 1 row affected (0.00 sec) mysql> insert into tb19 values('192.168.10.12',81);
ERROR 1062 (23000): Duplicate entry '192.168.10.12-81' for key 'PRIMARY'
mysql> select * from tb19;
+---------------+------+
| ip | port |
+---------------+------+
| 192.168.10.11 | 80 |
| 192.168.10.11 | 81 |
| 192.168.10.12 | 81 |
| 192.168.10.13 | 81 |
+---------------+------+
4 rows in set (0.00 sec) mysql>
注意:
1、对于InnoDB存储引擎来说,一张表内必须有一个字段是主键(如果没有,就自动创建隐藏字段);
2、但一个表内只能有一个主键primary key;
3、可以在字段后面指定primary key,亦可在最后指定primary key;
19-约束条件auto_increment
1、auto_increment自增长约束;
1)不指定id进行插入数据;
mysql> create table tb20(
-> id int primary key auto_increment,
-> name char(16)
-> );
Query OK, 0 rows affected (0.01 sec) mysql> desc tb20;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(16) | YES | | NULL | |
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.00 sec) mysql> insert into tb20 values('cuixiaozhao');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into tb20(name) values('cuixiaozhao');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb20(name) values('lijingping');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb20(name) values('gaozhifen');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb20(name) values('cuiqingliang');
Query OK, 1 row affected (0.00 sec) mysql> select * from tb20;
+----+--------------+
| id | name |
+----+--------------+
| 1 | cuixiaozhao |
| 2 | lijingping |
| 3 | gaozhifen |
| 4 | cuiqingliang |
+----+--------------+
4 rows in set (0.00 sec) mysql>
2)指定id位置进行插入数据;
mysql> insert into tb20(id,name) values(11,'cuixiaoshan');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb20(name) values('cuixiaosi);
'> insert into tb20(name) values('cuixiaosi);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cuixiaosi)' at line 1
mysql> insert into tb20(name) values('cuixiaosi');
Query OK, 1 row affected (0.00 sec) mysql> select * from tb20;
+----+--------------+
| id | name |
+----+--------------+
| 1 | cuixiaozhao |
| 2 | lijingping |
| 3 | gaozhifen |
| 4 | cuiqingliang |
| 11 | cuixiaoshan |
| 12 | cuixiaosi |
+----+--------------+
6 rows in set (0.00 sec) mysql>
show variables like 'auto_inc%';
mysql> show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
2 rows in set, 1 warning (0.00 sec)
3)步长概念auto_increment_increment;
4)起始位置的偏移量的概念auto_increment_offset;
5)设置步长;
set session auto_increment_increment = 5;临时生效
set global auto_increment_increment = 5;永久生效
6)设置起始偏移量;
set gloabl auto increment_offset = 3;
set session auto_increment_offset = 3;
mysql> show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
2 rows in set, 1 warning (0.00 sec) mysql> set session auto_increment_increment = 5;
Query OK, 0 rows affected (0.00 sec) mysql> set session auto_increment_offset = 6;
Query OK, 0 rows affected (0.00 sec) mysql> set session auto_increment_offset = 3;
Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 5 |
| auto_increment_offset | 3 |
+--------------------------+-------+
2 rows in set, 1 warning (0.00 sec) mysql> create table tb21(
-> id int primary key auto_increment,
-> name char(20)
-> );
Query OK, 0 rows affected (0.02 sec) mysql> insert into tb20(name) values('cxz');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb20(name) values('cxs');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb20(name) values('ljp');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb20(id,name) values(20,'ljp');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb20(id,name) values(21,'cxz');
Query OK, 1 row affected (0.00 sec) mysql> select * from tb21;
Empty set (0.00 sec) mysql> select * from tb20;
+----+--------------+
| id | name |
+----+--------------+
| 1 | cuixiaozhao |
| 2 | lijingping |
| 3 | gaozhifen |
| 4 | cuiqingliang |
| 11 | cuixiaoshan |
| 12 | cuixiaosi |
| 13 | cxz |
| 18 | cxs |
| 20 | ljp |
| 21 | cxz |
| 23 | ljp |
+----+--------------+
11 rows in set (0.00 sec) mysql> insert into tb21(name) values('cxz');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb21(name) values('cxs');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb21(id,name) values(30,'cxs');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb21(id,name) values(31,'cxz');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb21(id,name) values(32,'ljp');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb21(name) values('gzf');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb21(name) values('cql');
Query OK, 1 row affected (0.00 sec) mysql> select * from tb21;
+----+------+
| id | name |
+----+------+
| 3 | cxz |
| 8 | cxs |
| 30 | cxs |
| 31 | cxz |
| 32 | ljp |
| 33 | gzf |
| 38 | cql |
+----+------+
7 rows in set (0.00 sec) mysql>
2、清空表之delete与truncate;
1)注意不要使用delete来清空表,delete的用途是结合where等条件语句进行使用的;
2)清空表的数据理应使用truncate来操作,这样再次插入数据,索引值从最初的auto_increment_offset开始重新计数;
mysql> truncate tb21;
Query OK, 0 rows affected (0.01 sec) mysql> select * from tb21;
Empty set (0.00 sec) mysql> insert into tb21(name) values('cxz');
Query OK, 1 row affected (0.00 sec) mysql> insert into tb21(name) values('cql');
Query OK, 1 row affected (0.00 sec) mysql> select * from tb21;
+----+------+
| id | name |
+----+------+
| 3 | cxz |
| 8 | cql |
+----+------+
2 rows in set (0.00 sec) mysql> show variables like 'auto_incre%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 5 |
| auto_increment_offset | 3 |
+--------------------------+-------+
2 rows in set, 1 warning (0.00 sec) mysql>
注意:
起始偏移量要小于等于步长的值,要不然步子太大,扯着淡了。
先设置起始偏移量,再设置步长;
20-约束条件之foreign key
1、什么是外键约束foreign key?
foreign key:建立表之间的关系;
#1、建立表关系;
先建立被关联的表,并且保证被关联的表的字段唯一(unique、primary key)
create table dep(
id int primary key,
name char(50),
comment char(50)
);
再建立关联的表;
create table emp(
id int primary key,
name char(50),
sex enum('male','female'),
dep_id int,
foreign key(dep_id) references dep(id) on delete cascade on update cascade
);
#先往被关联表插入数据;
insert into dep values(1,'IT','技术能力有限部门'),(2,'销售','销售能力有限部门'),(3,'财务','花钱特别多的部门');
insert into emp values
(1,'cuixiaozhao','male',1),
(2,'lijingping','female',3),
(3,'cuiqingliang','male',2),
(4,'gaozhifen','female',2);
注意:
1、foreign key 要慎用,后续扩展非常麻烦;
2、通过应用程序的逻辑层面来实现;
3、涉及到数据库的变动,代码多数要重构;
4、表之间的3种关系——多对一、多多对、一对一;
21-表关系之多对一
1、表关系之多对一;
- 出版社(一)
- 书(多-press_id int,foreign key(press_id) refrence )
#一对多或称为多对一
三张表:出版社,作者信息,书 一对多(或多对一):一个出版社可以出版多本书 关联方式:foreign key
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.7.20-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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 |
| engines |
| luffycity |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
8 rows in set (0.00 sec) mysql> use luffycity;
Database changed
mysql> show tables;
+---------------------+
| Tables_in_luffycity |
+---------------------+
| consumer |
| dep |
| emp |
| familyinfo |
| info |
| services |
| student |
| tb16 |
| tb17 |
| tb19 |
| tb20 |
| tb21 |
| user |
+---------------------+
13 rows in set (0.00 sec) mysql> create table press(
-> id int primary key auto_increment ,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.01 sec) mysql> create table book(
-> id int primary key auto_increment,
-> name varchar(20),
-> press_id int not null,
-> foreign key(press_id) references press(id) on delete cascade on update cascade
-> );
Query OK, 0 rows affected (0.02 sec) mysql> insert into press(name) values('北京工业地雷出版社'),('人民音乐不好听出版社'),('知识产权没有用出版社');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> insert into book(name,press_id) values
-> ('九阳神功',1),
-> ('九阴真经',2),
-> ('九阴白骨爪',3),
-> ('独孤九剑',3),
-> ('降龙十巴掌',2),
-> ('葵花宝典',3);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',
('九阴白骨爪',3),
('独孤九剑',3),
('降龙十巴掌',2),
('葵' at line 3
mysql> insert into book(name,press_id) values
-> ('九阳神功',1),
-> ('九阴真经',2),
-> ('九阴白骨爪',3),
-> ('独孤九剑',3),
-> ('降龙十巴掌',2),
-> ('葵花宝典',3);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',
('九阴白骨爪',3),
('独孤九剑',3),
('降龙十巴掌',2),
('葵花' at line 3
mysql> insert into book(name,press_id) values
-> ('九阳神功',1),
-> ('九阴真经',2),
-> ('九阴白骨爪',3),
-> ('独孤九剑',3),
-> ('降龙十巴掌',2),
-> ('葵花宝典',3);
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0 mysql> select * from press;
+----+--------------------------------+
| id | name |
+----+--------------------------------+
| 1 | 北京工业地雷出版社 |
| 2 | 人民音乐不好听出版社 |
| 3 | 知识产权没有用出版社 |
+----+--------------------------------+
3 rows in set (0.00 sec) mysql> select * from book;
+----+-----------------+----------+
| id | name | press_id |
+----+-----------------+----------+
| 1 | 九阳神功 | 1 |
| 2 | 九阴真经 | 2 |
| 3 | 九阴白骨爪 | 3 |
| 4 | 独孤九剑 | 3 |
| 5 | 降龙十巴掌 | 2 |
| 6 | 葵花宝典 | 3 |
+----+-----------------+----------+
6 rows in set (0.00 sec) mysql>
22-表关系之多对多
1、多对多(即双向的多对一);
mysql> desc book;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| press_id | int(11) | NO | MUL | NULL | |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec) mysql> desc author;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec) mysql> create table author2book(
-> id int not null unique auto_increment,
-> author_id int not null,
-> book_id int not null,
-> constraint fk_author foreign key(author_id) references author(id) on delete cascade on update cascade,
-> constraint fk_book foreign key(book_id) references book(id) on delete cascade on update cascade,
-> primary key(author_id,book_id)
-> );
Query OK, 0 rows affected (0.01 sec) mysql>
mysql>
mysql> insert into author(name) values('egon'),('alex'),('yuanhao'),('wpq');
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0 mysql> insert into author2book(author_id,book_id) values
-> (1,2),
-> (1,2),
-> (1,3),
-> (1,4),
-> (1,5),
-> (2,1),
-> (2,6),
-> (3,4),
-> (3,5),
-> (3,6),
-> (4,1)
-> );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 13
mysql> insert into author2book(author_id,book_id) values
-> (
-> (1,2),
-> (1,2),
-> (1,3),
-> (1,4),
-> (1,5),
-> (2,1),
-> (2,6),
-> (3,4),
-> (3,5),
-> (3,6),
-> (4,1)
-> );
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> show tables;
+---------------------+
| Tables_in_luffycity |
+---------------------+
| author |
| author2book |
| book |
| consumer |
| dep |
| emp |
| familyinfo |
| info |
| press |
| services |
| student |
| tb16 |
| tb17 |
| tb19 |
| tb20 |
| tb21 |
| user |
+---------------------+
17 rows in set (0.00 sec) mysql> desc author2book
-> ;
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | UNI | NULL | auto_increment |
| author_id | int(11) | NO | PRI | NULL | |
| book_id | int(11) | NO | PRI | NULL | |
+-----------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec) mysql> insert into author2book(author_id,book_id) values
-> (
-> (1,2),
-> (1,2),
-> (1,3),
-> (1,4),
-> (1,5),
-> (2,1),
-> (2,6),
-> (3,4),
-> (3,5),
-> (3,6),
-> (4,1)
-> );
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> desc book;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| press_id | int(11) | NO | MUL | NULL | |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec) mysql> desc author;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec) mysql> insert into author2book(author_id,book_id) values
->
-> (1,2),
-> (1,2),
-> (1,3),
-> (1,4),
-> (1,5),
-> (2,1),
-> (2,6),
-> (3,4),
-> (3,5),
-> (3,6),
-> (4,1);
ERROR 1062 (23000): Duplicate entry '1-2' for key 'PRIMARY'
mysql> insert into author2book(author_id,book_id) values
-> (1,2),
-> (1,2),
-> (1,3),
-> (1,4),
-> (1,5),
-> (2,1),
-> (2,6),
-> (3,4),
-> (3,5),
-> (3,6),
-> (4,1)
-> ;
ERROR 1062 (23000): Duplicate entry '1-2' for key 'PRIMARY'
mysql> insert into author2book(author_id,book_id) values
-> (1,2),
-> (1,2),
-> (1,3),
-> (1,4),
-> (1,5),
-> (2,1),
-> (2,6),
-> (3,4),
-> (3,5),
-> (3,6),
-> (4,1)
-> ;^C
mysql> insert into author2book(author_id,book_id) values
-> (1,1),
-> (1,2),
-> (1,3),
-> (1,4),
-> (1,5),
-> (2,1),
-> (2,6),
-> (3,4),
-> (3,5),
-> (3,6),
-> (4,1)
-> ;
Query OK, 11 rows affected (0.00 sec)
Records: 11 Duplicates: 0 Warnings: 0 mysql>
23-表关系之一对一
1、表关系之一对一;
mysql> create table customer(
-> id int primary key auto_increment,
-> name varchar(20) not null,
-> qq varchar(10) not null,
-> phone char(16) not null
-> );
ERROR 1050 (42S01): Table 'customer' already exists
mysql>
mysql> create table student(
-> id int primary key auto_increment,
-> class_name varchar(20) not null,
-> customer_id int unique,
-> foreign key(customer_id) references customer(id) on delete cascade on update cascade
-> );
Query OK, 0 rows affected (0.01 sec) mysql> desc student;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| class_name | varchar(20) | NO | | NULL | |
| customer_id | int(11) | YES | UNI | NULL | |
+-------------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec) mysql> desc customer;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| qq | varchar(10) | NO | | NULL | |
| phone | char(16) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec) mysql> insert into customer(name,qq,phone) values
-> ('李飞机','',13811341220),
-> ('王大炮','',15213146809),
-> ('守榴弹','',1867141331),
-> ('吴坦克','',1851143312),
-> ('赢火箭','',1861243314),
-> ('战地雷','',18811431230)
-> ;
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0 mysql> insert into student(class_name,customer_id) values
-> ('脱产3班',3),
-> ('周末19期',4),
-> ('周末19期',5)
-> ;
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-------------+-------------+
| id | class_name | customer_id |
+----+-------------+-------------+
| 1 | 脱产3班 | 3 |
| 2 | 周末19期 | 4 |
| 3 | 周末19期 | 5 |
+----+-------------+-------------+
3 rows in set (0.00 sec)
mysql> select * from customer;
+----+-----------+-----------+-------------+
| id | name | qq | phone |
+----+-----------+-----------+-------------+
| 1 | 李飞机 | 31811231 | 13811341220 |
| 2 | 王大炮 | 123123123 | 15213146809 |
| 3 | 守榴弹 | 283818181 | 1867141331 |
| 4 | 吴坦克 | 283818181 | 1851143312 |
| 5 | 赢火箭 | 888818181 | 1861243314 |
| 6 | 战地雷 | 112312312 | 18811431230 |
+----+-----------+-----------+-------------+
6 rows in set (0.00 sec)
mysql>
24-记录的增删改查
1、插入数据INSERT;
1、插入完整数据(顺序插入)
INSERT INTO 表名(字段1,字段2,字段3...字段n) VALUES(值1,值2,值3...值n); INSERT INTO 表名 VALUES(值1,值2,值2...值n); 2、指定字段插入数据 INSERT INTO 表名(字段1,字段2,字段3...) VALUES (值1,值2,值3); 3、插入多条记录 INSERT INTO 表名 VALUES
(值1,值2,值3...值n),
(值1,值2,值3...值n),
(值1,值2,值3...值n); 4、插入查询后的结果 INSERT INTO 表名 (字段1,字段2,字段3...字段n) SELECT (字段1,字段2,字段3...字段n) FROM 表2 WHERE XXX == 'XXXX';
2、更新数据UPDATE;
语法:
UPDATE 表名 SET
字段1 = 值1,
字段2 = 值2,
WHERE CONDITION(条件); 示例:
UPDATE mysql.user SET password = password('Ab123456.')
WHERE user = 'root' and host = 'localhost';
3、删除数据DELETE;
语法:
DELETE FROM 表名 WHERE CONDITION(条件); 示例:
DELETE FROM mysql.user WHERE password = 'xxxx';
4、查询数据SELECT;
小结:
对数据库的操作,查询语句居多;
1、单表查询;
select distinct 字段1,字段2,字段3 from 库名.表名 where 条件 group by 分组条件 having 过滤 order by 排序 limit 限制条数;
2、多表查询;
3、查询通过步骤;
1、找到带查询的表名:from
2、拿着where指定的约束条件,去文件/表中取出一条条记录
3、将取出的一条条记录进行分组group by ,如果没有group by,则整体作为一组
4、将分组的结果进行having 过滤
5、执行select
6、进行distinct去重
7、将结果按照条件排序order by(默认asc,指定desc)
8、限制结果的显示条数
25-简单查询
1、表设计;
company.employee
员工id id int
姓名 emp_name varchar
性别 sex enum
年龄 age int
入职日期 hire_date date
岗位 post varchar
职位描述 post_comment varchar
薪水 salary double
办公室 office int
部门编号 depart_id int
2、查询场景举例;
- 单字段简单查询;
- 多字段组合查询,逗号分隔字段名称,as起个别名;
- 避免重复DISTINCT
- 通过四则运算查询
- CONCAT()定义显示格式(类Python中的格式化输出)
3、操作日志;
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.7.20-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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 |
| engines |
| luffycity |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
8 rows in set (0.00 sec) mysql> create database luffy_practise charset utf8;
Query OK, 1 row affected (0.00 sec) mysql> show create database luffy_practise;
+----------------+-------------------------------------------------------------------------+
| Database | Create Database |
+----------------+-------------------------------------------------------------------------+
| luffy_practise | CREATE DATABASE `luffy_practise` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------------+-------------------------------------------------------------------------+
1 row in set (0.00 sec) mysql> use luffy_practise;
Database changed
mysql> create table employee(
-> id int not null unique auto_increment,
-> name varchar(20) not null,
-> sex enum('male','female') not null default 'male',
-> age int(3) unsigned not null default 28,
-> hire_date date not null,
-> post_comment varchar(100),
-> salary double(15,2),
-> office int,
-> depart_id int
-> );
Query OK, 0 rows affected (0.01 sec) mysql> desc employee;
+--------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| sex | enum('male','female') | NO | | male | |
| age | int(3) unsigned | NO | | 28 | |
| hire_date | date | NO | | NULL | |
| post_comment | varchar(100) | YES | | NULL | |
| salary | double(15,2) | YES | | NULL | |
| office | int(11) | YES | | NULL | |
| depart_id | int(11) | YES | | NULL | |
+--------------+-----------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec) mysql> alter table add post varchar(50) after hire_date;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'add post varchar(50) after hire_date' at line 1
mysql> alter table employee add post varchar(50) after hire_date;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc employee;
+--------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| sex | enum('male','female') | NO | | male | |
| age | int(3) unsigned | NO | | 28 | |
| hire_date | date | NO | | NULL | |
| post | varchar(50) | YES | | NULL | |
| post_comment | varchar(100) | YES | | NULL | |
| salary | double(15,2) | YES | | NULL | |
| office | int(11) | YES | | NULL | |
| depart_id | int(11) | YES | | NULL | |
+--------------+-----------------------+------+-----+---------+----------------+
10 rows in set (0.00 sec) mysql> insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
-> ('egon','male',18,'','老男孩驻沙河办事处外交大使',7300.33,401,1),
-> ('alex','male',78,'','teacher',1000000.31,401,1),
-> ('wupeiqi','male',81,'','teacher',8300,401,1),
-> ('yuanhao','male',73,'','teacher',3500,401,1),
-> ('liwenzhou','male',28,'','teacher',2100,401,1),
-> ('jingliyang','female',18,'','teacher',9000,401,1),
-> ('jinxin','male',18,'','teacher',30000,401,1),
-> ('成龙','male',48,'','teacher',10000,401,1),
->
-> ('歪歪','female',48,'','sale',3000.13,402,2),
-> ('丫丫','female',38,'','sale',2000.35,402,2),
-> ('丁丁','female',18,'','sale',1000.37,402,2),
-> ('星星','female',18,'','sale',3000.29,402,2),
-> ('格格','female',28,'','sale',4000.33,402,2),
->
-> ('张野','male',28,'','operation',10000.13,403,3),
-> ('程咬金','male',18,'','operation',20000,403,3),
-> ('程咬银','female',18,'','operation',19000,403,3),
-> ('程咬铜','male',18,'','operation',18000,403,3),
-> ('程咬铁','female',18,'','operation',17000,403,3)
-> ;
Query OK, 18 rows affected (0.00 sec)
Records: 18 Duplicates: 0 Warnings: 0 mysql> select * from employee;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 |
| 5 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 |
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
| 8 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
| 11 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 |
| 16 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 |
| 17 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 |
| 18 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec) mysql> select id,name,sex,age,hire_date,post_comment,salary,office,depart_id from employee;
+----+------------+--------+-----+------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | NULL | 8300.00 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | NULL | 3500.00 | 401 | 1 |
| 5 | liwenzhou | male | 28 | 2012-11-01 | NULL | 2100.00 | 401 | 1 |
| 6 | jingliyang | female | 18 | 2011-02-11 | NULL | 9000.00 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | NULL | 30000.00 | 401 | 1 |
| 8 | 成龙 | male | 48 | 2010-11-11 | NULL | 10000.00 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | NULL | 2000.35 | 402 | 2 |
| 11 | 丁丁 | female | 18 | 2011-03-12 | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | NULL | 4000.33 | 402 | 2 |
| 14 | 张野 | male | 28 | 2016-03-11 | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | NULL | 20000.00 | 403 | 3 |
| 16 | 程咬银 | female | 18 | 2013-03-11 | NULL | 19000.00 | 403 | 3 |
| 17 | 程咬铜 | male | 18 | 2015-04-11 | NULL | 18000.00 | 403 | 3 |
| 18 | 程咬铁 | female | 18 | 2014-05-12 | NULL | 17000.00 | 403 | 3 |
+----+------------+--------+-----+------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec) mysql> select id,name,sex from employee;
+----+------------+--------+
| id | name | sex |
+----+------------+--------+
| 1 | egon | male |
| 2 | alex | male |
| 3 | wupeiqi | male |
| 4 | yuanhao | male |
| 5 | liwenzhou | male |
| 6 | jingliyang | female |
| 7 | jinxin | male |
| 8 | 成龙 | male |
| 9 | 歪歪 | female |
| 10 | 丫丫 | female |
| 11 | 丁丁 | female |
| 12 | 星星 | female |
| 13 | 格格 | female |
| 14 | 张野 | male |
| 15 | 程咬金 | male |
| 16 | 程咬银 | female |
| 17 | 程咬铜 | male |
| 18 | 程咬铁 | female |
+----+------------+--------+
18 rows in set (0.00 sec) mysql> select distinct id,name,name,age,sex from employee;
+----+------------+------------+-----+--------+
| id | name | name | age | sex |
+----+------------+------------+-----+--------+
| 1 | egon | egon | 18 | male |
| 2 | alex | alex | 78 | male |
| 3 | wupeiqi | wupeiqi | 81 | male |
| 4 | yuanhao | yuanhao | 73 | male |
| 5 | liwenzhou | liwenzhou | 28 | male |
| 6 | jingliyang | jingliyang | 18 | female |
| 7 | jinxin | jinxin | 18 | male |
| 8 | 成龙 | 成龙 | 48 | male |
| 9 | 歪歪 | 歪歪 | 48 | female |
| 10 | 丫丫 | 丫丫 | 38 | female |
| 11 | 丁丁 | 丁丁 | 18 | female |
| 12 | 星星 | 星星 | 18 | female |
| 13 | 格格 | 格格 | 28 | female |
| 14 | 张野 | 张野 | 28 | male |
| 15 | 程咬金 | 程咬金 | 18 | male |
| 16 | 程咬银 | 程咬银 | 18 | female |
| 17 | 程咬铜 | 程咬铜 | 18 | male |
| 18 | 程咬铁 | 程咬铁 | 18 | female |
+----+------------+------------+-----+--------+
18 rows in set (0.00 sec) mysql> select distinct post from employee;
+-----------------------------------------+
| post |
+-----------------------------------------+
| 老男孩驻沙河办事处外交大使 |
| teacher |
| sale |
| operation |
+-----------------------------------------+
4 rows in set (0.00 sec) mysql> select post from employee;
+-----------------------------------------+
| post |
+-----------------------------------------+
| 老男孩驻沙河办事处外交大使 |
| teacher |
| teacher |
| teacher |
| teacher |
| teacher |
| teacher |
| teacher |
| sale |
| sale |
| sale |
| sale |
| sale |
| operation |
| operation |
| operation |
| operation |
| operation |
+-----------------------------------------+
18 rows in set (0.00 sec) mysql> select distinct post from employee;
+-----------------------------------------+
| post |
+-----------------------------------------+
| 老男孩驻沙河办事处外交大使 |
| teacher |
| sale |
| operation |
+-----------------------------------------+
4 rows in set (0.00 sec) mysql> select * from employee;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 |
| 5 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 |
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
| 8 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
| 11 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 |
| 16 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 |
| 17 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 |
| 18 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec) mysql> select name,salary from employee;
+------------+------------+
| name | salary |
+------------+------------+
| egon | 7300.33 |
| alex | 1000000.31 |
| wupeiqi | 8300.00 |
| yuanhao | 3500.00 |
| liwenzhou | 2100.00 |
| jingliyang | 9000.00 |
| jinxin | 30000.00 |
| 成龙 | 10000.00 |
| 歪歪 | 3000.13 |
| 丫丫 | 2000.35 |
| 丁丁 | 1000.37 |
| 星星 | 3000.29 |
| 格格 | 4000.33 |
| 张野 | 10000.13 |
| 程咬金 | 20000.00 |
| 程咬银 | 19000.00 |
| 程咬铜 | 18000.00 |
| 程咬铁 | 17000.00 |
+------------+------------+
18 rows in set (0.00 sec) mysql> select name,salary*12 from employee;
+------------+-------------+
| name | salary*12 |
+------------+-------------+
| egon | 87603.96 |
| alex | 12000003.72 |
| wupeiqi | 99600.00 |
| yuanhao | 42000.00 |
| liwenzhou | 25200.00 |
| jingliyang | 108000.00 |
| jinxin | 360000.00 |
| 成龙 | 120000.00 |
| 歪歪 | 36001.56 |
| 丫丫 | 24004.20 |
| 丁丁 | 12004.44 |
| 星星 | 36003.48 |
| 格格 | 48003.96 |
| 张野 | 120001.56 |
| 程咬金 | 240000.00 |
| 程咬银 | 228000.00 |
| 程咬铜 | 216000.00 |
| 程咬铁 | 204000.00 |
+------------+-------------+
18 rows in set (0.00 sec) mysql> select name,salary*12 as annunal_salary from employee;
+------------+----------------+
| name | annunal_salary |
+------------+----------------+
| egon | 87603.96 |
| alex | 12000003.72 |
| wupeiqi | 99600.00 |
| yuanhao | 42000.00 |
| liwenzhou | 25200.00 |
| jingliyang | 108000.00 |
| jinxin | 360000.00 |
| 成龙 | 120000.00 |
| 歪歪 | 36001.56 |
| 丫丫 | 24004.20 |
| 丁丁 | 12004.44 |
| 星星 | 36003.48 |
| 格格 | 48003.96 |
| 张野 | 120001.56 |
| 程咬金 | 240000.00 |
| 程咬银 | 228000.00 |
| 程咬铜 | 216000.00 |
| 程咬铁 | 204000.00 |
+------------+----------------+
18 rows in set (0.00 sec) mysql> select * from employee;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 |
| 5 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 |
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
| 8 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
| 11 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 |
| 16 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 |
| 17 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 |
| 18 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec) mysql> select concat('姓名:',name,'年薪:',salary*12) AS Annual_salary from employee;
+-----------------------------------+
| Annual_salary |
+-----------------------------------+
| 姓名:egon年薪:87603.96 |
| 姓名:alex年薪:12000003.72 |
| 姓名:wupeiqi年薪:99600.00 |
| 姓名:yuanhao年薪:42000.00 |
| 姓名:liwenzhou年薪:25200.00 |
| 姓名:jingliyang年薪:108000.00 |
| 姓名:jinxin年薪:360000.00 |
| 姓名:成龙年薪:120000.00 |
| 姓名:歪歪年薪:36001.56 |
| 姓名:丫丫年薪:24004.20 |
| 姓名:丁丁年薪:12004.44 |
| 姓名:星星年薪:36003.48 |
| 姓名:格格年薪:48003.96 |
| 姓名:张野年薪:120001.56 |
| 姓名:程咬金年薪:240000.00 |
| 姓名:程咬银年薪:228000.00 |
| 姓名:程咬铜年薪:216000.00 |
| 姓名:程咬铁年薪:204000.00 |
+-----------------------------------+
18 rows in set (0.00 sec) mysql> select concat ws(':',name,age,salary) from employee;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(':',name,age,salary) from employee' at line 1
mysql> select concat ws(':',name,age,salary) from employee;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(':',name,age,salary) from employee' at line 1
mysql> select concat ws(':',name,age) from employee;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(':',name,age) from employee' at line 1
mysql> select concat_ws(':',name,age) from employee;
+-------------------------+
| concat_ws(':',name,age) |
+-------------------------+
| egon:18 |
| alex:78 |
| wupeiqi:81 |
| yuanhao:73 |
| liwenzhou:28 |
| jingliyang:18 |
| jinxin:18 |
| 成龙:48 |
| 歪歪:48 |
| 丫丫:38 |
| 丁丁:18 |
| 星星:18 |
| 格格:28 |
| 张野:28 |
| 程咬金:18 |
| 程咬银:18 |
| 程咬铜:18 |
| 程咬铁:18 |
+-------------------------+
18 rows in set (0.00 sec) mysql> select concat('<薪资:',salary,'>') from employee;
+-------------------------------+
| concat('<薪资:',salary,'>') |
+-------------------------------+
| <薪资:7300.33> |
| <薪资:1000000.31> |
| <薪资:8300.00> |
| <薪资:3500.00> |
| <薪资:2100.00> |
| <薪资:9000.00> |
| <薪资:30000.00> |
| <薪资:10000.00> |
| <薪资:3000.13> |
| <薪资:2000.35> |
| <薪资:1000.37> |
| <薪资:3000.29> |
| <薪资:4000.33> |
| <薪资:10000.13> |
| <薪资:20000.00> |
| <薪资:19000.00> |
| <薪资:18000.00> |
| <薪资:17000.00> |
+-------------------------------+
18 rows in set (0.00 sec) mysql> select concat('<薪资:',salary,'>'),concat('<姓名:',name,'>') from employee;
+-------------------------------+-----------------------------+
| concat('<薪资:',salary,'>') | concat('<姓名:',name,'>') |
+-------------------------------+-----------------------------+
| <薪资:7300.33> | <姓名:egon> |
| <薪资:1000000.31> | <姓名:alex> |
| <薪资:8300.00> | <姓名:wupeiqi> |
| <薪资:3500.00> | <姓名:yuanhao> |
| <薪资:2100.00> | <姓名:liwenzhou> |
| <薪资:9000.00> | <姓名:jingliyang> |
| <薪资:30000.00> | <姓名:jinxin> |
| <薪资:10000.00> | <姓名:成龙> |
| <薪资:3000.13> | <姓名:歪歪> |
| <薪资:2000.35> | <姓名:丫丫> |
| <薪资:1000.37> | <姓名:丁丁> |
| <薪资:3000.29> | <姓名:星星> |
| <薪资:4000.33> | <姓名:格格> |
| <薪资:10000.13> | <姓名:张野> |
| <薪资:20000.00> | <姓名:程咬金> |
| <薪资:19000.00> | <姓名:程咬银> |
| <薪资:18000.00> | <姓名:程咬铜> |
| <薪资:17000.00> | <姓名:程咬铁> |
+-------------------------------+-----------------------------+
18 rows in set (0.00 sec) mysql> desc employee;
+--------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| sex | enum('male','female') | NO | | male | |
| age | int(3) unsigned | NO | | 28 | |
| hire_date | date | NO | | NULL | |
| post | varchar(50) | YES | | NULL | |
| post_comment | varchar(100) | YES | | NULL | |
| salary | double(15,2) | YES | | NULL | |
| office | int(11) | YES | | NULL | |
| depart_id | int(11) | YES | | NULL | |
+--------------+-----------------------+------+-----+---------+----------------+
10 rows in set (0.00 sec) mysql> select distinct post from employee;
+-----------------------------------------+
| post |
+-----------------------------------------+
| 老男孩驻沙河办事处外交大使 |
| teacher |
| sale |
| operation |
+-----------------------------------------+
4 rows in set (0.00 sec) mysql> select name,salary*12 as annual_year from employee;
+------------+-------------+
| name | annual_year |
+------------+-------------+
| egon | 87603.96 |
| alex | 12000003.72 |
| wupeiqi | 99600.00 |
| yuanhao | 42000.00 |
| liwenzhou | 25200.00 |
| jingliyang | 108000.00 |
| jinxin | 360000.00 |
| 成龙 | 120000.00 |
| 歪歪 | 36001.56 |
| 丫丫 | 24004.20 |
| 丁丁 | 12004.44 |
| 星星 | 36003.48 |
| 格格 | 48003.96 |
| 张野 | 120001.56 |
| 程咬金 | 240000.00 |
| 程咬银 | 228000.00 |
| 程咬铜 | 216000.00 |
| 程咬铁 | 204000.00 |
+------------+-------------+
18 rows in set (0.00 sec) mysql>
26-where约束
1、where条件的查询逻辑;
- 比较运算符:>、<、>=、<=、<>、!=
- between 80 and 100,值在10~20之间;
- in (80,91,20)值是80、91或者20
- like 'cuixiaozha%',%代表任意多个字符,_表示一个字符;
- 逻辑运算符,在多个条件中可以直接使用逻辑运算符and or not
2、常见的查询场景;
- 单条件查询;
- 多条件组合查询;
- 关键字BETWEEN AND;
- 关键字 IS NULL (判断某个字段是否为NULL不能用等号,要使用IS),其中,''是空字符串而不是NULL
- 关键字IN集合查询;
- 关键字LIKE模糊查询(通配符%,通配符_);
3、操作日志;
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.7.20-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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 |
| engines |
| luffy_practise |
| luffycity |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
9 rows in set (0.01 sec) mysql> use luffy_practise;
Database changed
mysql> show tables;
+--------------------------+
| Tables_in_luffy_practise |
+--------------------------+
| employee |
+--------------------------+
1 row in set (0.00 sec) mysql> select * from employee;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 |
| 5 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 |
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
| 8 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
| 11 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 |
| 16 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 |
| 17 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 |
| 18 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
18 rows in set (0.01 sec) mysql> select * from employee where id >7;
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| 8 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
| 11 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 |
| 16 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 |
| 17 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 |
| 18 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
11 rows in set (0.00 sec) mysql> select id,name from employee where id >7;
+----+-----------+
| id | name |
+----+-----------+
| 8 | 成龙 |
| 9 | 歪歪 |
| 10 | 丫丫 |
| 11 | 丁丁 |
| 12 | 星星 |
| 13 | 格格 |
| 14 | 张野 |
| 15 | 程咬金 |
| 16 | 程咬银 |
| 17 | 程咬铜 |
| 18 | 程咬铁 |
+----+-----------+
11 rows in set (0.00 sec) mysql> select name,salary from employee where post = 'teacher' and salary > 8000;
+------------+------------+
| name | salary |
+------------+------------+
| alex | 1000000.31 |
| wupeiqi | 8300.00 |
| jingliyang | 9000.00 |
| jinxin | 30000.00 |
| 成龙 | 10000.00 |
+------------+------------+
5 rows in set (0.00 sec) mysql> select * from employee;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 |
| 5 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 |
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
| 8 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
| 11 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 |
| 16 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 |
| 17 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 |
| 18 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec) mysql> select name,salary from employee where salary >=20000 and salary <= 30000;
+-----------+----------+
| name | salary |
+-----------+----------+
| jinxin | 30000.00 |
| 程咬金 | 20000.00 |
+-----------+----------+
2 rows in set (0.00 sec) mysql> select name,salary from employee where salary between 20000 and 30000;
+-----------+----------+
| name | salary |
+-----------+----------+
| jinxin | 30000.00 |
| 程咬金 | 20000.00 |
+-----------+----------+
2 rows in set (0.00 sec) mysql> select name,salary from employee where salary <20000 or salary > 30000;
+------------+------------+
| name | salary |
+------------+------------+
| egon | 7300.33 |
| alex | 1000000.31 |
| wupeiqi | 8300.00 |
| yuanhao | 3500.00 |
| liwenzhou | 2100.00 |
| jingliyang | 9000.00 |
| 成龙 | 10000.00 |
| 歪歪 | 3000.13 |
| 丫丫 | 2000.35 |
| 丁丁 | 1000.37 |
| 星星 | 3000.29 |
| 格格 | 4000.33 |
| 张野 | 10000.13 |
| 程咬银 | 19000.00 |
| 程咬铜 | 18000.00 |
| 程咬铁 | 17000.00 |
+------------+------------+
16 rows in set (0.00 sec) mysql> select name,salary from employee where salary not between 20000 and 30000;
+------------+------------+
| name | salary |
+------------+------------+
| egon | 7300.33 |
| alex | 1000000.31 |
| wupeiqi | 8300.00 |
| yuanhao | 3500.00 |
| liwenzhou | 2100.00 |
| jingliyang | 9000.00 |
| 成龙 | 10000.00 |
| 歪歪 | 3000.13 |
| 丫丫 | 2000.35 |
| 丁丁 | 1000.37 |
| 星星 | 3000.29 |
| 格格 | 4000.33 |
| 张野 | 10000.13 |
| 程咬银 | 19000.00 |
| 程咬铜 | 18000.00 |
| 程咬铁 | 17000.00 |
+------------+------------+
16 rows in set (0.00 sec) mysql> select name,age from employee where age = 73 or age =81 or age =28;
+-----------+-----+
| name | age |
+-----------+-----+
| wupeiqi | 81 |
| yuanhao | 73 |
| liwenzhou | 28 |
| 格格 | 28 |
| 张野 | 28 |
+-----------+-----+
5 rows in set (0.00 sec) mysql> select * from employee where age in (73,81,28);
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 |
| 5 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
5 rows in set (0.00 sec) mysql> select * from employee where post = '';
Empty set (0.00 sec) mysql> select * from employee where post_comment is null;
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 |
| 5 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 |
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
| 8 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
| 11 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 |
| 16 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 |
| 17 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 |
| 18 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 |
+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec) mysql> select * from employee where post_comment is not null;
Empty set (0.00 sec) mysql> select * from employee where name like 'jin%';
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
+----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+
2 rows in set (0.00 sec) mysql> select * from employee where name like 'jin_';
Empty set (0.00 sec) mysql> select * from employee where name like 'jin___';
+----+--------+------+-----+------------+---------+--------------+----------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+--------+------+-----+------------+---------+--------------+----------+--------+-----------+
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
+----+--------+------+-----+------------+---------+--------------+----------+--------+-----------+
1 row in set (0.00 sec) mysql>
27-group by分组
1、什么是分组,我们为什么要分组?
1)分组发生在where条件之后;
2)将所有记录按照某个相同的字段进行归类;
3)小窍门:在“每”这个字后面的字段,就是我们分组的依据;
4)可以按照任意字段分组,但是分组完毕后,比如group by post,只能看到post字段,如果查看组内信息,需要借助于聚合函数;
2、ONLY_FULL_GROUP_BY
查看MySQL5.7默认的sql_mode;
SELECT @@global.sql_mode; SET GLOBAL sql_mode = 'ONLY_FULL_GROUP_BY';
3、GROUP BY
- 单独使用GROUP BY 关键字分组;
- GROUP BY 关键字和GROUP_CONCAT()函数一起使用;
- GROUP BY 与聚合函数一起使用MAX MIN COUNT SUM AVG;
4、聚合函数;
SELECT COUNT(*) FROM employee;
SELECT COUNT(*) FROM employee WHERE depart_id=1;
SELECT MAX(salary) FROM employee;
SELECT MIN(salary) FROM employee;
SELECT AVG(salary) FROM employee;
SELECT SUM(salary) FROM employee;
SELECT SUM(salary) FROM employee WHERE depart_id=3;
5、操作日志:
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 5.7.20-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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 |
| engines |
| luffy_practise |
| luffycity |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
9 rows in set (0.00 sec) mysql> use luffy_practise;
Database changed
mysql> show tables;
+--------------------------+
| Tables_in_luffy_practise |
+--------------------------+
| employee |
+--------------------------+
1 row in set (0.00 sec) mysql> use employee;
ERROR 1049 (42000): Unknown database 'employee'
mysql> desc employee;
+--------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| sex | enum('male','female') | NO | | male | |
| age | int(3) unsigned | NO | | 28 | |
| hire_date | date | NO | | NULL | |
| post | varchar(50) | YES | | NULL | |
| post_comment | varchar(100) | YES | | NULL | |
| salary | double(15,2) | YES | | NULL | |
| office | int(11) | YES | | NULL | |
| depart_id | int(11) | YES | | NULL | |
+--------------+-----------------------+------+-----+---------+----------------+
10 rows in set (0.01 sec) mysql> select * from employee group by post;
+----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
+----+--------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
4 rows in set (0.00 sec) mysql> set global sql_mode = 'ONLY_FULL_GROUP_BY';
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> select post from employee group by post;
+-----------------------------------------+
| post |
+-----------------------------------------+
| operation |
| sale |
| teacher |
| 老男孩驻沙河办事处外交大使 |
+-----------------------------------------+
4 rows in set (0.00 sec) mysql> select post,count(id) from employee group by post;
+-----------------------------------------+-----------+
| post | count(id) |
+-----------------------------------------+-----------+
| operation | 5 |
| sale | 5 |
| teacher | 7 |
| 老男孩驻沙河办事处外交大使 | 1 |
+-----------------------------------------+-----------+
4 rows in set (0.00 sec) mysql> select post,count(id) as employee_id from employee group by post;
+-----------------------------------------+-------------+
| post | employee_id |
+-----------------------------------------+-------------+
| operation | 5 |
| sale | 5 |
| teacher | 7 |
| 老男孩驻沙河办事处外交大使 | 1 |
+-----------------------------------------+-------------+
4 rows in set (0.00 sec) mysql> select post,max(salary) as employee_id from employee group by post;
+-----------------------------------------+-------------+
| post | employee_id |
+-----------------------------------------+-------------+
| operation | 20000.00 |
| sale | 4000.33 |
| teacher | 1000000.31 |
| 老男孩驻沙河办事处外交大使 | 7300.33 |
+-----------------------------------------+-------------+
4 rows in set (0.00 sec) mysql> select post,min(salary) as employee_id from employee group by post;
+-----------------------------------------+-------------+
| post | employee_id |
+-----------------------------------------+-------------+
| operation | 10000.13 |
| sale | 1000.37 |
| teacher | 2100.00 |
| 老男孩驻沙河办事处外交大使 | 7300.33 |
+-----------------------------------------+-------------+
4 rows in set (0.00 sec) mysql> select post,avg(salary) as employee_id from employee group by post;
+-----------------------------------------+---------------+
| post | employee_id |
+-----------------------------------------+---------------+
| operation | 16800.026000 |
| sale | 2600.294000 |
| teacher | 151842.901429 |
| 老男孩驻沙河办事处外交大使 | 7300.330000 |
+-----------------------------------------+---------------+
4 rows in set (0.00 sec) mysql> select post,count(salary) as employee_id from employee group by post;
+-----------------------------------------+-------------+
| post | employee_id |
+-----------------------------------------+-------------+
| operation | 5 |
| sale | 5 |
| teacher | 7 |
| 老男孩驻沙河办事处外交大使 | 1 |
+-----------------------------------------+-------------+
4 rows in set (0.00 sec) mysql> select post,sum(salary) as employee_id from employee group by post;
+-----------------------------------------+-------------+
| post | employee_id |
+-----------------------------------------+-------------+
| operation | 84000.13 |
| sale | 13001.47 |
| teacher | 1062900.31 |
| 老男孩驻沙河办事处外交大使 | 7300.33 |
+-----------------------------------------+-------------+
4 rows in set (0.00 sec) mysql> select max(salary) from employee;
+-------------+
| max(salary) |
+-------------+
| 1000000.31 |
+-------------+
1 row in set (0.00 sec) mysql> select name,max(salary) from employee;
+------+-------------+
| name | max(salary) |
+------+-------------+
| egon | 1000000.31 |
+------+-------------+
1 row in set (0.00 sec) mysql> select name,salary from employee;
+------------+------------+
| name | salary |
+------------+------------+
| egon | 7300.33 |
| alex | 1000000.31 |
| wupeiqi | 8300.00 |
| yuanhao | 3500.00 |
| liwenzhou | 2100.00 |
| jingliyang | 9000.00 |
| jinxin | 30000.00 |
| 成龙 | 10000.00 |
| 歪歪 | 3000.13 |
| 丫丫 | 2000.35 |
| 丁丁 | 1000.37 |
| 星星 | 3000.29 |
| 格格 | 4000.33 |
| 张野 | 10000.13 |
| 程咬金 | 20000.00 |
| 程咬银 | 19000.00 |
| 程咬铜 | 18000.00 |
| 程咬铁 | 17000.00 |
+------------+------------+
18 rows in set (0.00 sec) mysql> select name,max(salary) from employee;
+------+-------------+
| name | max(salary) |
+------+-------------+
| egon | 1000000.31 |
+------+-------------+
1 row in set (0.00 sec) mysql> select post,group_concat(name) from employee group by post;
+-----------------------------------------+---------------------------------------------------------+
| post | group_concat(name) |
+-----------------------------------------+---------------------------------------------------------+
| operation | 张野,程咬金,程咬银,程咬铜,程咬铁 |
| sale | 歪歪,丫丫,丁丁,星星,格格 |
| teacher | alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龙 |
| 老男孩驻沙河办事处外交大使 | egon |
+-----------------------------------------+---------------------------------------------------------+
4 rows in set (0.00 sec) mysql> select post,group_concat(name) from employee group by post;
+-----------------------------------------+---------------------------------------------------------+
| post | group_concat(name) |
+-----------------------------------------+---------------------------------------------------------+
| operation | 张野,程咬金,程咬银,程咬铜,程咬铁 |
| sale | 歪歪,丫丫,丁丁,星星,格格 |
| teacher | alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龙 |
| 老男孩驻沙河办事处外交大使 | egon |
+-----------------------------------------+---------------------------------------------------------+
4 rows in set (0.00 sec) mysql> select post,count(id) from employee where age > 50 group by post;
+---------+-----------+
| post | count(id) |
+---------+-----------+
| teacher | 3 |
+---------+-----------+
1 row in set (0.00 sec) mysql> select sex,count(id) from employee group by sex;
+--------+-----------+
| sex | count(id) |
+--------+-----------+
| male | 10 |
| female | 8 |
+--------+-----------+
2 rows in set (0.00 sec) mysql> select sex,avg(salary) from employee group by sex;
+--------+---------------+
| sex | avg(salary) |
+--------+---------------+
| male | 110920.077000 |
| female | 7250.183750 |
+--------+---------------+
2 rows in set (0.00 sec) mysql>
28-having过滤
1、查询顺序;
SELECT DISTINCT 字段1,字段2,字段n... from 库名.表名
WHERE 过滤条件
GROUP BY 分组过滤条件
HAVING 过滤
ORDER BY 排序字段
LIMIT n(限制的条数);
HAVING与WHERE不同之处;
#1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数;
#2. Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数;
2、操作日志;
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 5.7.20-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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 |
| engines |
| luffy_practise |
| luffycity |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
9 rows in set (0.00 sec) mysql> use luffy_practise;
Database changed
mysql> show tables;
+--------------------------+
| Tables_in_luffy_practise |
+--------------------------+
| employee |
+--------------------------+
1 row in set (0.00 sec) mysql> desc employee;
+--------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| sex | enum('male','female') | NO | | male | |
| age | int(3) unsigned | NO | | 28 | |
| hire_date | date | NO | | NULL | |
| post | varchar(50) | YES | | NULL | |
| post_comment | varchar(100) | YES | | NULL | |
| salary | double(15,2) | YES | | NULL | |
| office | int(11) | YES | | NULL | |
| depart_id | int(11) | YES | | NULL | |
+--------------+-----------------------+------+-----+---------+----------------+
10 rows in set (0.00 sec) mysql> select post,group_concat(name),count(id) from employee grop by post;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'by post' at line 1
mysql> select post,group_concat(name),count(id) from employee group by post;
+-----------------------------------------+---------------------------------------------------------+-----------+
| post | group_concat(name) | count(id) |
+-----------------------------------------+---------------------------------------------------------+-----------+
| operation | 张野,程咬金,程咬银,程咬铜,程咬铁 | 5 |
| sale | 歪歪,丫丫,丁丁,星星,格格 | 5 |
| teacher | alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龙 | 7 |
| 老男孩驻沙河办事处外交大使 | egon | 1 |
+-----------------------------------------+---------------------------------------------------------+-----------+
4 rows in set (0.00 sec) mysql> select post,group_concat(name),count(id) from employee group by post having count(id);
+-----------------------------------------+---------------------------------------------------------+-----------+
| post | group_concat(name) | count(id) |
+-----------------------------------------+---------------------------------------------------------+-----------+
| operation | 张野,程咬金,程咬银,程咬铜,程咬铁 | 5 |
| sale | 歪歪,丫丫,丁丁,星星,格格 | 5 |
| teacher | alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龙 | 7 |
| 老男孩驻沙河办事处外交大使 | egon | 1 |
+-----------------------------------------+---------------------------------------------------------+-----------+
4 rows in set (0.00 sec) mysql> select post,avg(salary) from employee group by post having avg(salary) > 10000;
+-----------+---------------+
| post | avg(salary) |
+-----------+---------------+
| operation | 16800.026000 |
| teacher | 151842.901429 |
+-----------+---------------+
2 rows in set (0.00 sec) mysql> select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary)< 20000;
+-----------+--------------+
| post | avg(salary) |
+-----------+--------------+
| operation | 16800.026000 |
+-----------+--------------+
1 row in set (0.00 sec) mysql> select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary)< 200000;
+-----------+---------------+
| post | avg(salary) |
+-----------+---------------+
| operation | 16800.026000 |
| teacher | 151842.901429 |
+-----------+---------------+
2 rows in set (0.00 sec) mysql>
29-order by排序
1、不指定排序规则,默认按照id从小到大排序;
语法正确,但有可能从执行顺序的角度来看,不符合逻辑;
2、操作日志;
mysql> select max(salary) from employee where max(salary);
ERROR 1111 (HY000): Invalid use of group function
mysql> select * from employee where salary >1000 group by post having count(id ) > 5;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'luffy_practise.employee.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
mysql> set global sql_mode = only_full_group_by;
Query OK, 0 rows affected (0.00 sec) mysql> select * from employee where salary >1000 group by post having count(id ) > 5;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'luffy_practise.employee.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
mysql> set global sql_mode = only_full_group_by;
Query OK, 0 rows affected (0.00 sec) mysql> select post,count(id) from employee where salary >1000 group by post having count(id ) > 5;
+---------+-----------+
| post | count(id) |
+---------+-----------+
| teacher | 7 |
+---------+-----------+
1 row in set (0.00 sec) mysql> select distinct post,count(id) ad emp_count from employee where salary >1000 group by post having count(id) >1 order by emp_count desc;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'emp_count from employee where salary >1000 group by post having count(id) >1 ord' at line 1
mysql> select distinct post,count(id) as emp_count from employee where salary >1000 group by post having count(id) >1 order by emp_count desc;
+-----------+-----------+
| post | emp_count |
+-----------+-----------+
| teacher | 7 |
| sale | 5 |
| operation | 5 |
+-----------+-----------+
3 rows in set (0.00 sec) mysql> select distinct post,count(id) as emp_count from employee where salary >1000 group by post having count(id) >1 order by emp_count asc;
+-----------+-----------+
| post | emp_count |
+-----------+-----------+
| sale | 5 |
| operation | 5 |
| teacher | 7 |
+-----------+-----------+
3 rows in set (0.00 sec)
30-limit限制条数
1、limit关键字限制条数(limit 5,4);
2、操作日志;
mysql> select * from employee limit 3;
+----+---------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+---------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
+----+---------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
3 rows in set (0.00 sec) mysql> select * from employee order by salary desc limit 1;
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
+----+------+------+-----+------------+---------+--------------+------------+--------+-----------+
1 row in set (0.00 sec) mysql> select * from employee limit 0,5;
+----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
| 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 |
| 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 |
| 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 |
| 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 |
| 5 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 |
+----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
5 rows in set (0.00 sec) mysql> select * from employee limit 5,10;
+----+------------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+------------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 |
| 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 |
| 8 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 |
| 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 |
| 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 |
| 11 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 |
+----+------------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
10 rows in set (0.00 sec) mysql> select * from employee limit 10,20;
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| 11 | 丁丁 | female | 18 | 2011-03-12 | sale | NULL | 1000.37 | 402 | 2 |
| 12 | 星星 | female | 18 | 2016-05-13 | sale | NULL | 3000.29 | 402 | 2 |
| 13 | 格格 | female | 28 | 2017-01-27 | sale | NULL | 4000.33 | 402 | 2 |
| 14 | 张野 | male | 28 | 2016-03-11 | operation | NULL | 10000.13 | 403 | 3 |
| 15 | 程咬金 | male | 18 | 1997-03-12 | operation | NULL | 20000.00 | 403 | 3 |
| 16 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 |
| 17 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 |
| 18 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
8 rows in set (0.00 sec) mysql> select * from employee limit 15,5;
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| id | name | sex | age | hire_date | post | post_comment | salary | office | depart_id |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
| 16 | 程咬银 | female | 18 | 2013-03-11 | operation | NULL | 19000.00 | 403 | 3 |
| 17 | 程咬铜 | male | 18 | 2015-04-11 | operation | NULL | 18000.00 | 403 | 3 |
| 18 | 程咬铁 | female | 18 | 2014-05-12 | operation | NULL | 17000.00 | 403 | 3 |
+----+-----------+--------+-----+------------+-----------+--------------+----------+--------+-----------+
3 rows in set (0.00 sec) mysql>
31-单表查询的语法顺序与执行顺序总结
1、单表查询总结;
1)语法顺序;
2)执行顺序
def from(db,table):
f = open(r'%s\%s'%(db,table))
return f def where(condition,f):
for line in f:
if condition:
yield line def group(lines):
pass def having(group_res):
pass def distinct(having_res):
pass def order(distinct_res):
pass def limit(order_res):
pass def select():
f = from('db1','t1')
lines = where('id>3',f)
group_res = group(lines)
having_res = having(group_res)
distinct(having_res)
order_res = order(distinct_res)
res = limit(order_res)
print(res)
return res
使用以上该方式,讲解select的语法顺序及执行顺序;
32-正则查询
1、正则表达式查询;
- regexp
SELECT * FROM employee WHERE NAME LIKE 'jin%';#like关键字模糊匹配;
SELECT * FROM employee WHERE NAME regexp '^jin';#引入正则;
SELECT * FROM employee WHERE NAME regexp '^jin.*(g|n)%';#引入正则;
33-连表操作
1、连表的分类;
- 内连接
- 左连接
- 右连接
- 全外连接
#内连接:只取两张表的共同部分
select * from employee inner join department on employee.dep_id = department.id
#左连接:在内连接的基础上,保留左表的记录;
select * from employee left join department on employee.dep_id = deparment.id;
#右连接:在内连接的基础上,保留右表的记录;
select * from employee right join department on employee.dep_id = department.id;
#全外连接:在内连接的基础上,保留左右两张表没有对应关系的记录;
select * from employee full join(其他数据库库可能支持,MySQL不支持) select * from employee left join department on employee.dep_id = deparment.id union select * from employee right join department on employee.dep_id = department.id;
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 41
Server version: 5.7.20-log MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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 |
| engines |
| luffy_practise |
| luffycity |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
9 rows in set (0.00 sec) mysql> use luffy_practise;
Database changed
mysql> show tables;
+--------------------------+
| Tables_in_luffy_practise |
+--------------------------+
| employee |
+--------------------------+
1 row in set (0.00 sec) mysql> create table department(
-> id int
-> ^C
mysql> create table department(
-> id int,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.02 sec) mysql> alter table employee rename to employee_history;
Query OK, 0 rows affected (0.01 sec) mysql> create table employee(
-> id int primary key auto_increment,
-> name varchar(20),
-> sex enum('male','female')not null default 'male',
-> age int,
-> dep_id int
-> );
Query OK, 0 rows affected (0.02 sec) mysql> insert into department values
-> (200,'技术'),
-> (201,'人力资源'),
-> (202,'销售'),
-> (203,'运营');
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0 mysql> insert into employee(name,sex,age,dep_id) values
-> ('egon','male',18,200),
-> ('alex','female',48,201),
-> ('wupeiqi','male',38,201),
-> ('yuanhao','female',28,202),
-> ('liwenzhou','male',18,200),
-> ('jingliyang','female',18,204)
-> ;
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0 mysql> desc department;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> desc employee;
+--------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| sex | enum('male','female') | NO | | male | |
| age | int(11) | YES | | NULL | |
| dep_id | int(11) | YES | | NULL | |
+--------+-----------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec) mysql> select * from department;
+------+--------------+
| id | name |
+------+--------------+
| 200 | 技术 |
| 201 | 人力资源 |
| 202 | 销售 |
| 203 | 运营 |
+------+--------------+
4 rows in set (0.00 sec) mysql> select * from employee;
+----+------------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+------------+--------+------+--------+
| 1 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 201 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | liwenzhou | male | 18 | 200 |
| 6 | jingliyang | female | 18 | 204 |
+----+------------+--------+------+--------+
6 rows in set (0.00 sec) mysql> select * from employee,department;
+----+------------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+----+------------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 1 | egon | male | 18 | 200 | 201 | 人力资源 |
| 1 | egon | male | 18 | 200 | 202 | 销售 |
| 1 | egon | male | 18 | 200 | 203 | 运营 |
| 2 | alex | female | 48 | 201 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 2 | alex | female | 48 | 201 | 202 | 销售 |
| 2 | alex | female | 48 | 201 | 203 | 运营 |
| 3 | wupeiqi | male | 38 | 201 | 200 | 技术 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力资源 |
| 3 | wupeiqi | male | 38 | 201 | 202 | 销售 |
| 3 | wupeiqi | male | 38 | 201 | 203 | 运营 |
| 4 | yuanhao | female | 28 | 202 | 200 | 技术 |
| 4 | yuanhao | female | 28 | 202 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 4 | yuanhao | female | 28 | 202 | 203 | 运营 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技术 |
| 5 | liwenzhou | male | 18 | 200 | 201 | 人力资源 |
| 5 | liwenzhou | male | 18 | 200 | 202 | 销售 |
| 5 | liwenzhou | male | 18 | 200 | 203 | 运营 |
| 6 | jingliyang | female | 18 | 204 | 200 | 技术 |
| 6 | jingliyang | female | 18 | 204 | 201 | 人力资源 |
| 6 | jingliyang | female | 18 | 204 | 202 | 销售 |
| 6 | jingliyang | female | 18 | 204 | 203 | 运营 |
+----+------------+--------+------+--------+------+--------------+
24 rows in set (0.00 sec) mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee inner join department on employee.dep_id = department.id;
+----+-----------+------+--------+--------------+
| id | name | age | sex | name |
+----+-----------+------+--------+--------------+
| 1 | egon | 18 | male | 技术 |
| 2 | alex | 48 | female | 人力资源 |
| 3 | wupeiqi | 38 | male | 人力资源 |
| 4 | yuanhao | 28 | female | 销售 |
| 5 | liwenzhou | 18 | male | 技术 |
+----+-----------+------+--------+--------------+
5 rows in set (0.00 sec) mysql> select employee.id,employee.name,department.name as depart_name from employee left join department on employee.dep_id = department.id;
+----+------------+--------------+
| id | name | depart_name |
+----+------------+--------------+
| 1 | egon | 技术 |
| 5 | liwenzhou | 技术 |
| 2 | alex | 人力资源 |
| 3 | wupeiqi | 人力资源 |
| 4 | yuanhao | 销售 |
| 6 | jingliyang | NULL |
+----+------------+--------------+
6 rows in set (0.00 sec) mysql> select employee.id,employee.name,department.name as depart_name from employee right join department on employee.dep_id = department.id;
+------+-----------+--------------+
| id | name | depart_name |
+------+-----------+--------------+
| 1 | egon | 技术 |
| 2 | alex | 人力资源 |
| 3 | wupeiqi | 人力资源 |
| 4 | yuanhao | 销售 |
| 5 | liwenzhou | 技术 |
| NULL | NULL | 运营 |
+------+-----------+--------------+
6 rows in set (0.00 sec) mysql> select * from employee left join department on employee.dep_id = department.id
-> union
-> select * from employee right join department on employee.dep_id = department.id
-> ;
+------+------------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+------+------------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 6 | jingliyang | female | 18 | 204 | NULL | NULL |
| NULL | NULL | NULL | NULL | NULL | 203 | 运营 |
+------+------------+--------+------+--------+------+--------------+
7 rows in set (0.00 sec) mysql>
34-select语句关键字执行优先级
1、完整的语法;
2、执行顺序;
35-子查询
1、练习1;
select name from department where id in (select dep_id from employee group by dep_id having avg(age) > 25);
2、练习2;
select * from employee where dep_id = (select id from department where name = '技术部');
3、练习3;
select name from department where id not in (select distinct dep_id from employee);
4、练习4;
select * from employee where exists (select id from department where name = '技术部');
5、练习5;
select * from employee as t1 inner join (select post,max(hire_date) as max_hire_date from employee group by post) as t2 on t1.post = t2.post
where t1.hire_date = t2.max_hire_date;
36-多表查询练习
1、基础数据准备;
/*
数据导入:
Navicat Premium Data Transfer Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50624
Source Host : localhost
Source Database : sqlexam Target Server Type : MySQL
Target Server Version : 50624
File Encoding : utf-8 Date: 10/21/2016 06:46:46 AM
*/ SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0; -- ----------------------------
-- Table structure for `class`
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`caption` varchar(32) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `class`
-- ----------------------------
BEGIN;
INSERT INTO `class` VALUES ('', '三年二班'), ('', '三年三班'), ('', '一年二班'), ('', '二年九班');
COMMIT; -- ----------------------------
-- Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`cname` varchar(32) NOT NULL,
`teacher_id` int(11) NOT NULL,
PRIMARY KEY (`cid`),
KEY `fk_course_teacher` (`teacher_id`),
CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `course`
-- ----------------------------
BEGIN;
INSERT INTO `course` VALUES ('', '生物', ''), ('', '物理', ''), ('', '体育', ''), ('', '美术', '');
COMMIT; -- ----------------------------
-- Table structure for `score`
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`num` int(11) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_score_student` (`student_id`),
KEY `fk_score_course` (`course_id`),
CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `score`
-- ----------------------------
BEGIN;
INSERT INTO `score` VALUES ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', '');
COMMIT; -- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`gender` char(1) NOT NULL,
`class_id` int(11) NOT NULL,
`sname` varchar(32) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_class` (`class_id`),
CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `student`
-- ----------------------------
BEGIN;
INSERT INTO `student` VALUES ('', '男', '', '理解'), ('', '女', '', '钢蛋'), ('', '男', '', '张三'), ('', '男', '', '张一'), ('', '女', '', '张二'), ('', '男', '', '张四'), ('', '女', '', '铁锤'), ('', '男', '', '李三'), ('', '男', '', '李一'), ('', '女', '', '李二'), ('', '男', '', '李四'), ('', '女', '', '如花'), ('', '男', '', '刘三'), ('', '男', '', '刘一'), ('', '女', '', '刘二'), ('', '男', '', '刘四');
COMMIT; -- ----------------------------
-- Table structure for `teacher`
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`tid` int(11) NOT NULL AUTO_INCREMENT,
`tname` varchar(32) NOT NULL,
PRIMARY KEY (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `teacher`
-- ----------------------------
BEGIN;
INSERT INTO `teacher` VALUES ('', '张磊老师'), ('', '李平老师'), ('', '刘海燕老师'), ('', '朱云海老师'), ('', '李杰老师');
COMMIT; SET FOREIGN_KEY_CHECKS = 1;
2、查询练习;
1、查询所有的课程的名称以及对应的任课老师姓名
2、查询学生表中男女生各有多少人
3、查询物理成绩等于100的学生的姓名
4、查询平均成绩大于八十分的同学的姓名和平均成绩
5、查询所有学生的学号,姓名,选课数,总成绩
6、 查询姓李老师的个数
7、 查询没有报李平老师课的学生姓名
8、 查询物理课程比生物课程高的学生的学号
9、 查询没有同时选修物理课程和体育课程的学生姓名
10、查询挂科超过两门(包括两门)的学生姓名和班级
、查询选修了所有课程的学生姓名
12、查询李平老师教的课程的所有成绩记录
13、查询全部学生都选修了的课程号和课程名
14、查询每门课程被选修的次数
15、查询之选修了一门课程的学生姓名和学号
16、查询所有学生考出的成绩并按从高到低排序(成绩去重)
17、查询平均成绩大于85的学生姓名和平均成绩
18、查询生物成绩不及格的学生姓名和对应生物分数
19、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名
20、查询每门课程成绩最好的前两名学生姓名
21、查询不同课程但成绩相同的学号,课程号,成绩
22、查询没学过“叶平”老师课程的学生姓名以及选修的课程名称;
23、查询所有选修了学号为1的同学选修过的一门或者多门课程的同学学号和姓名;
24、任课最多的老师中学生单科成绩最高的学生姓名
37-权限管理
1、创建账号;
1)本地账号;
create user 'cuixiaozhao'@'localhost' identified by 'Ab123456..';
2)远程账号;
create user 'lijingping'@'47.95.211.12' identified by 'Ab123456..';
create user 'lijingping'@'47.95.211.%' identified by 'Ab123456..';
create user 'lijingping'@'%' identified by 'Ab123456..';
2、授权;
user;权限代表*.*
db:权限代表db1.*
tables_priv;权限代表:db1.tb1
columns_priv;权限代表:id,name,sex
1)、所有的库名表名;
grant all on *.* to 'cuixiaozhao'@'localhost';
grant select on *.* to 'cuixiaozhao'@'localhost'; revoke select *.* from 'egon1'@'localhost'; 2)、具体库名&表名;
grant select luffycity.* to 'cuixiaozhao'@'localhost';
grant select luffycity.employee to 'cuixiaozhao'@'localhost';
revoke select luffycity.* from 'cuixiaozhao'@'localhost'; grant select employee.* to 'cuixiaozhao'@'localhost';
revoke select employee.* from 'cuixiaozhao'@'localhost'; 4)、字段级别; grant select(id,name),update(age) on luffycity.employee to 'cuixiaozhao'@'localhost';
38-Navicat工具的使用
1、
图形化Navicat For MySQL工具分享(含注册机);
Navicat For MySQL链接:https://pan.baidu.com/s/1lrK5qImWZqpTLrC2fz3Akw 密码:u993
Navicat For Premium 链接:https://pan.baidu.com/s/18i3zNavUDA_ezmxRey9I7g 密码:h67c
2、图形化Navicat For MySQL工具的使用;
Navicat For MySQL的PDF使用教程链接:https://pan.baidu.com/s/1qJJ1-surKE28XQwEgWoG_Q 密码:u6oc
3、掌握的技能;
- 新建连接
- 新建数据库
- 使用数据库
- 新建表
- 设计表、新增(修改)字段及约束
- 新建查询
- 备份数据库、数据表
- 批量添加注释Ctrl + ?键,取反:Ctrl + Shift + ?键
39-pymysql模块之基本使用
1、pymysql初识;
那如何在python程序中操作数据库呢?这就用到了pymysql模块,该模块本质就是一个套接字客户端软件,使用前需要事先安装。
pip3 install pymysql
2、链接、执行SQL、关闭(游标);
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# __Author__:TQTL911
# Version:python3.6.6
# Time:7/27/2018 6:06 PM
import pymysql usr = input('Username:').strip()
passwd = input('Password:').strip() #建立链接对象;
conn = pymysql.connect(
host = '47.95.121.155',
port = 3306,#注意此处为int类型;
user = 'tqtl',
password = 'Tqtl911!@%()123456',
db = 'LuffyCity',
charset = 'utf8'
)
#拿到游标;
cursor = conn.cursor() #执行SQL语句;
sql = 'select * from userinfo where username = "%s" and password = "%s"'%(usr,passwd)
print(sql)
rows = cursor.execute(sql) cursor.close()
conn.close() #运行判断
if rows:
print('登录成功!')
else:
print('登录失败!!!') """
Username:egon" -- xxxx
Password:
select * from userinfo where username = "egon" -- xxxx" and password = ""
登录成功!
出现了SQL注入现象;
"""
40-pymysql模块之sql注入
1、execure()之sql注入;
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# __Author__:TQTL911
# Version:python3.6.6
# Time:7/27/2018 6:41 PM
import pymysql user = input('Username:').strip()
passwd = input('Password:').strip() #建立链接对象;
conn = pymysql.connect(
host = '47.95.121.155',
port = 3306,#注意此处为int类型;
user = 'tqtl',
password = 'Tqtl911!@%()123456',
db = 'LuffyCity',
charset = 'utf8'
)
#拿到游标;
cursor = conn.cursor() #执行SQL语句;
#sql = 'select * from userinfo where username = "%s" and password = "%s"'%(user,passwd)
sql = 'select * from userinfo where username = %s and password = %s' #rows = cursor.execute(sql)
rows = cursor.execute(sql,(user,passwd))
"""
以上方法,避免了出现SQL注入;
""" cursor.close()
conn.close() #运行判断
if rows:
print('登录成功!')
else:
print('登录失败!!!')
解决思路:
# 原来是我们对sql进行字符串拼接;
sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd)
print(sql)
res=cursor.execute(sql) #改写为(execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了);
sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引号,因为pymysql会自动为我们加上
res=cursor.execute(sql,[user,pwd]) #pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。
41-pymysql模块之增删改查
1、增、删、改:conn.commit();
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# __Author__:TQTL911
# Version:python3.6.6
# Time:7/27/2018 6:59 PM
import pymysql #建立链接
conn = pymysql.connect(
host = '47.95.121.155',
port = 3306,
user = 'tqtl',
password = 'Tqtl911!@%()123456',
db = 'LuffyCity',
charset = 'utf8'
) #拿到数据
cursor = conn.cursor() #执行SQL语句
#增、删、改
sql = 'insert into userinfo(username,password) values(%s,%s)'
#rows = cursor.execute(sql,('cuixiaozhao','Ab123456.'))
rows = cursor.executemany(sql,[('cuixiaozhao','Ab123456.'),('lijingping','123'),('gaozhifen','456'),('cuiqingliang','111')])
print(rows)
print(cursor.lastrowid) conn.commit() #关闭链接
cursor.close()
conn.close() #查询操作;
import pymysql #建立链接
conn = pymysql.connect(
host = '47.95.121.155',
port = 3306,
user = 'tqtl',
password = 'Tqtl911!@%()123456',
db = 'LuffyCity',
charset = 'utf8'
) #拿到数据
#cursor = conn.cursor()#元组形式展示;
cursor = conn.cursor(pymysql.cursors.DictCursor)#字典形式显示; #执行SQL语句
#查询
rows =cursor.execute('select * from userinfo;') print(cursor.fetchone())#取出1个;
print(cursor.fetchone())
print(cursor.fetchone())
print(cursor.fetchone()) #print(cursor.fetchmany(3))#取出n个;
#print(cursor.fetchall())#取出所有; #移动光标
cursor.scroll(3,mode='absolute')#相对绝对位置移动
cursor.scroll(2,mode='relative')#相对当前位置移动
#关闭链接
cursor.close()
conn.close()
42-MySQL内置功能介绍
1、视图、触发器、存储过程、事务、函数及流程控制、索引原理;
43-视图
1、视图初识;
数据库中的视图是一个虚拟表,同真实的表一样,视图包含一系列带有名称的行和列数据。行和列数据来自由定义视图查询所引用的表,并且在引用视图时候动态生成。
2、视图的使用说明;
使用视图我们可以把查询过程中的临时表摘出来,用视图去实现,这样以后再想操作该临时表的数据时就无需重写复杂的sql了,
直接去视图中查找即可,但视图有明显地效率问题,并且视图是存放在数据库中的,
如果我们程序中使用的sql过分依赖数据库中的视图,即强耦合,那就意味着扩展sql极为不便,
因此并不推荐使用。
3、视图的创建与使用;
#语法:CREATE VIEW 视图名称 AS SQL语句
create view teacher_view as select tid from teacher where tname='李平老师'; #于是查询李平老师教授的课程名的sql可以改写为
mysql> select cname from course where teacher_id = (select tid from teacher_view);
+--------+
| cname |
+--------+
| 物理 |
| 美术 |
+--------+
rows in set (0.00 sec) #!!!注意注意注意:
#1. 使用视图以后就无需每次都重写子查询的sql,但是这么效率并不高,还不如我们写子查询的效率高; #2. 而且有一个致命的问题:视图是存放到数据库里的,如果我们程序中的sql过分依赖于数据库中存放的视图,
那么意味着,一旦sql需要修改且涉及到视图的部分,则必须去数据库中进行修改,而通常在公司中数据库有专门的DBA负责,
你要想完成修改,必须付出大量的沟通成本DBA可能才会帮你完成修改,极其地不方便
4、修改视图;
5、删除视图;
44-触发器
1、触发器概念初识(一碰就动);
45-存储过程
46-应用程序与数据库结合使用的三种方式
47-事务
48-函数与流程控制
49-索引原理
第四模块:网络编程进阶&数据库开发 第2章·MySQL数据库开发的更多相关文章
- 第六模块:WEB框架开发 第1章·Django框架开发1~50
01-Django基础介绍 02-Web应用程序1 03-Web应用程序2 04-http请求协议1 05-http请求协议2 06-http协议之响应协议 07-wsgire模块1 08-wsgir ...
- Python进阶----pymysql的安装与使用,mysql数据库的备份和恢复,mysql的事务和锁
Python进阶----pymysql的安装与使用,mysql数据库的备份和恢复,mysql的事务和锁 一丶安装 pip install PyMySQL 二丶pymysql连接数据库 ### 语法: ...
- Java进阶(二十五)Java连接mysql数据库(底层实现)
Java进阶(二十五)Java连接mysql数据库(底层实现) 前言 很长时间没有系统的使用java做项目了.现在需要使用java完成一个实验,其中涉及到java连接数据库.让自己来写,记忆中已无从搜 ...
- MySQL性能调优与架构设计——第9章 MySQL数据库Schema设计的性能优化
第9章 MySQL数据库Schema设计的性能优化 前言: 很多人都认为性能是在通过编写代码(程序代码或者是数据库代码)的过程中优化出来的,其实这是一个非常大的误区.真正影响性能最大的部分是在设计中就 ...
- MySQL性能调优与架构设计——第8章 MySQL数据库Query的优化
第8章 MySQL数据库Query的优化 前言: 在之前“影响 MySQL 应用系统性能的相关因素”一章中我们就已经分析过了Query语句对数据库性能的影响非常大,所以本章将专门针对 MySQL 的 ...
- MySQL性能调优与架构设计——第7章 MySQL数据库锁定机制
第7章 MySQL数据库锁定机制 前言: 为了保证数据的一致完整性,任何一个数据库都存在锁定机制.锁定机制的优劣直接应想到一个数据库系统的并发处理能力和性能,所以锁定机制的实现也就成为了各种数据库的核 ...
- mysql数据库优化课程---10、mysql数据库分组聚合
mysql数据库优化课程---10.mysql数据库分组聚合 一.总结 一句话总结:select concat(class,' 班') 班级,concat(count(*),' 人') 人数 from ...
- 第四模块:网络编程进阶&数据库开发 考核实战
1.什么是进程?什么是线程? 什么是协程? 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 线程:在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程 协程是一种用 ...
- 第四模块:网络编程进阶&数据库开发 口述
进程即正在执行的一个过程.进程是对正在运行程序的一个抽象. 子进程死了之后 ,父进程关闭的时候要清理掉子进程的僵尸进程(收尸),孤儿进程是指父进程先死掉了的,交给init管理. join() 等待子进 ...
随机推荐
- 关注磁盘的两个指标: IOPS 和传输带宽(吞吐量)
㈠ IOPS 磁盘的 IOPS.也就是每秒能进行多少次IO 那么.如何才算一次IO呢? 其实.这是个定义很混乱的问题 因为.系统 ...
- 利用python进行数据分析——(一)库的学习
总结一下自己对python常用包:Numpy,Pandas,Matplotlib,Scipy,Scikit-learn 一. Numpy: 标准安装的Python中用列表(list)保存一组值,可以用 ...
- 关于WEB的URL安全测试
测试思路: 对WEB做个简单的安全测试,主要是针对URL的测试. 回想起来,这次测试本质可以归为“权限”的测试,如下: 案例1: 1.分别开两个浏览器,以两个不同的帐号登陆web后台 2.第一个浏览器 ...
- Xcode 7.0 SDK(Software Development Kit) 及 Sandbox(沙盒) 存放路径
1. Sandbox(沙盒) 存放路径 我的硬盘/Users/wj121/Library/Developer/CoreSimulator/Devices/879D7E35-BE50-4620-97E1 ...
- 【Node.js学习笔记】使用Gulp项目自动化构建工具
刚接触node.js,对前端的一些东西还不是很清楚,据说Gulp这东西很强大,先来看看从网上抄的一段关于自动化构建的描述: 在为数众多的中小型软件作坊中,不存在自动化构建和发布工具.构建.交付准备环境 ...
- STM32F103片外运行代码分析
STM32F103片外运行代码分析 STM32F103有三种启动方式: 1.从片内Flash启动: 2.从片内RAM启动: 3.从片内系统存储器启动,内嵌的自举程序,用于串口IAP. 无法直接在片外N ...
- HTML表格属性及简单实例
这里主要总结记录下表格的一些属性和简单的样式,方便以后不时之需. 1.<table> 用来定义HTML的表格,具有本地属性 border 表示边框,border属性的值必须为1或空字符串( ...
- 【Django笔记二】Django2.0配置模板和静态文件
一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 二.创建模板 1.在my_project文件夹下新建文件夹templates用于存放模板文 ...
- 【补】英语对IT工作者的重要性
浅谈程序员的英语学习 作为在中国工作的程序员,不懂得英语似乎也不妨碍找到好工作,升职加薪.但程序员这个工种则稍有不同,因为程序,尤其是高级语言,基本上都是由英语和数字表达式构成的.英语对于程序员十 ...
- rz/sz:工作原理
我们知道用linux命令rz/sz可以通过一些终端软件如secureCRT等在linux服务器与本地windows之间传文件.在服务器上rz一下,在本地windows下就跳出一个窗口,选择文件后就传到 ...