MySQL数据类型以及基本使用详解
MySQL数据类型以及基本使用详解
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.MySQL服务器的主要组件
我们知道MySQL的主要组件主要是由服务端(mysqld)和客户端(mysql)组成的。它们都共用一个配置文件(通常叫做my.cnf),这个配置文件很独特,它需要使用中括号括起来标明是为哪种组件使用的,例如[mysql]下面的指令就表示为客户端配置的参数,如果[mysqld]下面的指令就表示为服务端配置的参数。其实MySQL的客户端组件有很多个,本篇博客是用的mysql命令只是MySQL客户端之一,其他常见的客户端如:Navicat for Mysql,EMSSQL ManagerforMySQL等等。
在Linux操作系统中我还是推荐使用mysql客户端的,如果在windows的话我推荐使用Navicat for Mysql,mysql在Linux连接服务器是很简单,我们用mysql连接数据库的时候通常会用到三个参数,分别是“-u(指定登录数据库的用户名)”,“-h(指定登录数据库的主机名)”,“-p(指定登录数据库的密码)”,“-e(指定SQL命令即可执行)”等等。
二.MySQL用户的分类
其实在MySQL初始化时,它会默认创建两类账户,一种是管理员用户(root),另外一种是匿名用户。
1 root: //管理员用户 2 127.0.0.1 //IPV4本地回环地址 3 localhost //本机的主机名 4 Hostname //主机名 5 ::1 //IPV6本地回环地址 6 " ": //匿名用户 7 localhost 8 hostname
这两类用户是系统默认的,当然我们不推荐直接使用它们。因为它们很不安全(只要看了我这篇博客的人都能通过这种方式去连接你的MySQL,因为他们都知道mysql的用户信息。),第一,匿名用户访问无法记录来访者,谁都可以使用,就类似于FTP服务器。第二,管理员用户默认是没有密码的,这就更不全啦,就好像你的办公电脑没有设置密码你很没有安全感似的。因此,我们需要删除不必要的用户,比如MySQL服务默认的2个匿名用户,给我们需要留存的用户设置密码[“SET PASSWORD FOR 'username'@'hostname/IP' = PASSWORD('yinzhengjie');”]。
三.MySQL客户端
1.客户端命令
客户端命令表示是在客户端执行的命令,我们只需要成功连接数据库之后输入“help”,就可以看到mysql客户端的命令啦。客户端的命令有个特点就是不需要命令结束符,比如“;”。
1 mysql> help 2 3 For information about MySQL products and services, visit: 4 http://www.mysql.com/ 5 For developer information, including the MySQL Reference Manual, visit: 6 http://dev.mysql.com/ 7 To buy MySQL Enterprise support, training, or other products, visit: 8 https://shop.mysql.com/ 9 10 List of all MySQL commands: 11 Note that all text commands must be first on line and end with ';' 12 ? (\?) Synonym for `help'. 13 clear (\c) Clear the current input statement. 14 connect (\r) Reconnect to the server. Optional arguments are db and host. 15 delimiter (\d) Set statement delimiter. 16 edit (\e) Edit command with $EDITOR. 17 ego (\G) Send command to mysql server, display result vertically. 18 exit (\q) Exit mysql. Same as quit. 19 go (\g) Send command to mysql server. 20 help (\h) Display this help. 21 nopager (\n) Disable pager, print to stdout. 22 notee (\t) Don't write into outfile. 23 pager (\P) Set PAGER [to_pager]. Print the query results via PAGER. 24 print (\p) Print current command. 25 prompt (\R) Change your mysql prompt. 26 quit (\q) Quit mysql. 27 rehash (\#) Rebuild completion hash. 28 source (\.) Execute an SQL script file. Takes a file name as an argument. 29 status (\s) Get status information from the server. 30 system (\!) Execute a system shell command. 31 tee (\T) Set outfile [to_outfile]. Append everything into given outfile. 32 use (\u) Use another database. Takes database name as argument. 33 charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets. 34 warnings (\W) Show warnings after every statement. 35 nowarning (\w) Don't show warnings after every statement. 36 37 For server side help, type 'help contents' 38 39 mysql>
1 mysql> status 2 -------------- 3 mysql Ver 14.14 Distrib 5.5.54, for linux2.6 (x86_64) using readline 5.1 4 5 Connection id: 4 6 Current database: 7 Current user: root@localhost 8 SSL: Not in use 9 Current pager: stdout 10 Using outfile: '' 11 Using delimiter: ; 12 Server version: 5.5.54-log MySQL Community Server (GPL) 13 Protocol version: 10 14 Connection: Localhost via UNIX socket 15 Server characterset: latin1 16 Db characterset: latin1 17 Client characterset: utf8 18 Conn. characterset: utf8 19 UNIX socket: /tmp/mysql.sock 20 Uptime: 49 min 43 sec 21 22 Threads: 1 Questions: 11 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.003 23 -------------- 24 25 mysql>
1 [root@yinzhengjie ~]# mysql -u root -p123 2 Welcome to the MySQL monitor. Commands end with ; or \g. 3 Your MySQL connection id is 6 4 Server version: 5.5.54-log MySQL Community Server (GPL) 5 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 7 8 Oracle is a registered trademark of Oracle Corporation and/or its 9 affiliates. Other names may be trademarks of their respective 10 owners. 11 12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14 mysql> \q 15 Bye 16 [root@yinzhengjie ~]#
1 [root@yinzhengjie ~]# mysql -u root -p123 2 Welcome to the MySQL monitor. Commands end with ; or \g. 3 Your MySQL connection id is 7 4 Server version: 5.5.54-log MySQL Community Server (GPL) 5 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 7 8 Oracle is a registered trademark of Oracle Corporation and/or its 9 affiliates. Other names may be trademarks of their respective 10 owners. 11 12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14 mysql> \? 15 16 For information about MySQL products and services, visit: 17 http://www.mysql.com/ 18 For developer information, including the MySQL Reference Manual, visit: 19 http://dev.mysql.com/ 20 To buy MySQL Enterprise support, training, or other products, visit: 21 https://shop.mysql.com/ 22 23 List of all MySQL commands: 24 Note that all text commands must be first on line and end with ';' 25 ? (\?) Synonym for `help'. 26 clear (\c) Clear the current input statement. 27 connect (\r) Reconnect to the server. Optional arguments are db and host. 28 delimiter (\d) Set statement delimiter. 29 edit (\e) Edit command with $EDITOR. 30 ego (\G) Send command to mysql server, display result vertically. 31 exit (\q) Exit mysql. Same as quit. 32 go (\g) Send command to mysql server. 33 help (\h) Display this help. 34 nopager (\n) Disable pager, print to stdout. 35 notee (\t) Don't write into outfile. 36 pager (\P) Set PAGER [to_pager]. Print the query results via PAGER. 37 print (\p) Print current command. 38 prompt (\R) Change your mysql prompt. 39 quit (\q) Quit mysql. 40 rehash (\#) Rebuild completion hash. 41 source (\.) Execute an SQL script file. Takes a file name as an argument. 42 status (\s) Get status information from the server. 43 system (\!) Execute a system shell command. 44 tee (\T) Set outfile [to_outfile]. Append everything into given outfile. 45 use (\u) Use another database. Takes database name as argument. 46 charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets. 47 warnings (\W) Show warnings after every statement. 48 nowarning (\w) Don't show warnings after every statement. 49 50 For server side help, type 'help contents' 51 52 mysql>
1 [root@yinzhengjie ~]# mysql -u root -p123 2 Welcome to the MySQL monitor. Commands end with ; or \g. 3 Your MySQL connection id is 8 4 Server version: 5.5.54-log MySQL Community Server (GPL) 5 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 7 8 Oracle is a registered trademark of Oracle Corporation and/or its 9 affiliates. Other names may be trademarks of their respective 10 owners. 11 12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14 mysql> 15 mysql> select user()\g 16 +----------------+ 17 | user() | 18 +----------------+ 19 | root@localhost | 20 +----------------+ 21 1 row in set (0.00 sec) 22 23 mysql>
1 [root@yinzhengjie ~]# mysql -u root -p123 2 Welcome to the MySQL monitor. Commands end with ; or \g. 3 Your MySQL connection id is 10 4 Server version: 5.5.54-log MySQL Community Server (GPL) 5 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 7 8 Oracle is a registered trademark of Oracle Corporation and/or its 9 affiliates. Other names may be trademarks of their respective 10 owners. 11 12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14 mysql> use mysql 15 Database changed 16 mysql> select User,Host,Password from user; 17 +------+-------------+-------------------------------------------+ 18 | User | Host | Password | 19 +------+-------------+-------------------------------------------+ 20 | root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | 21 | root | yinzhengjie | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | 22 | root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | 23 | root | ::1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 | 24 | | localhost | | 25 | | yinzhengjie | | 26 +------+-------------+-------------------------------------------+ 27 6 rows in set (0.00 sec) 28 29 mysql> 30 mysql> select User,Host,Password from user\G 31 *************************** 1. row *************************** 32 User: root 33 Host: localhost 34 Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 35 *************************** 2. row *************************** 36 User: root 37 Host: yinzhengjie 38 Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 39 *************************** 3. row *************************** 40 User: root 41 Host: 127.0.0.1 42 Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 43 *************************** 4. row *************************** 44 User: root 45 Host: ::1 46 Password: *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 47 *************************** 5. row *************************** 48 User: 49 Host: localhost 50 Password: 51 *************************** 6. row *************************** 52 User: 53 Host: yinzhengjie 54 Password: 55 6 rows in set (0.00 sec) 56 57 mysql>
1 [root@yinzhengjie ~]# mysql -u root -p123 2 Welcome to the MySQL monitor. Commands end with ; or \g. 3 Your MySQL connection id is 11 4 Server version: 5.5.54-log MySQL Community Server (GPL) 5 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 7 8 Oracle is a registered trademark of Oracle Corporation and/or its 9 affiliates. Other names may be trademarks of their respective 10 owners. 11 12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14 mysql> select \c 15 mysql> 16 mysql> show databases; 17 +--------------------+ 18 | Database | 19 +--------------------+ 20 | information_schema | 21 | mysql | 22 | performance_schema | 23 | test | 24 +--------------------+ 25 4 rows in set (0.01 sec) 26 27 mysql> 28 mysql> sdas sad as \c 29 mysql> 30 mysql>
1 [root@yinzhengjie ~]# mysql -u root -p123 2 Welcome to the MySQL monitor. Commands end with ; or \g. 3 Your MySQL connection id is 16 4 Server version: 5.5.54-log MySQL Community Server (GPL) 5 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 7 8 Oracle is a registered trademark of Oracle Corporation and/or its 9 affiliates. Other names may be trademarks of their respective 10 owners. 11 12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14 mysql> show databases; 15 +--------------------+ 16 | Database | 17 +--------------------+ 18 | information_schema | 19 | mysql | 20 | performance_schema | 21 | test | 22 +--------------------+ 23 4 rows in set (0.00 sec) 24 25 mysql> \umysql 26 Database changed 27 mysql> 28 mysql> use mysql 29 Database changed 30 mysql>
2.服务器端命令
服务器端命令简直就时多了去了,比客户端的也复杂的多,因此我在这里也不打算一一举例,而是用一些常用的命令进行扫盲模式。执行服务端的命令需要语句终止符,通常默认为分号(;),当然这个命令结束符(;)是可以被修改的。
a>.SELECT命令的基本使用
MySQL服务器端有很多内建函数( 简称BIF)。使用select命令去执行一个内建函数,并将该内建函数的执行结果返回给当前用户。
1 mysql> select user(); #查看数据库的用户信息。 2 +----------------+ 3 | user() | 4 +----------------+ 5 | root@localhost | 6 +----------------+ 7 1 row in set (0.02 sec) 8 9 mysql> 10 mysql> select current_time(); #查看当前操作系统的时间。 11 +----------------+ 12 | current_time() | 13 +----------------+ 14 | 00:25:33 | 15 +----------------+ 16 1 row in set (0.00 sec) 17 18 mysql>
1 mysql> select 100+200; #利用select做算术运算。 2 +---------+ 3 | 100+200 | 4 +---------+ 5 | 300 | 6 +---------+ 7 1 row in set (0.00 sec) 8 9 mysql> 10 mysql> select 5*6; 11 +-----+ 12 | 5*6 | 13 +-----+ 14 | 30 | 15 +-----+ 16 1 row in set (0.00 sec) 17 18 mysql>
b>.SHOW命令的基本使用
1 mysql> show databases; 2 +--------------------+ 3 | Database | 4 +--------------------+ 5 | information_schema | 6 | mysql | 7 | performance_schema | 8 | test | 9 +--------------------+ 10 4 rows in set (0.00 sec) 11 12 mysql>
1 mysql> use mysql 2 Database changed 3 mysql> show tables; 4 +---------------------------+ 5 | Tables_in_mysql | 6 +---------------------------+ 7 | columns_priv | 8 | db | 9 | event | 10 | func | 11 | general_log | 12 | help_category | 13 | help_keyword | 14 | help_relation | 15 | help_topic | 16 | host | 17 | ndb_binlog_index | 18 | plugin | 19 | proc | 20 | procs_priv | 21 | proxies_priv | 22 | servers | 23 | slow_log | 24 | tables_priv | 25 | time_zone | 26 | time_zone_leap_second | 27 | time_zone_name | 28 | time_zone_transition | 29 | time_zone_transition_type | 30 | user | 31 +---------------------------+ 32 24 rows in set (0.00 sec) 33 34 mysql>
1 [root@yinzhengjie ~]# mysql -u root -p123 2 Welcome to the MySQL monitor. Commands end with ; or \g. 3 Your MySQL connection id is 17 4 Server version: 5.5.54-log MySQL Community Server (GPL) 5 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 7 8 Oracle is a registered trademark of Oracle Corporation and/or its 9 affiliates. Other names may be trademarks of their respective 10 owners. 11 12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14 mysql> show databases; 15 +--------------------+ 16 | Database | 17 +--------------------+ 18 | information_schema | 19 | mysql | 20 | performance_schema | 21 | test | 22 +--------------------+ 23 4 rows in set (0.00 sec) 24 25 mysql> create database yinzhengjie; 26 Query OK, 1 row affected (0.01 sec) 27 28 mysql> show databases; 29 +--------------------+ 30 | Database | 31 +--------------------+ 32 | information_schema | 33 | mysql | 34 | performance_schema | 35 | test | 36 | yinzhengjie | 37 +--------------------+ 38 5 rows in set (0.00 sec) 39 40 mysql>
1 [root@yinzhengjie ~]# mysql -u root -p123 2 Welcome to the MySQL monitor. Commands end with ; or \g. 3 Your MySQL connection id is 18 4 Server version: 5.5.54-log MySQL Community Server (GPL) 5 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 7 8 Oracle is a registered trademark of Oracle Corporation and/or its 9 affiliates. Other names may be trademarks of their respective 10 owners. 11 12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14 mysql> show databases; 15 +--------------------+ 16 | Database | 17 +--------------------+ 18 | information_schema | 19 | mysql | 20 | performance_schema | 21 | test | 22 | yinzhengjie | 23 +--------------------+ 24 5 rows in set (0.00 sec) 25 26 mysql> drop database yinzhengjie; 27 Query OK, 0 rows affected (0.01 sec) 28 29 mysql> show databases; 30 +--------------------+ 31 | Database | 32 +--------------------+ 33 | information_schema | 34 | mysql | 35 | performance_schema | 36 | test | 37 +--------------------+ 38 4 rows in set (0.00 sec) 39 40 mysql>
3.命令帮助的获取
不管是客户端还是服务端命令都是可以通过关键字“help”来获取帮助信息,当然这得需要MySQL展开其内部的帮助文档才能获取到相应的信息的。可能有的童鞋会问,如果展开它的帮助文档呢?其实在我们安装完MySQL的时候就以及默认展开啦。命令本身不区分字符大小写,但与文件系统相关的部分则根据os的不同,可能区分大小写(比如数据库名称和表名等等。)
1 mysql> help select 2 Name: 'SELECT' 3 Description: 4 Syntax: 5 SELECT 6 [ALL | DISTINCT | DISTINCTROW ] 7 [HIGH_PRIORITY] 8 [STRAIGHT_JOIN] 9 [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] 10 [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] 11 select_expr [, select_expr ...] 12 [FROM table_references 13 [WHERE where_condition] 14 [GROUP BY {col_name | expr | position} 15 [ASC | DESC], ... [WITH ROLLUP]] 16 [HAVING where_condition] 17 [ORDER BY {col_name | expr | position} 18 [ASC | DESC], ...] 19 [LIMIT {[offset,] row_count | row_count OFFSET offset}] 20 [PROCEDURE procedure_name(argument_list)] 21 [INTO OUTFILE 'file_name' 22 [CHARACTER SET charset_name] 23 export_options 24 | INTO DUMPFILE 'file_name' 25 | INTO var_name [, var_name]] 26 [FOR UPDATE | LOCK IN SHARE MODE]] 27 28 SELECT is used to retrieve rows selected from one or more tables, and 29 can include UNION statements and subqueries. See [HELP UNION], and 30 http://dev.mysql.com/doc/refman/5.5/en/subqueries.html. 31 32 The most commonly used clauses of SELECT statements are these: 33 34 o Each select_expr indicates a column that you want to retrieve. There 35 must be at least one select_expr. 36 37 o table_references indicates the table or tables from which to retrieve 38 rows. Its syntax is described in [HELP JOIN]. 39 40 o The WHERE clause, if given, indicates the condition or conditions 41 that rows must satisfy to be selected. where_condition is an 42 expression that evaluates to true for each row to be selected. The 43 statement selects all rows if there is no WHERE clause. 44 45 In the WHERE expression, you can use any of the functions and 46 operators that MySQL supports, except for aggregate (summary) 47 functions. See 48 http://dev.mysql.com/doc/refman/5.5/en/expressions.html, and 49 http://dev.mysql.com/doc/refman/5.5/en/functions.html. 50 51 SELECT can also be used to retrieve rows computed without reference to 52 any table. 53 54 URL: http://dev.mysql.com/doc/refman/5.5/en/select.html 55 56 57 mysql>
4.MySQL的执行模式
a>.交互式执行模式
交互式执行模式,顾名思义,你需要登录数据库界面,然后执行相应的操作。
1 [root@yinzhengjie ~]# mysql -u root -p123 2 Welcome to the MySQL monitor. Commands end with ; or \g. 3 Your MySQL connection id is 17 4 Server version: 5.5.54-log MySQL Community Server (GPL) 5 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 7 8 Oracle is a registered trademark of Oracle Corporation and/or its 9 affiliates. Other names may be trademarks of their respective 10 owners. 11 12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14 mysql> show databases; 15 +--------------------+ 16 | Database | 17 +--------------------+ 18 | information_schema | 19 | mysql | 20 | performance_schema | 21 | test | 22 +--------------------+ 23 4 rows in set (0.00 sec) 24 25 mysql> create database yinzhengjie; 26 Query OK, 1 row affected (0.01 sec) 27 28 mysql> show databases; 29 +--------------------+ 30 | Database | 31 +--------------------+ 32 | information_schema | 33 | mysql | 34 | performance_schema | 35 | test | 36 | yinzhengjie | 37 +--------------------+ 38 5 rows in set (0.00 sec) 39 40 mysql>
b>.命令行执行模式
1 [root@yinzhengjie ~]# mysql -pyinzhengjie -e "show databases;" 2 +--------------------+ 3 | Database | 4 +--------------------+ 5 | information_schema | 6 | mysql | 7 | test | 8 +--------------------+ 9 [root@yinzhengjie ~]# mysql -pyinzhengjie -e "create database yinzhengjie;" 10 [root@yinzhengjie ~]# mysql -pyinzhengjie -e "show databases;" 11 +--------------------+ 12 | Database | 13 +--------------------+ 14 | information_schema | 15 | mysql | 16 | test | 17 | yinzhengjie | 18 +--------------------+ 19 [root@yinzhengjie ~]#
c>.批处理执行模式
1 [root@yinzhengjie ~]# more yinzhengjie.sql 2 select user(); 3 create database yinzhengjie; 4 show databases; 5 [root@yinzhengjie ~]# 6 [root@yinzhengjie ~]# mysql -uroot -p123 < yinzhengjie.sql 7 user() 8 root@localhost 9 Database 10 information_schema 11 mysql 12 performance_schema 13 test 14 yinzhengjie 15 [root@yinzhengjie ~]#
四.MySQL的数据类型
1.约束(constraint)
约束就是如果你填写了违反了某种法则的数据时(比如人的年龄不能超过200岁等等。)就会拒绝写入,而这种限定就叫做约束。对于关系型数据库来讲,约束通常有以下几种:
a>.主键约束
主键约束就是当你把某个字段或者某些字段合并起来当做主键来用了,这就意味着你往里面填写数据的时候就不能出现重复了。主键还有一个要求,即主键不能为空值(NULL)。对于一张表来讲,主键只能有一个。
b>.外键约束
假如在第一张表中有一个Name字段,而在第二张表中的第一个字段也有Name字段。那么很显然所有填入第一张表中的Name字段的数据都应该是参照了第二张表中的属性(换句话说,第一张表中的Name字段的填入规则必须和第二张表中的Name字段保持一致!)。这种参照(引用)方式叫做参照完整性约束,也叫做外键约束。
c>.惟一键约束
惟一键约束和主键类似,只不过它的主键是可以为空的(NULL),并且对于一张表来讲,主键可以有多个。
d>.检查式约束
无论是主键还是惟一键都没有办法实现把某一个字段的取值限制在合理的范围内。比如,要求用户输入年龄,某个用户手一哆嗦,本来想写40,结果写成了400,那么它真的有400岁吗?很显然并不是。因此就有了检查式约束,即用户自定义有效取值范围的,它通常是一个布尔(bool)表达式,比较能符合条件就允许你往数据库填写数据,如果不能符合条件就禁止填入。
2.键
键就是选取出来某个具有特殊意义的字段。对于关系型数据库(简称RDBMS)来讲,键指的是字段,因为它是用来当做查找标准或者处理标准所使用的。
a>.主键
主键就是能够惟一标识每一个记录的字段或字段的组合。主键是从候选键中挑选出来的一种方案。是需要被真正使用的。
打个比方,要求你在数据库存放中国排名前十的大学从建立到现在的学生信息,我们能用名字来做主键吗?显然是不能的,同理,我们也不能拿年龄当唯一键,更不能拿性别当唯一键啦。甚至学号也可能出现重复的情况(比如清华大学的学生和北大的学生的学号是一样的)。因此,我们可以将学校,姓名,年龄,性别以及学号组合起来当主键使用,这样出现重复的概念就极小啦。因此键未必是一个字段。
b>.候选键
做DBA的童鞋应该知道在数据库的一张表当中,能够唯一标识每一个记录的字段的组合可能不止一个,我们还以上面的例子来说,统计全国排名前十的大学的学员信息,我们可以用学校,姓名,年龄,性别学号来做唯一主键,当然也可以只用学校,姓名和学号来做唯一主键。也就是说选择主键的方式有很多种。因此,所有这些能够用于唯一标识每一个记录的键就叫做候选键。也就是说这些候选键都是可以拿来当主键用的,候选键只是一种概念,并没有被真正使用。
c>.外键
如果公共关键字在一个关系中是主关键字,那么这个公共关键字被称为另一个关系的外键。由此可见,外键表示了两个关系之间的相关联系。以另一个关系的外键作主关键字的表被称为主表,具有此外键的表被称为主表的从表。外键又称作外关键字。
打个比方,假如在第一张表中有一个Name字段,而在第二张表中的第一个字段也有Name字段。这个时候我们就可以说第一张表中的Name字段就是一个外键。也可以说第二张表中的Name字段是第一张表的一个外键。
3.数据类型
数据类型是用来比较方式(排序方式),存储空间(取值范围),能够参与的运算等等,这些功能都是由数据类型来确定的。
a>.字符型
1>.固定长度的字符类型,换句话说就是定义了一个字符长度,如果你存储的字节长度不足时,系统会自动给你用“0”进行占位操作,当字符类型的长度超过我们预定义的长度时,就会报错。用char(#number[最多存储255个字符])关键字来定义。(也就是说,char(10),就表示你每个字符串存储的长度应该是10,比如你的单个字符串长度是7,那么还有3个空位系统自动会用“0”进行占位。也就没有字符存储的大小是一致的。)
2>.自动变化长度的字符类型,即varchar(#bumber[最多存储65535个字符,即2个字节])。和上面的char(#number)用法相似,只不过唯一不同的是存储的字符的长度是可变化的。也就是说,varchar(10),就表示你每个字符串的长度应该是10,如果你的单个字符串长度是6,那么它就会以6个长度来存取的你的数据,而不会去可以给你填满其他的占位符,它是以你实际长度为准的。注意,char(#number)和varchar(#num)存储数据的时候都不区分大小写。如果你强烈要求要区分大消息的话也是可以的,和char(#number)类似的一种方法叫做binary(#number),他们唯一不同就是char(#number)不区分大小写,而binary(#number)是区分大小写的。同理,和varchar(#number)对应的就是varbinary(#number)。
3>.其实char(#number),var(#number),binary(#number),varbinary(#number)它们的存储数据的大小是有上限的。如果你写博客,里面的正文数据长度一定是很大的。这个时候这四种定义类型就不再合适了,这个时候就有了面向对象存储的概念,就是不讲数据放在一个表中了,而是将数据放在一个指定的文件,而在表中存储的只是一个指针。当用户用到该数据时,就会去调用这个指针所对应的文件。而这种存储方式也是区分大小写了,相比之前学习的四种定义类型,test和blob数据类型存储的容量更大,test的类型拥有char(#number),binary(#number)的特点,而blob数据类型拥有varchar(#number),varbinary(#number)。而test和blob又被细分了4类,例如blob分为TinyBlob(最多存储255个字符,即一个字节),Blob(最多存储64kb,需要减去2个字节的空间),MediumBlob(最多存储16MB的空间,需要减去3个字节),LongBlob(最多存储4G的空间,需要减去4个字节)。test分为TinyText(最多存储255个字符,即一个字节与char索引方式是不同的,TinyText不能使用全字段索引而char确实可以的),Text(最多存储65535个字符,需要减去2个字节),MediumText(最多存储16777215个字符,需要减去3个字节),LongText(最多存储4294967295个字符,表示可以存储4个G,不过一般没人那么干,再问谁会将一个表中的一行字段占用4G的空间呢)等等。
4>.字符型(char、varchar和text)常用的属性修饰符:
A>.NOT NULL(非空约束)
B>.NULL(允许为空)
C>.default ‘string’(默认值,不适用于text类型)
D>.CHARACTER SET 字符集
mysql>show variables like '%char%'; #查看默认的字符集
mysql>show character set; #查看数据库支持的字符集
E>.collation '规则':排序规则
msyql>show collation; #查看数据库支持的排序规则
5>.binary,varbinaray和blob字符常用的属性修饰符
A>.NOT NULL
B>.NULL
C>.default 不适用与blob
b>.数值型
1>.精确数值型
精确数值型有两类,即整形(integer)和十进制(decimal)。在金融领域中,一般都用十进制存储数值型。而integer类型来讲,它有好几种变化形式分别用来表示所能够存储的范围大小的,比如:
A>.只占用一个字节的tinyint(微整型,取值范围是-128~127或者0~255);
B>.占用两个字节的samllint(小整型,取值范围是-32768~32767或者0~65535);
C>.占用三个字节的mediuint(中等整型,取值范围-8388608~8388607或者0~16777215);
D>.占用四个字节的int(整型,取值范围-2147483648~2147483647或者0~4294967295);
E>.占用八个字节的bingint(大整形,取值范围是-9223372036854775808~9223372036854775807或者0~18446744073709551615)等等。
整形的常用属性修饰符:
A>.AUTO_INCREMENT:自动增长(前提:非空,且唯一,支持索引,非负值[UNSIGNED],注意:TRUNCATE 用来清空表中数据)
B>.LEST_INSERT_ID():可以查看上次增长的数值,当插入多行时,只记录第一行
C>.UNSIGNED:无符号
D>.NULL
E>.NOT NULL
F>.DEFAULT
2>.近似数值型
近似数值型又分为单精度浮点型(float)和双精度浮点型(double)。
浮点型常用修饰符:(使用g,f来定义总共有多少数字和小数点后有多少数字)
A>.NULL
B>.NOT NULL
C>.UNSIGNSD
D>.DEFAULT
3>.位
按位(bit)实现数据存储的。这种方式不建议使用。
c>.日期时间型
1>我们在数据库中可能需要存储日期或时间,日期时间型其实本质上被我们存储为数值或字符。
A>.其中我们可以用占用三个字节的date(日期型)来存储时间;
B>.用占用三个字节的time(时间型)来存储时间;
C>.用占用八个字节的datetime(日期时间型)来存储日期和时间;
D>.当然,你也可以存储在四个字节的timestamp(时间戳,存储自1970年1月1号0点0分0秒至你所指定的时间为止所经过的秒数。);
E>.对了还有一个字节来记录年的year类型。即Year(2)和year(4),前者表示方法是:“00~99”,默认值是:“00”;后者表示方法是:“1901~2155”,默认为“0000”。
2>.日期时间型常用的修饰符
A>.NULL
B>.NOT NULL
C>.DEFAULT
d>.布尔型
MySQL其实没有真正意义上的布尔型,而是使用的tinyint(微整型),而且只显示一位来表示的,要么是0,要么是1.
e>NULL
表示什么也没有存,注意空白字符不等于空哟,数字0也不代表空。
f>.内置类型(事实上ENUM和SET也是属于字符型哟)
1>.MySQL也有两种常见的内置类型,即ENUM(枚举)和SET(集合)。
2>.枚举类型是指将所有的情况的列举出来,我们从中挑选处一种即可。比如今天是星期几,我们通过枚举方法列出所有的可能性,将星期一至星期日的其中情况都枚举出来,这样用户输入的星期八则是不合法的。集合类型是指在一个范围内有很多元素,你可以把这些元素任意拼凑,怎么拼都行,但是每个元素必须是在给定范围内的,不能超出限定范围。就好比你是某学校的大一新生的班主任,现在你要对你的学生进行分组处理。其中每个组的每一个成员必须都是你的班级的学生,而不能讲其他班级的学生拉倒你的班级进行分组。
3>.ENUM和SET的修饰符
A>.NULL
B>.NOT NULL
C>.DEFAULT ‘’
4.表(table)
由行和列组成的二维关系,这也是他被称为关系型数据库类型的原因。
a>.表的创建(CREATE)
用法格式:“create table TabelName(VariableName VariableType,......);”
1 [root@yinzhengjie ~]# mysql -uroot -pyinzhengjie 2 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 3 [root@yinzhengjie ~]# 4 [root@yinzhengjie ~]# mysql -uroot -p123 5 Welcome to the MySQL monitor. Commands end with ; or \g. 6 Your MySQL connection id is 39 7 Server version: 5.5.54-log MySQL Community Server (GPL) 8 9 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 10 11 Oracle is a registered trademark of Oracle Corporation and/or its 12 affiliates. Other names may be trademarks of their respective 13 owners. 14 15 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 16 17 mysql> show databases; 18 +--------------------+ 19 | Database | 20 +--------------------+ 21 | information_schema | 22 | mysql | 23 | performance_schema | 24 | test | 25 | yinzhengjie | 26 +--------------------+ 27 5 rows in set (0.01 sec) 28 29 mysql> use yinzhengjie 30 Database changed 31 mysql> create table Student (Name varchar(30),Age tinyint,Gender ENUM('boy','girl')); ----->这里是创建一个表,该表有3个字段,分别是姓名(Name),年龄(Age)和性别(Gender),定义变量名称和Golang很相似。 32 Query OK, 0 rows affected (0.01 sec) 33 34 mysql>
1 [root@yinzhengjie ~]# mysql -uroot -p123 2 Welcome to the MySQL monitor. Commands end with ; or \g. 3 Your MySQL connection id is 42 4 Server version: 5.5.54-log MySQL Community Server (GPL) 5 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 7 8 Oracle is a registered trademark of Oracle Corporation and/or its 9 affiliates. Other names may be trademarks of their respective 10 owners. 11 12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14 mysql> show databases; 15 +--------------------+ 16 | Database | 17 +--------------------+ 18 | information_schema | 19 | mysql | 20 | performance_schema | 21 | test | 22 | yinzhengjie | 23 +--------------------+ 24 5 rows in set (0.00 sec) 25 26 mysql> use yinzhengjie 27 Database changed 28 mysql> show tables; 29 +-----------------------+ 30 | Tables_in_yinzhengjie | 31 +-----------------------+ 32 | Student | 33 +-----------------------+ 34 1 row in set (0.00 sec) 35 36 mysql> desc Student; 37 +--------+--------------------+------+-----+---------+-------+ 38 | Field | Type | Null | Key | Default | Extra | 39 +--------+--------------------+------+-----+---------+-------+ 40 | Name | varchar(30) | YES | | NULL | | 41 | Age | tinyint(4) | YES | | NULL | | 42 | Gender | enum('boy','girl') | YES | | NULL | | 43 +--------+--------------------+------+-----+---------+-------+ 44 3 rows in set (0.00 sec) 45 46 mysql>
1 [root@yinzhengjie ~]# mysql -uroot -p123 2 Welcome to the MySQL monitor. Commands end with ; or \g. 3 Your MySQL connection id is 44 4 Server version: 5.5.54-log MySQL Community Server (GPL) 5 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 7 8 Oracle is a registered trademark of Oracle Corporation and/or its 9 affiliates. Other names may be trademarks of their respective 10 owners. 11 12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14 mysql> show databases; 15 +--------------------+ 16 | Database | 17 +--------------------+ 18 | information_schema | 19 | mysql | 20 | performance_schema | 21 | test | 22 | yinzhengjie | 23 +--------------------+ 24 5 rows in set (0.00 sec) 25 26 mysql> use yinzhengjie 27 Database changed 28 mysql> show tables; 29 +-----------------------+ 30 | Tables_in_yinzhengjie | 31 +-----------------------+ 32 | Student | 33 +-----------------------+ 34 1 row in set (0.00 sec) 35 36 mysql> drop table Student; 37 Query OK, 0 rows affected (0.00 sec) 38 39 mysql> show tables; 40 Empty set (0.00 sec) 41 42 mysql>
1 注意,字段或字段类型还可以有修饰符
2
3 1>.NOT NULL #表示插入数据时不允许为空
4 2>.NULL #表示插入的数据可以为空
5 3>.DEDAULT #字符要加引号,数字不能加引号
6 4>.UNSIGNED #无符号(只能用在整型上)
7 5>.AUTO_INCREMENT #自动增长类型的字段必须为主键或唯一键,即使你清空了一个表格的数据,它默认情况下是不会重置的哟。
8 6>.PRIMARY KEY #定义主键
9 7>.UNIQUE KEY #唯一键
1 [root@yinzhengjie ~]# mysql -uroot -p123 2 Welcome to the MySQL monitor. Commands end with ; or \g. 3 Your MySQL connection id is 45 4 Server version: 5.5.54-log MySQL Community Server (GPL) 5 6 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. 7 8 Oracle is a registered trademark of Oracle Corporation and/or its 9 affiliates. Other names may be trademarks of their respective 10 owners. 11 12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 14 mysql> use yinzhengjie 15 Database changed 16 mysql> create table Student (StuID int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,Name varchar(30) NOT NULL,Age tinyint UNSIGNED NOT NULL,Gender ENUM('boy','girl') NOT NULL DEFAULT 'boy'); ----->和之前创建的表名称一样,只不过在每个变量类型后面加了修饰符,来限定变量是否可用为空,并且新添加了一个INT类型的StuID字段,并且让该INT类型自动增长。 17 Query OK, 0 rows affected (0.01 sec) 18 19 mysql> 20 mysql> desc Student; 21 +--------+---------------------+------+-----+---------+----------------+ 22 | Field | Type | Null | Key | Default | Extra | 23 +--------+---------------------+------+-----+---------+----------------+ 24 | StuID | int(10) unsigned | NO | PRI | NULL | auto_increment | 25 | Name | varchar(30) | NO | | NULL | | 26 | Age | tinyint(3) unsigned | NO | | NULL | | 27 | Gender | enum('boy','girl') | NO | | boy | | 28 +--------+---------------------+------+-----+---------+----------------+ 29 4 rows in set (0.00 sec) 30 31 mysql>
b>.表的插入(INSERT)
用法格式:“INSERT INTO TableName(VariableName1,VariableName2,...) VALUE|VALUES (VariableValue1,VariableValue2.....);”
1 mysql> desc Student; 2 +--------+---------------------+------+-----+---------+----------------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +--------+---------------------+------+-----+---------+----------------+ 5 | StuID | int(10) unsigned | NO | PRI | NULL | auto_increment | 6 | Name | varchar(30) | NO | | NULL | | 7 | Age | tinyint(3) unsigned | NO | | NULL | | 8 | Gender | enum('boy','girl') | NO | | boy | | 9 +--------+---------------------+------+-----+---------+----------------+ 10 4 rows in set (0.00 sec) 11 12 mysql> insert into Student(Name,Age,Gender) values ( "yinzihengjie",17,'boy'); 13 Query OK, 1 row affected (0.01 sec) 14 mysql> select * from Student; 15 +-------+--------------+-----+--------+ 16 | StuID | Name | Age | Gender | 17 +-------+--------------+-----+--------+ 18 | 1 | yinzihengjie | 17 | boy | 19 +-------+--------------+-----+--------+ 20 1 row in set (0.00 sec) 21 22 mysql>
1 mysql> select * from Student; 2 +-------+--------------+-----+--------+ 3 | StuID | Name | Age | Gender | 4 +-------+--------------+-----+--------+ 5 | 1 | yinzihengjie | 17 | boy | 6 +-------+--------------+-----+--------+ 7 1 row in set (0.00 sec) 8 9 mysql> insert into Student(Name,Age,Gender) values ( "linus",48,'boy'),("dengziqi",26,'girl'); 10 Query OK, 2 rows affected (0.02 sec) 11 Records: 2 Duplicates: 0 Warnings: 0 12 13 mysql> select * from Student; 14 +-------+--------------+-----+--------+ 15 | StuID | Name | Age | Gender | 16 +-------+--------------+-----+--------+ 17 | 1 | yinzihengjie | 17 | boy | 18 | 2 | linus | 48 | boy | 19 | 3 | dengziqi | 26 | girl | 20 +-------+--------------+-----+--------+ 21 3 rows in set (0.00 sec) 22 23 mysql>
1 mysql> select * from Student; 2 +-------+--------------+-----+--------+ 3 | StuID | Name | Age | Gender | 4 +-------+--------------+-----+--------+ 5 | 1 | yinzihengjie | 17 | boy | 6 | 2 | linus | 48 | boy | 7 | 3 | dengziqi | 26 | girl | 8 +-------+--------------+-----+--------+ 9 3 rows in set (0.00 sec) 10 11 mysql> insert into Student(Name,Age) values ( "jay Chou",38),("joker Xue",34),("zhangweijian",52); 12 Query OK, 3 rows affected (0.00 sec) 13 Records: 3 Duplicates: 0 Warnings: 0 14 15 mysql> select * from Student; 16 +-------+--------------+-----+--------+ 17 | StuID | Name | Age | Gender | 18 +-------+--------------+-----+--------+ 19 | 1 | yinzihengjie | 17 | boy | 20 | 2 | linus | 48 | boy | 21 | 3 | dengziqi | 26 | girl | 22 | 4 | jay Chou | 38 | boy | 23 | 5 | joker Xue | 34 | boy | 24 | 6 | zhangweijian | 52 | boy | 25 +-------+--------------+-----+--------+ 26 6 rows in set (0.00 sec) 27 28 mysql>
c>.表的查询
1 用法格式:“select VariableName1,VariableName2 from TableName where ConditionalExpression;”
2
3 当然查询是可以用,删除,修改都是可以用like,rlike以及组合条件进行过滤,从而得到我们想要的字段。
4
5
6 like通配符:
7 %:任意长度的任意字符
8 _:匹配任意单个字符
9 rlink
10 正则表达式
11
12
13 组合条件
14 and
15 or
16 not
1 mysql> select * from Student; 2 +-------+--------------+-----+--------+ 3 | StuID | Name | Age | Gender | 4 +-------+--------------+-----+--------+ 5 | 1 | yinzihengjie | 17 | boy | 6 | 2 | linus | 48 | boy | 7 | 3 | dengziqi | 26 | girl | 8 | 4 | jay Chou | 38 | boy | 9 | 5 | joker Xue | 34 | boy | 10 | 6 | zhangweijian | 52 | boy | 11 +-------+--------------+-----+--------+ 12 6 rows in set (0.00 sec) 13 mysql> select Name,Age,Gender from Student where Name = 'yinzihengjie'; 14 +--------------+-----+--------+ 15 | Name | Age | Gender | 16 +--------------+-----+--------+ 17 | yinzihengjie | 17 | boy | 18 +--------------+-----+--------+ 19 1 row in set (0.00 sec) 20 21 mysql>
1 mysql> select * from Student; 2 +-------+--------------+-----+--------+ 3 | StuID | Name | Age | Gender | 4 +-------+--------------+-----+--------+ 5 | 1 | yinzihengjie | 17 | boy | 6 | 2 | linus | 48 | boy | 7 | 3 | dengziqi | 26 | girl | 8 | 4 | jay Chou | 38 | boy | 9 | 5 | joker Xue | 34 | boy | 10 | 6 | zhangweijian | 52 | boy | 11 +-------+--------------+-----+--------+ 12 6 rows in set (0.00 sec) 13 14 mysql> select Name,Age,Gender from Student where Gender = 'boy' and Age > 35; 15 +--------------+-----+--------+ 16 | Name | Age | Gender | 17 +--------------+-----+--------+ 18 | linus | 48 | boy | 19 | jay Chou | 38 | boy | 20 | zhangweijian | 52 | boy | 21 +--------------+-----+--------+ 22 3 rows in set (0.00 sec) 23 24 mysql>
1 mysql> show create table Student; 2 +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 3 | Table | Create Table | 4 +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 5 | Student | CREATE TABLE `Student` ( 6 `StuID` int(10) unsigned NOT NULL AUTO_INCREMENT, 7 `Name` varchar(30) NOT NULL, 8 `Age` tinyint(3) unsigned NOT NULL, 9 `Gender` enum('boy','girl') NOT NULL DEFAULT 'boy', 10 PRIMARY KEY (`StuID`) 11 ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 | 12 +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 13 1 row in set (0.00 sec) 14 15 mysql>
1 mysql> show engines; 2 +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 3 | Engine | Support | Comment | Transactions | XA | Savepoints | 4 +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 5 | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | 6 | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | 7 | CSV | YES | CSV storage engine | NO | NO | NO | 8 | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | 9 | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | 10 | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | 11 | ARCHIVE | YES | Archive storage engine | NO | NO | NO | 12 | MyISAM | YES | MyISAM storage engine | NO | NO | NO | 13 | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | 14 +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 15 9 rows in set (0.00 sec) 16 17 mysql>
1 mysql> show table status\G 2 *************************** 1. row *************************** 3 Name: Student 4 Engine: InnoDB 5 Version: 10 6 Row_format: Compact 7 Rows: 4 8 Avg_row_length: 4096 9 Data_length: 16384 10 Max_data_length: 0 11 Index_length: 0 12 Data_free: 10485760 13 Auto_increment: 7 14 Create_time: 2017-11-11 08:14:12 15 Update_time: NULL 16 Check_time: NULL 17 Collation: latin1_swedish_ci 18 Checksum: NULL 19 Create_options: 20 Comment: 21 1 row in set (0.00 sec) 22 23 mysql>
1 mysql> show character set; 2 +----------+-----------------------------+---------------------+--------+ 3 | Charset | Description | Default collation | Maxlen | 4 +----------+-----------------------------+---------------------+--------+ 5 | big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 | 6 | dec8 | DEC West European | dec8_swedish_ci | 1 | 7 | cp850 | DOS West European | cp850_general_ci | 1 | 8 | hp8 | HP West European | hp8_english_ci | 1 | 9 | koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 | 10 | latin1 | cp1252 West European | latin1_swedish_ci | 1 | 11 | latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 | 12 | swe7 | 7bit Swedish | swe7_swedish_ci | 1 | 13 | ascii | US ASCII | ascii_general_ci | 1 | 14 | ujis | EUC-JP Japanese | ujis_japanese_ci | 3 | 15 | sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 | 16 | hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 | 17 | tis620 | TIS620 Thai | tis620_thai_ci | 1 | 18 | euckr | EUC-KR Korean | euckr_korean_ci | 2 | 19 | koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 | 20 | gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 | 21 | greek | ISO 8859-7 Greek | greek_general_ci | 1 | 22 | cp1250 | Windows Central European | cp1250_general_ci | 1 | 23 | gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 | 24 | latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 | 25 | armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 | 26 | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | 27 | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | 28 | cp866 | DOS Russian | cp866_general_ci | 1 | 29 | keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 | 30 | macce | Mac Central European | macce_general_ci | 1 | 31 | macroman | Mac West European | macroman_general_ci | 1 | 32 | cp852 | DOS Central European | cp852_general_ci | 1 | 33 | latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 | 34 | utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 | 35 | cp1251 | Windows Cyrillic | cp1251_general_ci | 1 | 36 | utf16 | UTF-16 Unicode | utf16_general_ci | 4 | 37 | cp1256 | Windows Arabic | cp1256_general_ci | 1 | 38 | cp1257 | Windows Baltic | cp1257_general_ci | 1 | 39 | utf32 | UTF-32 Unicode | utf32_general_ci | 4 | 40 | binary | Binary pseudo charset | binary | 1 | 41 | geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 | 42 | cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 | 43 | eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 | 44 +----------+-----------------------------+---------------------+--------+ 45 39 rows in set (0.03 sec) 46 47 mysql>
1 mysql> show collation; 2 +--------------------------+----------+-----+---------+----------+---------+ 3 | Collation | Charset | Id | Default | Compiled | Sortlen | 4 +--------------------------+----------+-----+---------+----------+---------+ 5 | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | 6 | big5_bin | big5 | 84 | | Yes | 1 | 7 | dec8_swedish_ci | dec8 | 3 | Yes | Yes | 1 | 8 | dec8_bin | dec8 | 69 | | Yes | 1 | 9 | cp850_general_ci | cp850 | 4 | Yes | Yes | 1 | 10 | cp850_bin | cp850 | 80 | | Yes | 1 | 11 | hp8_english_ci | hp8 | 6 | Yes | Yes | 1 | 12 | hp8_bin | hp8 | 72 | | Yes | 1 | 13 | koi8r_general_ci | koi8r | 7 | Yes | Yes | 1 | 14 | koi8r_bin | koi8r | 74 | | Yes | 1 | 15 | latin1_german1_ci | latin1 | 5 | | Yes | 1 | 16 | latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 | 17 | latin1_danish_ci | latin1 | 15 | | Yes | 1 | 18 | latin1_german2_ci | latin1 | 31 | | Yes | 2 | 19 | latin1_bin | latin1 | 47 | | Yes | 1 | 20 | latin1_general_ci | latin1 | 48 | | Yes | 1 | 21 | latin1_general_cs | latin1 | 49 | | Yes | 1 | 22 | latin1_spanish_ci | latin1 | 94 | | Yes | 1 | 23 | latin2_czech_cs | latin2 | 2 | | Yes | 4 | 24 | latin2_general_ci | latin2 | 9 | Yes | Yes | 1 | 25 | latin2_hungarian_ci | latin2 | 21 | | Yes | 1 | 26 | latin2_croatian_ci | latin2 | 27 | | Yes | 1 | 27 | latin2_bin | latin2 | 77 | | Yes | 1 | 28 | swe7_swedish_ci | swe7 | 10 | Yes | Yes | 1 | 29 | swe7_bin | swe7 | 82 | | Yes | 1 | 30 | ascii_general_ci | ascii | 11 | Yes | Yes | 1 | 31 | ascii_bin | ascii | 65 | | Yes | 1 | 32 | ujis_japanese_ci | ujis | 12 | Yes | Yes | 1 | 33 | ujis_bin | ujis | 91 | | Yes | 1 | 34 | sjis_japanese_ci | sjis | 13 | Yes | Yes | 1 | 35 | sjis_bin | sjis | 88 | | Yes | 1 | 36 | hebrew_general_ci | hebrew | 16 | Yes | Yes | 1 | 37 | hebrew_bin | hebrew | 71 | | Yes | 1 | 38 | tis620_thai_ci | tis620 | 18 | Yes | Yes | 4 | 39 | tis620_bin | tis620 | 89 | | Yes | 1 | 40 | euckr_korean_ci | euckr | 19 | Yes | Yes | 1 | 41 | euckr_bin | euckr | 85 | | Yes | 1 | 42 | koi8u_general_ci | koi8u | 22 | Yes | Yes | 1 | 43 | koi8u_bin | koi8u | 75 | | Yes | 1 | 44 | gb2312_chinese_ci | gb2312 | 24 | Yes | Yes | 1 | 45 | gb2312_bin | gb2312 | 86 | | Yes | 1 | 46 | greek_general_ci | greek | 25 | Yes | Yes | 1 | 47 | greek_bin | greek | 70 | | Yes | 1 | 48 | cp1250_general_ci | cp1250 | 26 | Yes | Yes | 1 | 49 | cp1250_czech_cs | cp1250 | 34 | | Yes | 2 | 50 | cp1250_croatian_ci | cp1250 | 44 | | Yes | 1 | 51 | cp1250_bin | cp1250 | 66 | | Yes | 1 | 52 | cp1250_polish_ci | cp1250 | 99 | | Yes | 1 | 53 | gbk_chinese_ci | gbk | 28 | Yes | Yes | 1 | 54 | gbk_bin | gbk | 87 | | Yes | 1 | 55 | latin5_turkish_ci | latin5 | 30 | Yes | Yes | 1 | 56 | latin5_bin | latin5 | 78 | | Yes | 1 | 57 | armscii8_general_ci | armscii8 | 32 | Yes | Yes | 1 | 58 | armscii8_bin | armscii8 | 64 | | Yes | 1 | 59 | utf8_general_ci | utf8 | 33 | Yes | Yes | 1 | 60 | utf8_bin | utf8 | 83 | | Yes | 1 | 61 | utf8_unicode_ci | utf8 | 192 | | Yes | 8 | 62 | utf8_icelandic_ci | utf8 | 193 | | Yes | 8 | 63 | utf8_latvian_ci | utf8 | 194 | | Yes | 8 | 64 | utf8_romanian_ci | utf8 | 195 | | Yes | 8 | 65 | utf8_slovenian_ci | utf8 | 196 | | Yes | 8 | 66 | utf8_polish_ci | utf8 | 197 | | Yes | 8 | 67 | utf8_estonian_ci | utf8 | 198 | | Yes | 8 | 68 | utf8_spanish_ci | utf8 | 199 | | Yes | 8 | 69 | utf8_swedish_ci | utf8 | 200 | | Yes | 8 | 70 | utf8_turkish_ci | utf8 | 201 | | Yes | 8 | 71 | utf8_czech_ci | utf8 | 202 | | Yes | 8 | 72 | utf8_danish_ci | utf8 | 203 | | Yes | 8 | 73 | utf8_lithuanian_ci | utf8 | 204 | | Yes | 8 | 74 | utf8_slovak_ci | utf8 | 205 | | Yes | 8 | 75 | utf8_spanish2_ci | utf8 | 206 | | Yes | 8 | 76 | utf8_roman_ci | utf8 | 207 | | Yes | 8 | 77 | utf8_persian_ci | utf8 | 208 | | Yes | 8 | 78 | utf8_esperanto_ci | utf8 | 209 | | Yes | 8 | 79 | utf8_hungarian_ci | utf8 | 210 | | Yes | 8 | 80 | utf8_sinhala_ci | utf8 | 211 | | Yes | 8 | 81 | utf8_general_mysql500_ci | utf8 | 223 | | Yes | 1 | 82 | ucs2_general_ci | ucs2 | 35 | Yes | Yes | 1 | 83 | ucs2_bin | ucs2 | 90 | | Yes | 1 | 84 | ucs2_unicode_ci | ucs2 | 128 | | Yes | 8 | 85 | ucs2_icelandic_ci | ucs2 | 129 | | Yes | 8 | 86 | ucs2_latvian_ci | ucs2 | 130 | | Yes | 8 | 87 | ucs2_romanian_ci | ucs2 | 131 | | Yes | 8 | 88 | ucs2_slovenian_ci | ucs2 | 132 | | Yes | 8 | 89 | ucs2_polish_ci | ucs2 | 133 | | Yes | 8 | 90 | ucs2_estonian_ci | ucs2 | 134 | | Yes | 8 | 91 | ucs2_spanish_ci | ucs2 | 135 | | Yes | 8 | 92 | ucs2_swedish_ci | ucs2 | 136 | | Yes | 8 | 93 | ucs2_turkish_ci | ucs2 | 137 | | Yes | 8 | 94 | ucs2_czech_ci | ucs2 | 138 | | Yes | 8 | 95 | ucs2_danish_ci | ucs2 | 139 | | Yes | 8 | 96 | ucs2_lithuanian_ci | ucs2 | 140 | | Yes | 8 | 97 | ucs2_slovak_ci | ucs2 | 141 | | Yes | 8 | 98 | ucs2_spanish2_ci | ucs2 | 142 | | Yes | 8 | 99 | ucs2_roman_ci | ucs2 | 143 | | Yes | 8 | 100 | ucs2_persian_ci | ucs2 | 144 | | Yes | 8 | 101 | ucs2_esperanto_ci | ucs2 | 145 | | Yes | 8 | 102 | ucs2_hungarian_ci | ucs2 | 146 | | Yes | 8 | 103 | ucs2_sinhala_ci | ucs2 | 147 | | Yes | 8 | 104 | ucs2_general_mysql500_ci | ucs2 | 159 | | Yes | 1 | 105 | cp866_general_ci | cp866 | 36 | Yes | Yes | 1 | 106 | cp866_bin | cp866 | 68 | | Yes | 1 | 107 | keybcs2_general_ci | keybcs2 | 37 | Yes | Yes | 1 | 108 | keybcs2_bin | keybcs2 | 73 | | Yes | 1 | 109 | macce_general_ci | macce | 38 | Yes | Yes | 1 | 110 | macce_bin | macce | 43 | | Yes | 1 | 111 | macroman_general_ci | macroman | 39 | Yes | Yes | 1 | 112 | macroman_bin | macroman | 53 | | Yes | 1 | 113 | cp852_general_ci | cp852 | 40 | Yes | Yes | 1 | 114 | cp852_bin | cp852 | 81 | | Yes | 1 | 115 | latin7_estonian_cs | latin7 | 20 | | Yes | 1 | 116 | latin7_general_ci | latin7 | 41 | Yes | Yes | 1 | 117 | latin7_general_cs | latin7 | 42 | | Yes | 1 | 118 | latin7_bin | latin7 | 79 | | Yes | 1 | 119 | utf8mb4_general_ci | utf8mb4 | 45 | Yes | Yes | 1 | 120 | utf8mb4_bin | utf8mb4 | 46 | | Yes | 1 | 121 | utf8mb4_unicode_ci | utf8mb4 | 224 | | Yes | 8 | 122 | utf8mb4_icelandic_ci | utf8mb4 | 225 | | Yes | 8 | 123 | utf8mb4_latvian_ci | utf8mb4 | 226 | | Yes | 8 | 124 | utf8mb4_romanian_ci | utf8mb4 | 227 | | Yes | 8 | 125 | utf8mb4_slovenian_ci | utf8mb4 | 228 | | Yes | 8 | 126 | utf8mb4_polish_ci | utf8mb4 | 229 | | Yes | 8 | 127 | utf8mb4_estonian_ci | utf8mb4 | 230 | | Yes | 8 | 128 | utf8mb4_spanish_ci | utf8mb4 | 231 | | Yes | 8 | 129 | utf8mb4_swedish_ci | utf8mb4 | 232 | | Yes | 8 | 130 | utf8mb4_turkish_ci | utf8mb4 | 233 | | Yes | 8 | 131 | utf8mb4_czech_ci | utf8mb4 | 234 | | Yes | 8 | 132 | utf8mb4_danish_ci | utf8mb4 | 235 | | Yes | 8 | 133 | utf8mb4_lithuanian_ci | utf8mb4 | 236 | | Yes | 8 | 134 | utf8mb4_slovak_ci | utf8mb4 | 237 | | Yes | 8 | 135 | utf8mb4_spanish2_ci | utf8mb4 | 238 | | Yes | 8 | 136 | utf8mb4_roman_ci | utf8mb4 | 239 | | Yes | 8 | 137 | utf8mb4_persian_ci | utf8mb4 | 240 | | Yes | 8 | 138 | utf8mb4_esperanto_ci | utf8mb4 | 241 | | Yes | 8 | 139 | utf8mb4_hungarian_ci | utf8mb4 | 242 | | Yes | 8 | 140 | utf8mb4_sinhala_ci | utf8mb4 | 243 | | Yes | 8 | 141 | cp1251_bulgarian_ci | cp1251 | 14 | | Yes | 1 | 142 | cp1251_ukrainian_ci | cp1251 | 23 | | Yes | 1 | 143 | cp1251_bin | cp1251 | 50 | | Yes | 1 | 144 | cp1251_general_ci | cp1251 | 51 | Yes | Yes | 1 | 145 | cp1251_general_cs | cp1251 | 52 | | Yes | 1 | 146 | utf16_general_ci | utf16 | 54 | Yes | Yes | 1 | 147 | utf16_bin | utf16 | 55 | | Yes | 1 | 148 | utf16_unicode_ci | utf16 | 101 | | Yes | 8 | 149 | utf16_icelandic_ci | utf16 | 102 | | Yes | 8 | 150 | utf16_latvian_ci | utf16 | 103 | | Yes | 8 | 151 | utf16_romanian_ci | utf16 | 104 | | Yes | 8 | 152 | utf16_slovenian_ci | utf16 | 105 | | Yes | 8 | 153 | utf16_polish_ci | utf16 | 106 | | Yes | 8 | 154 | utf16_estonian_ci | utf16 | 107 | | Yes | 8 | 155 | utf16_spanish_ci | utf16 | 108 | | Yes | 8 | 156 | utf16_swedish_ci | utf16 | 109 | | Yes | 8 | 157 | utf16_turkish_ci | utf16 | 110 | | Yes | 8 | 158 | utf16_czech_ci | utf16 | 111 | | Yes | 8 | 159 | utf16_danish_ci | utf16 | 112 | | Yes | 8 | 160 | utf16_lithuanian_ci | utf16 | 113 | | Yes | 8 | 161 | utf16_slovak_ci | utf16 | 114 | | Yes | 8 | 162 | utf16_spanish2_ci | utf16 | 115 | | Yes | 8 | 163 | utf16_roman_ci | utf16 | 116 | | Yes | 8 | 164 | utf16_persian_ci | utf16 | 117 | | Yes | 8 | 165 | utf16_esperanto_ci | utf16 | 118 | | Yes | 8 | 166 | utf16_hungarian_ci | utf16 | 119 | | Yes | 8 | 167 | utf16_sinhala_ci | utf16 | 120 | | Yes | 8 | 168 | cp1256_general_ci | cp1256 | 57 | Yes | Yes | 1 | 169 | cp1256_bin | cp1256 | 67 | | Yes | 1 | 170 | cp1257_lithuanian_ci | cp1257 | 29 | | Yes | 1 | 171 | cp1257_bin | cp1257 | 58 | | Yes | 1 | 172 | cp1257_general_ci | cp1257 | 59 | Yes | Yes | 1 | 173 | utf32_general_ci | utf32 | 60 | Yes | Yes | 1 | 174 | utf32_bin | utf32 | 61 | | Yes | 1 | 175 | utf32_unicode_ci | utf32 | 160 | | Yes | 8 | 176 | utf32_icelandic_ci | utf32 | 161 | | Yes | 8 | 177 | utf32_latvian_ci | utf32 | 162 | | Yes | 8 | 178 | utf32_romanian_ci | utf32 | 163 | | Yes | 8 | 179 | utf32_slovenian_ci | utf32 | 164 | | Yes | 8 | 180 | utf32_polish_ci | utf32 | 165 | | Yes | 8 | 181 | utf32_estonian_ci | utf32 | 166 | | Yes | 8 | 182 | utf32_spanish_ci | utf32 | 167 | | Yes | 8 | 183 | utf32_swedish_ci | utf32 | 168 | | Yes | 8 | 184 | utf32_turkish_ci | utf32 | 169 | | Yes | 8 | 185 | utf32_czech_ci | utf32 | 170 | | Yes | 8 | 186 | utf32_danish_ci | utf32 | 171 | | Yes | 8 | 187 | utf32_lithuanian_ci | utf32 | 172 | | Yes | 8 | 188 | utf32_slovak_ci | utf32 | 173 | | Yes | 8 | 189 | utf32_spanish2_ci | utf32 | 174 | | Yes | 8 | 190 | utf32_roman_ci | utf32 | 175 | | Yes | 8 | 191 | utf32_persian_ci | utf32 | 176 | | Yes | 8 | 192 | utf32_esperanto_ci | utf32 | 177 | | Yes | 8 | 193 | utf32_hungarian_ci | utf32 | 178 | | Yes | 8 | 194 | utf32_sinhala_ci | utf32 | 179 | | Yes | 8 | 195 | binary | binary | 63 | Yes | Yes | 1 | 196 | geostd8_general_ci | geostd8 | 92 | Yes | Yes | 1 | 197 | geostd8_bin | geostd8 | 93 | | Yes | 1 | 198 | cp932_japanese_ci | cp932 | 95 | Yes | Yes | 1 | 199 | cp932_bin | cp932 | 96 | | Yes | 1 | 200 | eucjpms_japanese_ci | eucjpms | 97 | Yes | Yes | 1 | 201 | eucjpms_bin | eucjpms | 98 | | Yes | 1 | 202 +--------------------------+----------+-----+---------+----------+---------+ 203 197 rows in set (0.00 sec) 204 205 mysql>
1 mysql> show variables like 'data%'; 2 +---------------+---------------+ 3 | Variable_name | Value | 4 +---------------+---------------+ 5 | datadir | /data/mydata/ | 6 +---------------+---------------+ 7 1 row in set (0.00 sec) 8 9 mysql>
1 mysql> show global variables like 'version'; 2 +---------------+------------+ 3 | Variable_name | Value | 4 +---------------+------------+ 5 | version | 5.5.54-log | 6 +---------------+------------+ 7 1 row in set (0.00 sec) 8 9 mysql>
1 mysql> show session variables like 'tmp_table_size'; 2 +----------------+----------+ 3 | Variable_name | Value | 4 +----------------+----------+ 5 | tmp_table_size | 16777216 | 6 +----------------+----------+ 7 1 row in set (0.00 sec) 8 9 mysql>
1 mysql> show status like 'Com_select'; 2 +---------------+-------+ 3 | Variable_name | Value | 4 +---------------+-------+ 5 | Com_select | 4 | 6 +---------------+-------+ 7 1 row in set (0.00 sec) 8 9 mysql>
d>.删除数据(delete)
用法格式:“ delete from TableName where ConditionalExpression;”
1 mysql> select * from Student; 2 +-------+--------------+-----+--------+ 3 | StuID | Name | Age | Gender | 4 +-------+--------------+-----+--------+ 5 | 1 | yinzihengjie | 17 | boy | 6 | 2 | linus | 48 | boy | 7 | 3 | dengziqi | 26 | girl | 8 | 4 | jay Chou | 38 | boy | 9 | 5 | joker Xue | 34 | boy | 10 | 6 | zhangweijian | 52 | boy | 11 +-------+--------------+-----+--------+ 12 6 rows in set (0.00 sec) 13 14 mysql> delete from Student where name like 'z%'; 15 Query OK, 1 row affected (0.00 sec) 16 17 mysql> select * from Student; 18 +-------+--------------+-----+--------+ 19 | StuID | Name | Age | Gender | 20 +-------+--------------+-----+--------+ 21 | 1 | yinzihengjie | 17 | boy | 22 | 2 | linus | 48 | boy | 23 | 3 | dengziqi | 26 | girl | 24 | 4 | jay Chou | 38 | boy | 25 | 5 | joker Xue | 34 | boy | 26 +-------+--------------+-----+--------+ 27 5 rows in set (0.00 sec) 28 29 mysql>
1 mysql> select * from Student; 2 +-------+--------------+-----+--------+ 3 | StuID | Name | Age | Gender | 4 +-------+--------------+-----+--------+ 5 | 1 | yinzihengjie | 17 | boy | 6 | 2 | linus | 48 | boy | 7 | 3 | dengziqi | 26 | girl | 8 | 4 | jay Chou | 38 | boy | 9 | 5 | joker Xue | 34 | boy | 10 +-------+--------------+-----+--------+ 11 5 rows in set (0.00 sec) 12 13 mysql> delete from Student where Name rlike '^l.*'; 14 Query OK, 1 row affected (0.00 sec) 15 16 mysql> select * from Student; 17 +-------+--------------+-----+--------+ 18 | StuID | Name | Age | Gender | 19 +-------+--------------+-----+--------+ 20 | 1 | yinzihengjie | 17 | boy | 21 | 3 | dengziqi | 26 | girl | 22 | 4 | jay Chou | 38 | boy | 23 | 5 | joker Xue | 34 | boy | 24 +-------+--------------+-----+--------+ 25 4 rows in set (0.00 sec) 26 27 mysql>
e>.更新数据(update)
用法格式:“update TableName set VariableType = VariableValue where ConditionalExpression;”
1 mysql> select * from Student; 2 +-------+--------------+-----+--------+ 3 | StuID | Name | Age | Gender | 4 +-------+--------------+-----+--------+ 5 | 1 | yinzihengjie | 17 | boy | 6 | 3 | dengziqi | 26 | girl | 7 | 4 | jay Chou | 38 | boy | 8 | 5 | joker Xue | 34 | boy | 9 +-------+--------------+-----+--------+ 10 4 rows in set (0.00 sec) 11 12 mysql> update Student set Age=20 where Name = 'yinzihengjie'; 13 Query OK, 1 row affected (0.04 sec) 14 Rows matched: 1 Changed: 1 Warnings: 0 15 16 mysql> select * from Student; 17 +-------+--------------+-----+--------+ 18 | StuID | Name | Age | Gender | 19 +-------+--------------+-----+--------+ 20 | 1 | yinzihengjie | 20 | boy | 21 | 3 | dengziqi | 26 | girl | 22 | 4 | jay Chou | 38 | boy | 23 | 5 | joker Xue | 34 | boy | 24 +-------+--------------+-----+--------+ 25 4 rows in set (0.00 sec) 26 27 mysql>
五.MySQL内置的SQL模型(SQL_MODE)
1.MySQL内置的SQL模型常用的模式
MySQL有内置的SQL模型SQL_MODE:用来定义字符超出的操作和模仿别的数据库的类型,通过修改全局变量。 常用的模式有:
A>.TRADITIONAL 使用传统模式
B>.STRICT_TRANS_TABLES 仅对支持事务的表的严格模式
C>.STRICT_ALL_TABLES 对所有表使用严格模式
2.设定服务器变量的值:通常仅用于支持动态的变量
A>.支持修改的服务器变量
动态变量:可以在MySQL运行时修改
静态变量:与配置文件中修改其值,并重启后方能生效
B>.服务器变量从其生效范围来讲,分两类
全局变量:服务器级别,修改之后仅对新建立的会话生效
回话变量:会话级别,仅对当前会话有效
会话建立时,从全局继承各变量
C>.查看服务器变量
mysql> show [{global|session}] variables [like ''];
mysql> select @@{globa|session}.variable_name
mysql> mysql> select * from information_schema.GLOBAL_VARIABLES where VARIABLE_NAME='SOME_VARIABLE_NAME'; 查看全局变量
mysql> mysql> select * from information_schema.SESSION_VARIABLES where VARIABLE_NAME='SOME_VARIABLE_NAME'; 查看会话变量
D>.修改变量
前提:默认仅管理员有权限修改全局变量
mysql> set {global|session} variable_name='value';
注意:无论是全局还是会话级别的动态变量修改,在重启mysql后都会失效,想永久有效,可以定义在配置文件中的响应段中[mysqld]