Linux下MYSQL数据语言,全局变量,查询


一、MYQL数据语言


   1、数据库语言

SQL语言包括四类种主要程序设计语言类别的语句:
        数据定义语言(DDL),例如:CREATE、DROP、ALTER等语句。
        数据操作语言(DML),例如:INSERT(插入)、UPDATE(修改)、DELETE(删除)语句。
        数据查询语言(DQL),例如:SELECT语句。(一般不会单独归于一类,因为只有一个语句)。
        数据控制语言(DCL),例如:GRANT、REVOKE等语句。
        事务控制语句(TCL),例如:COMMIT、ROLLBACK等语句。


   2、 数据类型


     ①、字符型

char(n)

n1~8000字符之间

n字节

nchar(n)

n1~4000 Unicode字符之间

2×n字节

nvarchar(max)

最多为230�C1(1073741823)Unicode

2×字符数+2字节额外开销

text

最多为231�C1(2147483647)字符

每字符1字节+2字节额外开销

varchar(n)

n1~8000字符之间

每字符1字节+2字节额外开销

varchar(max)

最多为231�C1(2147483647)字符

每字符1字节+2字节额外开销


binary(n)

固定长度的 n 个字节二进制数据。

N 必须从 1 到 8,000。存储空间大小为 n+4 字节。

n+4 字节。

varbinary(n)

n 个字节变长二进制数据。

N必须从 1 到 8,000。

存储空间大小为实际输入数据长度 +4 个字节,而不是 n 个字节。


   ②、数值型

bit

0、1或null

1字节(8位)

tinyint

0~255之间的整数

1字节

smallint

�C32768~32767之间的整数

2字节

int

�C2147483648~2147483647之间的整数

4字节

bigint

�C9223372036854775808~9223372036854775807

之间的整数

8字节

numeric(p,s)或decimal(p,s)

�C1038+1~1038�C1之间的数值

最多17字节

money

�C922337203685477.5808~922337203685477.5807

8字节

smallmoney

�C214748.3648~214748.3647

4字节

       近似数值型  

float[(n)]

�C1.79E+308~�C2.23E�C308,0,2.23E�C3081.79E+308

n≤24�C4字节

n>24�C8字节

real( )

�C3.40E+38~�C1.18E�C38,0,1.18E�C38~3.40E+38

4字节


   ③、时间和数据型

date

1年1月1日~99991231

3字节

datetime

1753年1月1日~99991231日,精确到最近的3.33毫秒

8字节

datetime2(n)

1年11~99991231

0~7之间的n指定小数秒

6~8字节

datetimeoffset(n)

1年1月1日~99991231

0~7之间的n指定小数秒+/�C偏移量

8~10字节

smalldateTime

1900年1月1日~207966日,精确到1分钟

4字节

time(n)

小时:分钟:秒.9999999

0~7之间的n指定小数秒

3~5字节


  3、数据类型常用属性修饰符

关键字

修饰对象

NULL

数据列可为空

所有数据类型

NOT NULL

数据列不可为空

所有数据类型

DEFAULT xxx

默认值,如果插入数据时,

没有指定值,将取这个默认值

所有数据类型


PRIMARY KEY 主键
AUTO_INCREMENT

自动增长。

前提:非空,且惟一;支持索引,非负值

仅适用于整数类型
UNSIGNED 无符号
整型,浮点型
CHARACTER SET '字符集' 指定一个字符集
CHAR、VARCHAR和TEXT
COLLATION '规则' 排序规则 CHAR、VARCHAR和TEXT


修饰符

描述

相同点

区别

Enum

枚举

它们的取值范围是一

个预先定义好的列表。

ENUM或SET数据列的取

值只能从这个列表中进行选择

ENUM只能取单值,它的数

据列表是一个枚举集合。

它的合法取值列表最多允

许有65535个成员。

例如:ENUM("N","Y")表示,

该数据列的取值要么是"Y",要么就是"N"。

Set

集合


SET可取多值。它的合法取值

列表最多允许有64个成员。

