Mysql数据库之华圣重工
严重的提醒:使用前请先使用数据库,使用数据库,使用数据库!
否则你将无厘头的一直报错,一直报错,一直报错。
虽然说这是最基本的,但很容易忽视。
如下
1064 - 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
严重的提醒:一定不要使用关键字,一定不要使用保留字!
否则你会无厘头的一直报错,一直报错,一直报错。
关键字和保留字请看附页。
如下
1064 - 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
Ax 创建数据库和数据表及表结构
华圣集团有一个项目,新成立了一个重工的子公司,现需要对员工的数据进行录入,新建一个数据库,并导入信息,你是数据库管理员,完成以下需求。
创建数据库huasheng_zg(华圣重工),表member(员工信息表),将以下数据放入其中。
字段名 | 数据类型 | 备注 | 是否主键 |
---|---|---|---|
eon_id | varchar(20) | 华圣ID号 | 是 |
eon_name | varchar(20) | 名字 | 否 |
eon_sex | varchar(10) | 性别 | 否 |
eon_birthday | date | 出生年月 | 否 |
eon_class | varchar(10) | 所在部门 | 否 |
-- 创建数据库
create database huasheng_zg;
use huasheng_zg;
-- 创建表、添加字段
create table member(
eon_id int comment "华圣ID",
eon_name varchar(20) comment "名字",
eon_sex varchar(10) comment "性别",
eon_brithday date comment "出生年月",
eon_class varchar(10) comment "所在部门",
PRIMARY KEY(eon_id)
)charset=utf8;
Bx 录入员工数据
a 添加记录
经过长期的招人,公司招聘以下人员,使用SQL语句录入到member表中。
ID | Name | Gender | Birthday | Department |
---|---|---|---|---|
106 | Eon | 男 | 2000-10-04 | CEO |
100 | 唐三 | 男 | 2000-02-28 | 秘书 |
108 | Pgg | 男 | 2000-12-30 | 信息安全部 |
110 | 白给 | 未知 | 2000-01-01 | 矛盾纠纷兼法务办公室 |
109 | 笑笑 | 女 | 2000-11-12 | 财务部 |
199 | 唐五 | 女 | 2000-02-21 | 唐门 |
-- 添加数据
insert member value
("106", "Eon" , "男" , "2000-10-04", "CEO"),
("100", "唐三" , "男" , "2000-02-28", "秘书"),
("108", "Pgg" , "男" , "2000-12-30", "信息安全部"),
("110", "白给" , "未知", "2000-01-01", "矛盾纠纷兼法务办公室"),
("109", "笑笑" , "女" , "2000-11-12", "财务部"),
("199", "唐五" , "女" , "2000-02-21", "唐门");
b 查询
现在,财务部门需要查看所有人的华圣ID号和名字还有所在部门
select eon_id,eon_name,eon_class from member;
c 去重查询
粗心的数据库管理员重复输入了Eon的信息,导致出现了两个Eon。
现在要查看去重部门字段之后的member表的eon_class字段。
最后,把重复多余的记录删除
select distinct eon_class from member;
delete from member where eon_id = "105";
d 排序查询
公司想给员工按照生日的顺序提醒信息部门送去生日祝福。
分别使用升序和降序对出生日期进行查看,只查看姓名、生日字段
# 一直查不出来,原来我birthday打错了,将错就错吧。
-- 升
select eon_name,eon_brithday from member order by eon_brithday asc;
-- 反(降序)
select eon_name,eon_brithday from member order by eon_brithday desc;
+----------+--------------+
| eon_name | eon_brithday |
+----------+--------------+
| 白给 | 2000-01-01 |
| 唐五 | 2000-02-21 |
| 唐三 | 2000-02-28 |
| Eon | 2000-10-04 |
| 笑笑 | 2000-11-12 |
| Pgg | 2000-12-30 |
+----------+--------------+
6 rows in set (0.04 sec)
+----------+--------------+
| eon_name | eon_brithday |
+----------+--------------+
| Pgg | 2000-12-30 |
| 笑笑 | 2000-11-12 |
| Eon | 2000-10-04 |
| 唐三 | 2000-02-28 |
| 唐五 | 2000-02-21 |
| 白给 | 2000-01-01 |
+----------+--------------+
6 rows in set (0.05 sec)
Cx 增加位置表
为了更详细的知道员工的所在位置信息,新增一个位置表(location)。
使Eon_ID字段设置为主键,并且自增
a 表结构
字段名字 | 类型 | 备注 | 是否主键 |
---|---|---|---|
Eon_id | int unsigned | 华圣ID | 是,自增 |
Eon_floor | varchar(20) | 楼层 | 否 |
Eon_ad_office | varchar(20) | 办公室 | 否 |
-- 创建位置表、添加字段
create table location(
Eon_id int unsigned AUTO_INCREMENT comment "华圣ID",
Eon_floor varchar(20) comment "楼层",
Eon_ad_office varchar(20) comment "办公室",
PRIMARY KEY(Eon_id)
)charset=utf8;
b 记录
Eon_ID | 楼层 | 办公室 |
---|---|---|
100 | 1F | 1032 |
106 | 2F | 2069 |
108 | 3F | 3001 |
109 | 4F | 4052 |
110 | 4F | 4053 |
199 | 1F | 1032 |
-- 添加数据
insert location value
("100","1F","1032"),
("106","2F","2069"),
("108","3F","3001"),
("109","4F","4052"),
("110","4F","4053"),
("199","1F","1032");
C 子查询
有人向前台询问笑笑的办公室位置,通过数据库查询笑笑所在楼层及办公室
-- 子查询
select Eon_floor,Eon_ad_office from location
where eon_id = (
select eon_id from member where eon_name = "笑笑"
);
现在急需一批搬运工
查询所有男生所在楼层和办公室位置
select Eon_floor,Eon_ad_office from location
where eon_id in (
select eon_id from member where eon_sex = "男"
);
左思右想,怎么少了一个人,原来是白给。
d 内连接查询
这样太麻烦了,为此使用语句将所有人对应的ID、姓名、部门以及楼层和办公室位置升序的方式查询出来,然后打印成一张表。
-- 内连接
select member.eon_id,member.eon_name,member.eon_class,location.Eon_floor,location.Eon_ad_office from member
inner join location on member.eon_id = location.Eon_id;
/*
简洁写法
*/
mysql> select m.eon_id,m.eon_name,m.eon_class,l.Eon_floor,l.Eon_ad_office from member m inner join location l on m.eon_id = l.Eon_id;
+--------+----------+----------------------+-----------+---------------+
| eon_id | eon_name | eon_class | Eon_floor | Eon_ad_office |
+--------+----------+----------------------+-----------+---------------+
| 100 | 唐三 | 秘书 | 1F | 1032 |
| 106 | Eon | CEO | 2F | 2069 |
| 108 | Pgg | 信息安全部 | 3F | 3001 |
| 109 | 笑笑 | 财务部 | 4F | 4052 |
| 110 | 白给 | 矛盾纠纷兼法务办公室 | 4F | 4053 |
| 199 | 唐五 | 唐门 | 1F | 1032 |
+--------+----------+----------------------+-----------+---------------+
6 rows in set (0.05 sec)
# 也就是在分别在member和location后面添加别称,然后每次描述字段的时候就可以使用别称了
# 排序
mysql> select m.eon_id,m.eon_name,m.eon_class,l.Eon_floor,l.Eon_ad_office from member m inner join location l on m.eon_id = l.Eon_id order by Eon_ad_office asc;
Fx 附页:关键字和保留字
A
ACCESSIBLE
(R)ACCOUNT
; added in 5.7.6 (nonreserved)ACTION
ADD
(R)AFTER
AGAINST
AGGREGATE
ALGORITHM
ALL
(R)ALTER
(R)ALWAYS
; added in 5.7.6 (nonreserved)ANALYSE
ANALYZE
(R)AND
(R)ANY
AS
(R)ASC
(R)ASCII
ASENSITIVE
(R)AT
AUTOEXTEND_SIZE
AUTO_INCREMENT
AVG
AVG_ROW_LENGTH
B
BACKUP
BEFORE
(R)BEGIN
BETWEEN
(R)BIGINT
(R)BINARY
(R)BINLOG
BIT
BLOB
(R)BLOCK
BOOL
BOOLEAN
BOTH
(R)BTREE
BY
(R)BYTE
C
CACHE
CALL
(R)CASCADE
(R)CASCADED
CASE
(R)CATALOG_NAME
CHAIN
CHANGE
(R)CHANGED
CHANNEL
; added in 5.7.6 (nonreserved)CHAR
(R)CHARACTER
(R)CHARSET
CHECK
(R)CHECKSUM
CIPHER
CLASS_ORIGIN
CLIENT
CLOSE
COALESCE
CODE
COLLATE
(R)COLLATION
COLUMN
(R)COLUMNS
COLUMN_FORMAT
COLUMN_NAME
COMMENT
COMMIT
COMMITTED
COMPACT
COMPLETION
COMPRESSED
COMPRESSION
; added in 5.7.8 (nonreserved)CONCURRENT
CONDITION
(R)CONNECTION
CONSISTENT
CONSTRAINT
(R)CONSTRAINT_CATALOG
CONSTRAINT_NAME
CONSTRAINT_SCHEMA
CONTAINS
CONTEXT
CONTINUE
(R)CONVERT
(R)CPU
CREATE
(R)CROSS
(R)CUBE
CURRENT
CURRENT_DATE
(R)CURRENT_TIME
(R)CURRENT_TIMESTAMP
(R)CURRENT_USER
(R)CURSOR
(R)CURSOR_NAME
D
DATA
DATABASE
(R)DATABASES
(R)DATAFILE
DATE
DATETIME
DAY
DAY_HOUR
(R)DAY_MICROSECOND
(R)DAY_MINUTE
(R)DAY_SECOND
(R)DEALLOCATE
DEC
(R)DECIMAL
(R)DECLARE
(R)DEFAULT
(R)DEFAULT_AUTH
DEFINER
DELAYED
(R)DELAY_KEY_WRITE
DELETE
(R)DESC
(R)DESCRIBE
(R)DES_KEY_FILE
DETERMINISTIC
(R)DIAGNOSTICS
DIRECTORY
DISABLE
DISCARD
DISK
DISTINCT
(R)DISTINCTROW
(R)DIV
(R)DO
DOUBLE
(R)DROP
(R)DUAL
(R)DUMPFILE
DUPLICATE
DYNAMIC
E
EACH
(R)ELSE
(R)ELSEIF
(R)ENABLE
ENCLOSED
(R)ENCRYPTION
; added in 5.7.11 (nonreserved)END
ENDS
ENGINE
ENGINES
ENUM
ERROR
ERRORS
ESCAPE
ESCAPED
(R)EVENT
EVENTS
EVERY
EXCHANGE
EXECUTE
EXISTS
(R)EXIT
(R)EXPANSION
EXPIRE
EXPLAIN
(R)EXPORT
EXTENDED
EXTENT_SIZE
F
FALSE
(R)FAST
FAULTS
FETCH
(R)FIELDS
FILE
FILE_BLOCK_SIZE
; added in 5.7.6 (nonreserved)FILTER
; added in 5.7.3 (nonreserved)FIRST
FIXED
FLOAT
(R)FLOAT4
(R)FLOAT8
(R)FLUSH
FOLLOWS
; added in 5.7.2 (nonreserved)FOR
(R)FORCE
(R)FOREIGN
(R)FORMAT
FOUND
FROM
(R)FULL
FULLTEXT
(R)FUNCTION
G
GENERAL
GENERATED
(R); added in 5.7.6 (reserved)GEOMETRY
GEOMETRYCOLLECTION
GET
(R)GET_FORMAT
GLOBAL
GRANT
(R)GRANTS
GROUP
(R)GROUP_REPLICATION
; added in 5.7.6 (nonreserved)
H
HANDLER
HASH
HAVING
(R)HELP
HIGH_PRIORITY
(R)HOST
HOSTS
HOUR
HOUR_MICROSECOND
(R)HOUR_MINUTE
(R)HOUR_SECOND
(R)
I
IDENTIFIED
IF
(R)IGNORE
(R)IGNORE_SERVER_IDS
IMPORT
IN
(R)INDEX
(R)INDEXES
INFILE
(R)INITIAL_SIZE
INNER
(R)INOUT
(R)INSENSITIVE
(R)INSERT
(R)INSERT_METHOD
INSTALL
INSTANCE
; added in 5.7.11 (nonreserved)INT
(R)INT1
(R)INT2
(R)INT3
(R)INT4
(R)INT8
(R)INTEGER
(R)INTERVAL
(R)INTO
(R)INVOKER
IO
IO_AFTER_GTIDS
(R)IO_BEFORE_GTIDS
(R)IO_THREAD
IPC
IS
(R)ISOLATION
ISSUER
ITERATE
(R)
J
JOIN
(R)JSON
; added in 5.7.8 (nonreserved)
K
KEY
(R)KEYS
(R)KEY_BLOCK_SIZE
KILL
(R)
L
LANGUAGE
LAST
LEADING
(R)LEAVE
(R)LEAVES
LEFT
(R)LESS
LEVEL
LIKE
(R)LIMIT
(R)LINEAR
(R)LINES
(R)LINESTRING
LIST
LOAD
(R)LOCAL
LOCALTIME
(R)LOCALTIMESTAMP
(R)LOCK
(R)LOCKS
LOGFILE
LOGS
LONG
(R)LONGBLOB
(R)LONGTEXT
(R)LOOP
(R)LOW_PRIORITY
(R)
M
MASTER
MASTER_AUTO_POSITION
MASTER_BIND
(R)MASTER_CONNECT_RETRY
MASTER_DELAY
MASTER_HEARTBEAT_PERIOD
MASTER_HOST
MASTER_LOG_FILE
MASTER_LOG_POS
MASTER_PASSWORD
MASTER_PORT
MASTER_RETRY_COUNT
MASTER_SERVER_ID
MASTER_SSL
MASTER_SSL_CA
MASTER_SSL_CAPATH
MASTER_SSL_CERT
MASTER_SSL_CIPHER
MASTER_SSL_CRL
MASTER_SSL_CRLPATH
MASTER_SSL_KEY
MASTER_SSL_VERIFY_SERVER_CERT
(R)MASTER_TLS_VERSION
; added in 5.7.10 (nonreserved)MASTER_USER
MATCH
(R)MAXVALUE
(R)MAX_CONNECTIONS_PER_HOUR
MAX_QUERIES_PER_HOUR
MAX_ROWS
MAX_SIZE
MAX_STATEMENT_TIME
; added in 5.7.4 (nonreserved); removed in 5.7.8MAX_UPDATES_PER_HOUR
MAX_USER_CONNECTIONS
MEDIUM
MEDIUMBLOB
(R)MEDIUMINT
(R)MEDIUMTEXT
(R)MEMORY
MERGE
MESSAGE_TEXT
MICROSECOND
MIDDLEINT
(R)MIGRATE
MINUTE
MINUTE_MICROSECOND
(R)MINUTE_SECOND
(R)MIN_ROWS
MOD
(R)MODE
MODIFIES
(R)MODIFY
MONTH
MULTILINESTRING
MULTIPOINT
MULTIPOLYGON
MUTEX
MYSQL_ERRNO
N
NAME
NAMES
NATIONAL
NATURAL
(R)NCHAR
NDB
NDBCLUSTER
NEVER
; added in 5.7.4 (nonreserved)NEW
NEXT
NO
NODEGROUP
NONBLOCKING
; removed in 5.7.6NONE
NOT
(R)NO_WAIT
NO_WRITE_TO_BINLOG
(R)NULL
(R)NUMBER
NUMERIC
(R)NVARCHAR
O
OFFSET
OLD_PASSWORD
; removed in 5.7.5ON
(R)ONE
ONLY
OPEN
OPTIMIZE
(R)OPTIMIZER_COSTS
(R); added in 5.7.5 (reserved)OPTION
(R)OPTIONALLY
(R)OPTIONS
OR
(R)ORDER
(R)OUT
(R)OUTER
(R)OUTFILE
(R)OWNER
P
PACK_KEYS
PAGE
PARSER
PARSE_GCOL_EXPR
; added in 5.7.6 (reserved); became nonreserved in 5.7.8PARTIAL
PARTITION
(R)PARTITIONING
PARTITIONS
PASSWORD
PHASE
PLUGIN
PLUGINS
PLUGIN_DIR
POINT
POLYGON
PORT
PRECEDES
; added in 5.7.2 (nonreserved)PRECISION
(R)PREPARE
PRESERVE
PREV
PRIMARY
(R)PRIVILEGES
PROCEDURE
(R)PROCESSLIST
PROFILE
PROFILES
PROXY
PURGE
(R)
Q
QUARTER
QUERY
QUICK
R
RANGE
(R)READ
(R)READS
(R)READ_ONLY
READ_WRITE
(R)REAL
(R)REBUILD
RECOVER
REDOFILE
REDO_BUFFER_SIZE
REDUNDANT
REFERENCES
(R)REGEXP
(R)RELAY
RELAYLOG
RELAY_LOG_FILE
RELAY_LOG_POS
RELAY_THREAD
RELEASE
(R)RELOAD
REMOVE
RENAME
(R)REORGANIZE
REPAIR
REPEAT
(R)REPEATABLE
REPLACE
(R)REPLICATE_DO_DB
; added in 5.7.3 (nonreserved)REPLICATE_DO_TABLE
; added in 5.7.3 (nonreserved)REPLICATE_IGNORE_DB
; added in 5.7.3 (nonreserved)REPLICATE_IGNORE_TABLE
; added in 5.7.3 (nonreserved)REPLICATE_REWRITE_DB
; added in 5.7.3 (nonreserved)REPLICATE_WILD_DO_TABLE
; added in 5.7.3 (nonreserved)REPLICATE_WILD_IGNORE_TABLE
; added in 5.7.3 (nonreserved)REPLICATION
REQUIRE
(R)RESET
RESIGNAL
(R)RESTORE
RESTRICT
(R)RESUME
RETURN
(R)RETURNED_SQLSTATE
RETURNS
REVERSE
REVOKE
(R)RIGHT
(R)RLIKE
(R)ROLLBACK
ROLLUP
ROTATE
; added in 5.7.11 (nonreserved)ROUTINE
ROW
ROWS
ROW_COUNT
ROW_FORMAT
RTREE
S
SAVEPOINT
SCHEDULE
SCHEMA
(R)SCHEMAS
(R)SCHEMA_NAME
SECOND
SECOND_MICROSECOND
(R)SECURITY
SELECT
(R)SENSITIVE
(R)SEPARATOR
(R)SERIAL
SERIALIZABLE
SERVER
SESSION
SET
(R)SHARE
SHOW
(R)SHUTDOWN
SIGNAL
(R)SIGNED
SIMPLE
SLAVE
SLOW
SMALLINT
(R)SNAPSHOT
SOCKET
SOME
SONAME
SOUNDS
SOURCE
SPATIAL
(R)SPECIFIC
(R)SQL
(R)SQLEXCEPTION
(R)SQLSTATE
(R)SQLWARNING
(R)SQL_AFTER_GTIDS
SQL_AFTER_MTS_GAPS
SQL_BEFORE_GTIDS
SQL_BIG_RESULT
(R)SQL_BUFFER_RESULT
SQL_CACHE
SQL_CALC_FOUND_ROWS
(R)SQL_NO_CACHE
SQL_SMALL_RESULT
(R)SQL_THREAD
SQL_TSI_DAY
SQL_TSI_HOUR
SQL_TSI_MINUTE
SQL_TSI_MONTH
SQL_TSI_QUARTER
SQL_TSI_SECOND
SQL_TSI_WEEK
SQL_TSI_YEAR
SSL
(R)STACKED
START
STARTING
(R)STARTS
STATS_AUTO_RECALC
STATS_PERSISTENT
STATS_SAMPLE_PAGES
STATUS
STOP
STORAGE
STORED
(R); added in 5.7.6 (reserved)STRAIGHT_JOIN
(R)STRING
SUBCLASS_ORIGIN
SUBJECT
SUBPARTITION
SUBPARTITIONS
SUPER
SUSPEND
SWAPS
SWITCHES
T
TABLE
(R)TABLES
TABLESPACE
TABLE_CHECKSUM
TABLE_NAME
TEMPORARY
TEMPTABLE
TERMINATED
(R)TEXT
THAN
THEN
(R)TIME
TIMESTAMP
TIMESTAMPADD
TIMESTAMPDIFF
TINYBLOB
(R)TINYINT
(R)TINYTEXT
(R)TO
(R)TRAILING
(R)TRANSACTION
TRIGGER
(R)TRIGGERS
TRUE
(R)TRUNCATE
TYPE
TYPES
U
UNCOMMITTED
UNDEFINED
UNDO
(R)UNDOFILE
UNDO_BUFFER_SIZE
UNICODE
UNINSTALL
UNION
(R)UNIQUE
(R)UNKNOWN
UNLOCK
(R)UNSIGNED
(R)UNTIL
UPDATE
(R)UPGRADE
USAGE
(R)USE
(R)USER
USER_RESOURCES
USE_FRM
USING
(R)UTC_DATE
(R)UTC_TIME
(R)UTC_TIMESTAMP
(R)
V
VALIDATION
; added in 5.7.5 (nonreserved)VALUE
VALUES
(R)VARBINARY
(R)VARCHAR
(R)VARCHARACTER
(R)VARIABLES
VARYING
(R)VIEW
VIRTUAL
(R); added in 5.7.6 (reserved)
W
WAIT
WARNINGS
WEEK
WEIGHT_STRING
WHEN
(R)WHERE
(R)WHILE
(R)WITH
(R)WITHOUT
; added in 5.7.5 (nonreserved)WORK
WRAPPER
WRITE
(R)
X
X509
XA
XID
; added in 5.7.5 (nonreserved)XML
XOR
(R)
Y
YEAR
YEAR_MONTH
(R)
Z
ZEROFILL
(R)
MySQL 5.7 New Keywords and Reserved Words
A
ACCOUNT
ALWAYS
C
CHANNEL
COMPRESSION
E
ENCRYPTION
F
FILE_BLOCK_SIZE
FILTER
FOLLOWS
G
GENERATED
(R)GROUP_REPLICATION
I
INSTANCE
J
JSON
M
MASTER_TLS_VERSION
N
NEVER
O
OPTIMIZER_COSTS
(R)
P
PARSE_GCOL_EXPR
PRECEDES
R
REPLICATE_DO_DB
REPLICATE_DO_TABLE
REPLICATE_IGNORE_DB
REPLICATE_IGNORE_TABLE
REPLICATE_REWRITE_DB
REPLICATE_WILD_DO_TABLE
REPLICATE_WILD_IGNORE_TABLE
ROTATE
S
STACKED
STORED
(R)
V
VALIDATION
VIRTUAL
(R)
W
WITHOUT
X
XID
MySQL 5.7 Removed Keywords and Reserved Words
The following list shows the keywords and reserved words that are removed in MySQL 5.7, compared to MySQL 5.6. Reserved keywords are marked with (R).
OLD_PASSWORD