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(指定登录数据库的密码)”
二.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: [email protected] 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>
stsauts #查询运行状态
1 [[email protected] ~]# 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 [[email protected] ~]#
\q #退出当前数据库
1 [[email protected] ~]# 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>
\? #获取帮助信息,相当于help指令。
1 [[email protected] ~]# 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 | [email protected] | 20 +----------------+ 21 1 row in set (0.00 sec) 22 23 mysql>
\g #直接将命令送到服务器端执行,这种情况适用于你不知道当前数据库的命令结束符是什么,换句话说,默认的命令结束符“;”被修改的情况下可以使用它
1 [[email protected] ~]# 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>
\G #不再显示为表格显示,而是以列的方式显示,这种情况适合在默认的表格显示不下时(也就是说,字段量非常大时),这个参数就很有用哟。
1 [[email protected] ~]# 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>
\c #取消命令的执行,它很适合当你不想继续执行该行代码时使用,相当于Linux系统的ctrl+c哟。
1 [[email protected] ~]# 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>
\u #使用默认的数据库,你可以理解称切换数据库目录,相当于Linux的cd命令哟
2.服务器端命令
服务器端命令简直就时多了去了,比客户端的也复杂的多,因此我在这里也不打算一一举例,而是用一些常用的命令进行扫盲模式。执行服务端的命令需要语句终止符,通常默认为分号(;),当然这个命令结束符(;)是可以被修改的。
a>.SELECT命令的基本使用
MySQL服务器端有很多内建函数( 简称BIF)。使用select命令去执行一个内建函数,并将该内建函数的执行结果返回给当前用户。
1 mysql> select user(); #查看数据库的用户信息。 2 +----------------+ 3 | user() | 4 +----------------+ 5 | [email protected] | 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>
select #执行内建函数案例
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>
select #可用于算术运算的案例
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>
SHOW DATABASES #显示自己有权限可以查看所有库
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>
SHOW TABLES #显示某库的所有表
c>.CRETARE命令的基本使用
1 [[email protected] ~]# 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>
CRETARE DATABASE database_name #创建数据库,相当于Linux的mkdir命令哟~因此,你可以去你的数据库目录去创建目录,不过要注意的是,你创建的目录权限要进行修改哟,如果你用root创建的目录,在数据库中mysql用户可能无权操作该目录哟
d>.DROP命令的基本使用
1 [[email protected] ~]# 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>
DROP DATABASE database_name; 删除数据库
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>
mysql> help KEYWORD 获取帮助信息案例展示
4.MySQL的执行模式
a>.交互式执行模式
交互式执行模式,顾名思义,你需要登录数据库界面,然后执行相应的操作。
1 [[email protected] ~]# 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>
MySQL交互式执行模式案例
b>.命令行执行模式
MySQL令行执行模式案例
c>.批处理执行模式
1 [[email protected] ~]# more yinzhengjie.sql 2 select user(); 3 create database yinzhengjie; 4 show databases; 5 [[email protected] ~]# 6 [[email protected] ~]# mysql -uroot -p123 < yinzhengjie.sql 7 user() 8 [email protected] 9 Database 10 information_schema 11 mysql 12 performance_schema 13 test 14 yinzhengjie 15 [[email protected] ~]#
批处理模式执行命令案例
四.MySQL的数据类型
1.键