空字符串也是一个合法的SET值。


   4、MySQL中字符大小写:

1 、SQL关键字及函数名不区分字符大小写;
2 、数据库、表及视图名称的大小区分与否取决于低层OS及FS
3 、存储过程、存储函数及事件调度器的名字不区分大小写,但触发器区分;
4 、表别名区分大不写;
5 、对字段中的数据,如果字段类型为Binary类型,则区分大小写;非Binary不区分大小写



二、MYSQL变量


1、设定服务器变量的值(仅用于支持动态的变量)

支持修改的服务器变量:
    动态变量:可以在Mysql运行时修改
    静态变量:在配置文件中修改其值,并重启后方能生效;    
    服务器变量从其生效范围来讲,有两类:
    全局变量(global):服务器级别,修改之后仅对新建立的会话有效;
    会话变量(session):会话级别,仅对当前回话有效;        
    //会话建立时,从全局继承各变量;


2、查看服务器变量

mysql> SHOW [{GLOBAL|SESSION}] VARIABLES [LIKE ''];
mysql> SELECT @@{GLOBAL|SESSION}.VARILABLE_NAME;
mysql> SELECT * FROM INFORMATION_SCHEMA.GLOBAL_VARIABLES WHERE VARIABLE_NAME='SOME_VARIABLE_NAME';
mysql> SELECT * FROM INFORMATION_SCHEMA.SESSION_VARIABLES WHERE VARIABLE_NAME='SOME_VARIABLE_NAME';
    //常用为前两个


       查询包含字符bin的全局变量

wKioL1NL3HfBdTouAAGu7h2cJXw123.jpg


3、修改变量

       前提:默认仅管理员有权限修改全局变量

//格式
mysql> SET {GLOBAL|SESSION} VARIABLE_NAME='VALUE';


   示例:

mysql> SET GLOBAL tx_isolation='read-committed'; 
//设置服务器的事务隔离级别为读提交

   注意:

   无论是全局还是会话级别的动态变量修改,在重启mysqld后都会失效;想永久有效,需定义在配置文件中的相应段中。



三、库、表管理


   help帮助命令:        

mysql> help create database;    ##用help命令查看建库方法
Name: 'CREATE DATABASE'
Description:
Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name    //if not exists指如果不存在,才建立。
    [create_specification] ...


   1、库管理:

       查看库,创建、删除库test

mysql> create database test;      ##创建库test
mysql> show databases;            ##显示所有已存在库
mysql> use test;                  ##进入库test
mysql> drop database test;        ##删除库test



    2表管理:

       字段的定义:字段名、类型和类型修饰符

       键、约束或索引:

       PRIMARY KEY, UNIQUE KEY, FOREIGN KEY, CHECK

       {INDEX|KEY}


       ①、创建、删除表tb1(需要自建一个库,进入库)

mysql> create table tb1 (id tinyint AUTO_INCREMENT UNIQUE KEY, name char(30) NOT NULL, age tinyint UNSIGNED NOT NULL, gender enum('F','M') NOT NULL, note varchar(30));
##创建表tb1
mysql> show tables;    ##查看所有表明
mysql> drop table tb1;    ##删除表tb1


       ②增、删、查看表内容

mysql> insert into tb1(id,name,age,gender,note) values(1,'tom',20,'F','good boy');
Query OK, 1 row affected (0.14 sec)
mysql> insert into tb1 values(2,'jerry',21,'M','beautiful girl');
Query OK, 1 row affected (0.00 sec)        ##增加用户信息
mysql> select * from tb1;        ##查看表中数据信息
+----+-------+-----+--------+----------------+
| id | name  | age | gender | note           |
+----+-------+-----+--------+----------------+
|  1 | tom   |  20 | F      | good boy       |
|  2 | jerry |  21 | M      | beautiful girl |
+----+-------+-----+--------+----------------+
2 rows in set (0.00 sec)
mysql> delete from tb1 where name='tom';        ##删除用户信息
Query OK, 1 row affected (0.04 sec)
mysql> select * from tb1;
+----+-------+-----+--------+----------------+
| id | name  | age | gender | note           |
+----+-------+-----+--------+----------------+
|  2 | jerry |  21 | M      | beautiful girl |
+----+-------+-----+--------+----------------+
1 row in set (0.00 sec)


       根据表tb1创建一个空表tb2

mysql> create table tb2 like tb1;


③、查看表属性和状态  

       查看表属性:

mysql> DESC tb1;
+--------+---------------+------+-----+---------+----------------+
| Field  | Type          | Null | Key | Default | Extra          |
+--------+---------------+------+-----+---------+----------------+
| id     | tinyint(4)    | NO   | PRI | NULL    | auto_increment |
| name   | char(30)      | NO   |     | NULL    |                |
| age    | tinyint(4)    | NO   |     | NULL    |                |
| gender | enum('F','M') | YES  |     | NULL    |                |
| note   | varchar(30)   | YES  |     | NULL    |                |
+--------+---------------+------+-----+---------+----------------+
5 rows in set (0.04 sec)


       查看表状态

mysql> show table status like 'tb1'\G;
*************************** 1. row ***************************
           Name: tb1
         Engine: MyISAM
        Version: 10
     Row_format: Dynamic
           Rows: 2
 Avg_row_length: 28
    Data_length: 56
Max_data_length: 281474976710655
   Index_length: 2048
      Data_free: 0
 Auto_increment: 3
    Create_time: 2014-04-09 21:15:19
    Update_time: 2014-04-09 21:15:19
     Check_time: NULL
      Collation: latin1_swedish_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)


   ④、修改表中信息

       ●增加字段(add)

mysql> alter table tb1 ADD phnoe int NOT NULL after gender;    //after x 可省,默认插入最后列
Query OK, 2 rows affected (0.10 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> select * from tb1;
+----+-------+-----+--------+-------+----------------+
| id | name  | age | gender | phnoe | note           |
+----+-------+-----+--------+-------+----------------+
|  1 | tom   |  20 | F      |     0 | good boy       |
|  2 | jerry |  21 | M      |     0 | beautiful girl |
+----+-------+-----+--------+-------+----------------+
2 rows in set (0.00 sec)


       ●删除字段(drop)

mysql> alter table tb1 drop phone;        //删除字段phone


       ●修改字段属性(modify)


mysql> select * from tb1;
+----+-------+-----+--------+-------+----------------+
| id | name  | age | gender | phnoe | note           |
+----+-------+-----+--------+-------+----------------+
|  1| tom   |  20| F      |     0| good boy       |
|  2| jerry |  21| M      |     0| beautiful girl |
+----+-------+-----+--------+-------+----------------+
mysql> alter table tb1 MODIFY gender enum('man','woman') NOT NULL after name;
//修改gender字段enum内容,并且顺序改到name后面。
mysql> DESC tb1;    //查看表属性
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| id     | tinyint(4)          | NO   | PRI | NULL    | auto_increment |
| name   | char(30)            | NO   |     | NULL    |                |
| gender | enum('man','woman') | NO   |     | NULL    |                |
| age    | tinyint(4)          | NO   |     | NULL    |                |
| phnoe  | int(11)             | NO   |     | NULL    |                |
| note   | varchar(30)         | YES  |     | NULL    |                |


       ●修改字段名字(change)

mysql> alter table tb1 CHANGE name stuname char(30) NOT NULL;
//修改字段name为stuname,后面可以修改位置:after 字段名;
mysql> DESC tb1;
+---------+---------------------+------+-----+---------+----------------+
| Field   | Type                | Null | Key | Default | Extra          |
+---------+---------------------+------+-----+---------+----------------+
| id      | tinyint(4)          | NO   | PRI | NULL    | auto_increment |
| stuname | char(30)            | NO   |     | NULL    |                |
| gender  | enum('man','woman') | NO   |     | NULL    |                |
| age     | tinyint(4)          | NO   |     | NULL    |                |
| phnoe   | int(11)             | NO   |     | NULL    |                |
| note    | varchar(30)         | YES  |     | NULL    |                |
+---------+---------------------+------+-----+---------+----------------+


       ●修改表名(RENAME)

mysql> show tables;
+-------------------+
| Tables_in_da_test |
+-------------------+
| tb1               |
| tb2               |
+-------------------+
mysql> alter table tb2 RENAME TO tb6;
mysql> show tables;
+-------------------+
| Tables_in_da_test |
+-------------------+
| tb1               |
| tb6               |
+-------------------+
//
mysql> RENAME table tb2 to tb6;//也可以直接使用RENAME,有风险,不建议使用


       ●增加、查询、删除索引


mysql> alter table tb1 add INDEX(stuname);    //增加索引
mysql> show indexes from tb1;                 //查看索引
mysql> alter table tb1 drop INDEX stuname;    //删除索引



   实战演练,练习题:


新建如下表(包括结构和内容):


ID    Name          Age     Gender     Course

1     Ling Huchong   24      Male       Hamogong

2     Huang Rong    19      Female     Chilian Shenzhang

3     Lu Wushaung   18      Female     Jiuyang Shenggong

4     Zhu Ziliu     52      Male       Pixie Jianfa

5     Chen Jialuo   22      Male       Xianglong Shiba Zhang

6     Ou Yangfeng   70      Male       Shenxiang Bannuo Gong


1、新增字段:

Class 字段定义自行选择;放置于Name字段后;


2、将ID字段名称修改为TID;


3、将Age字段放置最后;


答案:
mysql> create table test (ID tinyint AUTO_INCREMENT UNIQUE KEY, Name char(30) NOT NULL, Age tinyint UNSIGNED NOT NULL, Gender enum('Female','Male') NOT NULL, Course varchar(30));
mysql> insert into test values(1,'Ling Huchong',24,'Male','Hamogong');
... ...
mysql> select * from test;
+----+--------------+-----+--------+------------------------+
| ID | Name         | Age | Gender | Course                 |
+----+--------------+-----+--------+------------------------+
|  1 | Ling Huchong |  24 | Male   | Hamogong               |
|  2 | Huang Rong   |  19 | Female | Chilian Sehnzhang      |
|  3 | Lu Wushuang  |  18 | Female | Jiuyang Shenzhang      |
|  4 | Zhu Ziliu    |  52 | Male   | Pixie Jianfa           |
|  5 | Chen Jialuo  |  22 | Male   | Xianglong Shiba Zhang  |
|  6 | OU Yangfeng  |  70 | Male   | Shengxiang Bannuo Gong |
+----+--------------+-----+--------+------------------------+
//
1、mysql> alter table test ADD Class TINYINT NOT NULL after Name;
mysql> DESC test;
+--------+-----------------------+------+-----+---------+----------------+
| Field  | Type                  | Null | Key | Default | Extra          |
+--------+-----------------------+------+-----+---------+----------------+
| ID     | tinyint(4)            | NO   | PRI | NULL    | auto_increment |
| Name   | char(30)              | NO   |     | NULL    |                |
| Class  | tinyint(4)            | NO   |     | NULL    |                |
| Age    | tinyint(3) unsigned   | NO   |     | NULL    |                |
| Gender | enum('Female','Male') | NO   |     | NULL    |                |
| Course | varchar(30)           | YES  |     | NULL    |                |
+--------+-----------------------+------+-----+---------+----------------+
//
2、mysql> alter table test CHANGE ID TID TINYINT AUTO_INCREMENT UNIQUE KEY;
mysql> select * from test;
+-----+--------------+-------+-----+--------+------------------------+
| TID | Name         | Class | Age | Gender | Course                 |
+-----+--------------+-------+-----+--------+------------------------+
|   1 | Ling Huchong |     0 |  24 | Male   | Hamogong               |
|   2 | Huang Rong   |     0 |  19 | Female | Chilian Sehnzhang      |
|   3 | Lu Wushuang  |     0 |  18 | Female | Jiuyang Shenzhang      |
|   4 | Zhu Ziliu    |     0 |  52 | Male   | Pixie Jianfa           |
|   5 | Chen Jialuo  |     0 |  22 | Male   | Xianglong Shiba Zhang  |
|   6 | OU Yangfeng  |     0 |  70 | Male   | Shengxiang Bannuo Gong |
+-----+--------------+-------+-----+--------+------------------------+
//
3、mysql> alter table test MODIFY Age TINYINT UNSIGNED NOT NULL after Course;
mysql> select * from test;
+-----+--------------+-------+--------+------------------------+-----+
| TID | Name         | Class | Gender | Course                 | Age |
+-----+--------------+-------+--------+------------------------+-----+
|   1 | Ling Huchong |     0 | Male   | Hamogong               |  24 |
|   2 | Huang Rong   |     0 | Female | Chilian Sehnzhang      |  19 |
|   3 | Lu Wushuang  |     0 | Female | Jiuyang Shenzhang      |  18 |
|   4 | Zhu Ziliu    |     0 | Male   | Pixie Jianfa           |  52 |
|   5 | Chen Jialuo  |     0 | Male   | Xianglong Shiba Zhang  |  22 |
|   6 | OU Yangfeng  |     0 | Male   | Shengxiang Bannuo Gong |  70 |
+-----+--------------+-------+--------+------------------------+-----+



四、MySQL的查询操作,select基本用法


1、查询分类

单表查询:简单查询
多表查询:连续查询
联合查询:多个查询一起使用(union)


2、选择和投影

//投影:挑选要显示的字段                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
select 字段1,字段2,... from tb_name
select * from tb_name;
//选择:挑选符合条件的行
select 字段,...from tb_name where 子句;
where子句:布尔条件表达式-->符合的显示,不符合的不显示;


       投影:

//以test表为例
mysql> select * from test;
+-----+--------------+-------+--------+------------------------+-----+
| TID | Name         | Class | Gender | Course                 | Age |
+-----+--------------+-------+--------+------------------------+-----+
|   1 | Ling Huchong |     1 | Male   | Hamogong               |  24 |
|   2 | Huang Rong   |     2 | Female | Chilian Sehnzhang      |  19 |
|   3 | Lu Wushuang  |     2 | Female | Jiuyang Shenzhang      |  18 |
|   4 | Zhu Ziliu    |     3 | Male   | Pixie Jianfa           |  52 |
|   5 | Chen Jialuo  |     1 | Male   | Xianglong Shiba Zhang  |  22 |
|   6 | OU Yangfeng  |     3 | Male   | Shengxiang Bannuo Gong |  70 |
|   7 | tom          |     1 | Male   | NULL                   |  11 |
+-----+--------------+-------+--------+------------------------+-----+


       选择:

mysql> SELECT * FROM test WHERE name='Ling Huchong';
+-----+--------------+-------+--------+----------+-----+
| TID | Name         | Class | Gender | Course   | Age |
+-----+--------------+-------+--------+----------+-----+
|   1 | Ling Huchong |     1 | Male   | Hamogong |  24 |
+-----+--------------+-------+--------+----------+-----+
1 row in set (0.00 sec)


3、布尔条件表达式操作符

=    等于
<=>  abc = bde ,abc =NULL
<>   不等于
<    小于
<=   小于等于
>    大于
>=   大于等于
IS NULL 为空
IS NOT NULL 不为空
LIKE: 支持的通配符: %(任意长度的任意字符), _(任意单个字符)
RLIKE, REGEXP: 支持使用正则表达式
IN: 判断指定字段的值是否在给定在列表中;


组合条件测试:

NOT, !
AND, &&
OR, ||


聚合函数:

SUM():求和
AVG():求平均值
MAX():求最大值
MIN():求最小值
COUNT():计数


示例:

       以表test为例:

wKioL1NL5mCyDwWuAAGK-6YSazQ100.jpg


1、查找test表中20到30岁之间的名字字段(WHERE、BETWEEN、AND)

mysql> SELECT name FROM test WHERE age BETWEEN 20 AND 30;
+--------------+
| name         |
+--------------+
| Ling Huchong |
| Chen Jialuo  |
+--------------+


2、查找test表中年龄符合21,22,23,24,25年龄的名字(IN)

mysql> SELECT name FROM test WHERE age IN(21,22,23,24,25);
+--------------+
| name         |
+--------------+
| Ling Huchong |
| Chen Jialuo  |
+--------------+
2 rows in set (0.00 sec)


3、查找以O开头的名字(LIKE、RLIKE)

mysql> SELECT name FROM test WHERE name LIKE('O%');
+-------------+
| name        |
+-------------+
| OU Yangfeng |
+-------------+
1 row in set (0.00 sec)
mysql> SELECT name FROM test WHERE name RLIKE('^O.*');
+-------------+
| name        |
+-------------+
| OU Yangfeng |
+-------------+
1 row in set (0.00 sec)


4、查询年龄大于20并且性别男,根据年龄升序排序(ORDER、ASC)

mysql> SELECT name,age,gender FROM test WHERE age > 20 AND gender='Male' ORDER BY age ASC;
+--------------+-----+--------+
| name         | age | gender |
+--------------+-----+--------+
| Chen Jialuo  |  22 | Male   |
| Ling Huchong |  24 | Male   |
| Zhu Ziliu    |  52 | Male   |
| OU Yangfeng  |  70 | Male   |
+--------------+-----+--------+


5、查询年龄大于20并且性别男,根据年龄降序排序(DESC)

mysql> SELECT name,age,gender FROM test WHERE age > 20 AND gender='Male' ORDER BY age DESC;
+--------------+-----+--------+
| name         | age | gender |
+--------------+-----+--------+
| OU Yangfeng  |  70 | Male   |
| Zhu Ziliu    |  52 | Male   |
| Ling Huchong |  24 | Male   |
| Chen Jialuo  |  22 | Male   |
+--------------+-----+--------+


6、test表以性别分组,查询各个性别的年龄和(SUM())

mysql> select Gender,sum(Age) from test group by Gender;
+--------+----------+
| Gender | sum(Age) |
+--------+----------+
| Female |       37 |
| Male   |      179 |
+--------+----------+


7、查询班级大于等于两个人的班级(GROUP BY,HAVING)

mysql> SELECT class,count(name) FROM test GROUP BY class HAVING count(name)>=2;
+-------+-------------+
| class | count(name) |
+-------+-------------+
|     1 |           3 |
|     2 |           2 |
|     3 |           2 |
+-------+-------------+


   查询总年龄小于等于50的班级(GROUP BY,HAVING)

mysql> SELECT class FROM test GROUP BY class HAVING sum(age)<=50;
+-------+
| class |
+-------+
|     2 |
+-------+


8、查询test表,只显示前两行(LIMIT 2)

mysql> select * from test limit 2;
+-----+--------------+-------+--------+-------------------+-----+
| TID | Name         | Class | Gender | Course            | Age |
+-----+--------------+-------+--------+-------------------+-----+
|   1 | Ling Huchong |     1 | Male   | Hamogong          |  24 |
|   2 | Huang Rong   |     2 | Female | Chilian Sehnzhang |  19 |
+-----+--------------+-------+--------+-------------------+-----+


   查找test表,跳过前两行,然后显示三行显示(LIMIT 2,3)

mysql> select * from test limit 2,3;
+-----+-------------+-------+--------+-----------------------+-----+
| TID | Name        | Class | Gender | Course                | Age |
+-----+-------------+-------+--------+-----------------------+-----+
|   3 | Lu Wushuang |     2 | Female | Jiuyang Shenzhang     |  18 |
|   4 | Zhu Ziliu   |     3 | Male   | Pixie Jianfa          |  52 |
|   5 | Chen Jialuo |     1 | Male   | Xianglong Shiba Zhang |  22 |
+-----+-------------+-------+--------+-----------------------+-----+



SELECT语句选项:

distinct:指定结果相同的只显示一次
sql_cache:缓存查询结果于缓存中;
sql_no_cache:不缓存查询结果



下一篇将继续讲解MYQL。





你可能感兴趣的:(mysql)