• 存储引擎本质上就是表类型,每个表都可以根据需要选择适合的表类型,是表级别的概念,同一个数据库的各个表是可以使用不同的存储类型的;
  • 存储引擎在创建表时,可以进行指定,否则使用的就是默认的存储引擎;
  • 存储引擎:
  • MyISAM:不支持事务,支持表级别的锁;
  • InnoDB:支持事务,支持行级别的锁;
  • show table status:用于查看表的状态,以及使用的存储引擎等信息;
  • 连接SQL服务器的两种方式:
  • 动态SQL:通过函数或者方法与数据库服务建立连接;
  • 嵌入式SQL:使用程序开发语言本身的格式来写的,通过API直接连接到服务器上面
  • mysqld_safe:使用的是安全线程;
  • mysqld_multi:用于在一个物理主机上面运行多个MySQL实例,一般是通过监听在不同的端口上面实现的;
  • 查看MySQL默认的指令和参数
[root@server60 ~]# mysql --help --verbose

SQL server 服务器属性 处理器的线程设置_mysql

  • show character set:用于查看支持的字符集合
  • SQL server 服务器属性 处理器的线程设置_Mysql数据类型和基本操作_02

  • 管理DBA的主要职责:数据库安装,升级,备份恢复,恢复,用户管理,权限管理,监控,性能分析,基准测试;
  • 经验总结是核心;
  • MySQL的数据类型
  • 数值型:
  • 精确数值型:INT[整形] decimal[十进制]
  • 近似数值型:FLOAT[浮点型] DOUBLE real
  • 字符型:
  • 定长:CHAR BINARY[区分大小写]
  • 变长:VARCHAR VARBINARY[区分大小写]
  • 文本大对象:text[不区分大小写] blob[区分大小写]
  • 枚举:ENUM
  • 集合:SET
  • 日期时间:
  • data:
  • time:
  • datetime:
  • timestamp:
  • 数据类型的作用:
  • 1.用于存储什么类型的值;
  • 2.能够占据多大的空间;
  • 3.定长还是变长的;
  • 4.MySQL如何对其进行比较和排序;
  • 5.是否可以创建索引;
  • MySQL的数值类型:
  • Character String Types;
  • Binary Large Object String Types
  • Numeric Types
  • Boolean Types
  • datetime Types
  • Internal Types
  • Numeric Data Types:
  • TINYINT:A Very Small integer 1个字节;
  • SMALLINT:A Small integer 2个字节;
  • MEDIUMINT:A medium-sized integer 3个字节;
  • INT:A standard integer 4个字节;
  • BIGINT:A large integer 8个字节;
  • DECIMAL:精确十进制数;
  • FLOAT:A single-precision floating-point number;
  • DOUBLE:A Double-precision floating-point number;
  • BIT:A bit field,按位进行存储;
  • String Data Types :
  • CHAR(M):Max 255;
  • VARCHAR(M):MAX 65535 M-1个字符,一个字节可以表示的需要一个结束符号,每一个字节都需要多出一位用于存储结束符号;
  • BINARY:MAX
  • VARBINARY:
  • TINYBLOB:
  • BLOB:表示二进制大对象;
  • MEDIUMBLOB:
  • LONGBLOB:
  • TINYTEXT(L):表示255 L-1;
  • TEXT(L):表示65535 L-2;
  • MEDIUMTET(L):16777215 L-3;
  • LONGTEXT(L):4294967 295 L-4;
  • ENUM:枚举,只能够在里面选择一个,必须是有限的范围,最多可以表示65535种变化;
  • SET:集合,里面的元素是可以随意组合的,通常枚举的是1-->64个字符串;
  • Data and Time Data Types :
  • DATA:CCYY-MM-DD;
  • TIME:hh:mm:ss;
  • DATATIME:CCYY-MM-DD hh:mm:ss表示范围1000-01-01 00:00:01 to 9999-12-31 23:59:59
  • YEAR:CCYY YY,表示范围YEAR(2) 00-->99 YEAR(4):1901-2155
  • 字符串类型的修是属性:
  • NOT NULL:表示不允许为空;
  • NULL:表示可以为空;
  • DEFAULT:表示默认值;
  • CHARACTER SET:表示字符集和;
  • COLLATION:表示排序规则字符的排序规则从表继承,表从数据库继承,数据库从数据库服务器来继承;
  • 查看当前数据库系统支持的字符集合
  • SQL server 服务器属性 处理器的线程设置_mysql_03

  • 显示所有的排序规则
  • SQL server 服务器属性 处理器的线程设置_存储引擎_04

  • Binary Large Object String Types:这个是区分大小写的;
  • TINYBLOB:255bytes;
  • MEDIUMBLOB:16Mb;
  • LONGBLOB:4Gb;
  • BLOB:64Kb;
  • BINARY:
  • VARBINARY:
  • 字段支持的属性
  • AUTO_INCREMENT :自动生成数据序列,可以自动进行增长,可以设定步长;
  • 这个字段不可以为空,需要创建索引,主键,或者是唯一键索引;
  • 必须是整数类型,必须是是无符号类型unsigned;
  • 可以使用函数LAST_INSERT_ID()用于显示上一次生成的自动序列的值,函数的执行使用select 函数名()
  • 必须定义为NOT NULL;
  • MySQLSQL模型
  • 用于定义用户的数据数据或者行为在违反了SQL 的模式的时候,应该采取怎样的机制;
  • ANSI_QUOTE:表示双引号相当于反引号,单引号用于标识字符串;
  • IGNORE_SPACE:表示在内键函数时,忽略空白字符;
  • STRICT_ALL_TABLES:没有进行设置的时候,所有的非法数据都是允许的,但是返回警告信息如果设置,就不允许进行填入;
  • STRICT_TRANS_TABLS:向支持事务的表中增加非法的数据是不允许的,并且返回错误;
  • TRADITIONAL:使MySQL的模型支持更多中的数据库模型;
  • 查看使用的SQL模型
  • SQL server 服务器属性 处理器的线程设置_mysql_05

服务器变量

  • 全局变量:
  • 是在服务器配置文件里面定义的变量,通过mysql> SHOW GLOBAL VARIABLES;进行查看,全局的配置只有管理员才有权限;
  • 会话变量:
  • 表示在登录Mysql会话中的变量,会话终止,变量的生命周期结束;,使用mysql> SHOW SESSION VARIABLES;进行查看;
  • 按照生效时间:
  • 动态调整:可以及时调整,立即生效;
  • 静态的:有的写在配置文件中,有的必须通过参数传递给Mysqld进程;
  • 动态调整参数的生效方式:
  • 全局:对当前会话无效,只对于新建立会话有效;
  • 会话:及时生效,只对于当前会话有效;
  • 新建立的会话默认是从全局继承的,但是允许修改;
  • @@用于显示服务器变量,@用于显示用户自定义变量
  • SQL server 服务器属性 处理器的线程设置_数据库_06


  • SQL server 服务器属性 处理器的线程设置_存储引擎_07

  • 服务器变量的设定:
  • SELECT GLOBAL | SESSION 变量名='value';;
  • 全局变量对于新建立会话有效,对于当前会话无效;
  • SQL server 服务器属性 处理器的线程设置_存储引擎_08

  • 新建立会话查询会话变量是否生效
  • SQL server 服务器属性 处理器的线程设置_Mysql数据类型和基本操作_09

  • 数据库的管理
  • 创建数据库
  • SQL server 服务器属性 处理器的线程设置_存储引擎_10

mysql> CREATE DATABASE IF NOT EXISTS studentsdb CHARACTER SET 'gbk' COLLATE 'gbk_chinese_ci';
Query OK, 1 row affected (0.81 sec)
  • 对应的数据库的数据目录里面会生成一个目录
  • SQL server 服务器属性 处理器的线程设置_Mysql数据类型和基本操作_11

  • 修改数据库
  • SQL server 服务器属性 处理器的线程设置_Mysql数据类型和基本操作_12

  • 对于修改来说,通常只限于修改CHARACTER[字符集合] SET COLLATE[排序规则]以及DIRECTORY NAME[数据字典],最后一个多用于数据库数据需要从一个老版本的迁移到新版本时可能需要升级数据字典名称
  • 删除数据库
  • SQL server 服务器属性 处理器的线程设置_存储引擎_13

  • 表的管理
  • 创建表
  • 1.直接定义一张空表
  • SQL server 服务器属性 处理器的线程设置_mysql_14

mysql> CREATE TABLE tb1(
    -> id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    -> name CHAR(30) NOT NULL,
    -> age TINYINT NOT NULL);
Query OK, 0 rows affected (2.35 sec)
  • 对于上面主键的定义还可以通过
mysql> CREATE TABLE tb2(
    -> id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    -> name CHAR(30) NOT NULL,
    -> age TINYINT NOT NULL,
    -> PRIMARY KEY(id) UNIQUE(name),INDEX(age));
Query OK, 0 rows affected (0.03 sec)
  • 键:都是索引,属于特殊的索引,也称为约束,属于特殊的索引,索引通常包括两种B-tree HASH;
mysql> CREATE TABLE courses(
    -> Cid TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    -> Course VARCHAR(50) NOT NULL);
Query OK, 0 rows affected (0.01 sec)
  • 查看表的状态
  • 删除表,并且重新创建表,指定存储引擎为MyISAM
mysql> CREATE TABLE courses( Cid TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, Course VARCHAR(50) NOT NULL) ENGINE=MyISAM;
mysql> SHOW TABLE STATUS LIKE 'courses'\G;
*************************** 1. row ***************************
           Name: courses
         Engine: MyISAM
        Version: 10
     Row_format: Dynamic  由于存储机制的不同这一项是发生改变了的;
           Rows: 0
 Avg_row_length: 0
    Data_length: 0
Max_data_length: 281474976710655
   Index_length: 1024
      Data_free: 0
 Auto_increment: 1
    Create_time: 2018-05-07 06:14:15
    Update_time: 2018-05-07 06:14:15
     Check_time: NULL
      Collation: gbk_chinese_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)
  • 插入数据
mysql> INSERT INTO courses (Course) values("lishi"),('shuxue'),('huaxue');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
mysql> SELECT * FROM courses;
+-----+--------+
| Cid | Course |
+-----+--------+
|   1 | lishi  |
|   2 | shuxue |
|   3 | huaxue |
+-----+--------+
3 rows in set (0.00 sec)
  • 查看索引
mysql> SHOW INDEX FROM courses\G;
*************************** 1. row ***************************
        Table: courses
   Non_unique: 0
     Key_name: PRIMARY
 Seq_in_index: 1
  Column_name: Cid
    Collation: A
  Cardinality: 3
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE
      Comment: 
Index_comment: 
1 row in set (0.01 sec)
  • 2.从其他表中查询出数据,并且以之创建新表;
mysql> CREATE TABLE testcourses SELECT * FROM courses WHERE Cid <=2;
Query OK, 2 rows affected (0.05 sec)
Records: 2  Duplicates: 0  Warnings: 0

SQL server 服务器属性 处理器的线程设置_数据库_15

  • 查看表的结构
  • SQL server 服务器属性 处理器的线程设置_数据库_16

  • 表的结构是近似一样的,但是丢失了AUTO_INCREMENT属性
  • 3.仿照其他表的末班进行创建新表
  • SQL server 服务器属性 处理器的线程设置_mysql_17

  • ;
mysql> CREATE TABLE test LIKE courses;
Query OK, 0 rows affected (0.02 sec)

mysql> DESC courses;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| Cid    | tinyint(3) unsigned | NO   | PRI | NULL    | auto_increment |
| Course | varchar(50)         | NO   |     | NULL    |                |
+--------+---------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
  • 关于create_definition
  • SQL server 服务器属性 处理器的线程设置_数据库_18

  • 支持表的选项
  • SQL server 服务器属性 处理器的线程设置_存储引擎_19

  • ** MyISAM和InnoDB存储引擎的存储机制是不一样的**
  • 修改表
  • SQL server 服务器属性 处理器的线程设置_存储引擎_20

  • 添加唯一键索引
  • SQL server 服务器属性 处理器的线程设置_mysql_21

  • 查看添加的索引
  • SQL server 服务器属性 处理器的线程设置_数据库_22

  • 修改字段的定义,必须自己附带上所有的属性信息,否则属性信息会丢失;
mysql> ALTER TABLE test CHANGE Course course VARCHAR(50) NOT NULL;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
  • 新增加字段
mysql> ALTER TABLE test ADD starttime date default '2018-05-8';
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> DESC test;
+-----------+---------------------+------+-----+------------+----------------+
| Field     | Type                | Null | Key | Default    | Extra          |
+-----------+---------------------+------+-----+------------+----------------+
| Cid       | tinyint(3) unsigned | NO   | PRI | NULL       | auto_increment |
| course    | varchar(50)         | NO   | UNI | NULL       |                |
| starttime | date                | YES  |     | 2018-05-08 |                |
+-----------+---------------------+------+-----+------------+----------------+
3 rows in set (0.00 sec)
  • 更改表的名称
mysql> ALTER TABLE test RENAME TO tcoursesest;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW TABLES;
+----------------------+
| Tables_in_studentsdb |
+----------------------+
| courses              |
| tcoursesest          |
| testcourses          |
+----------------------+
3 rows in set (0.00 sec)
  • 删除表
  • 创建student
mysql> CREATE TABLE student(
    -> SID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    -> Name VARCHAR(50),
    -> CID INT NOT NULL);
Query OK, 0 rows affected (0.00 sec)
  • 插入一些数据
mysql> INSERT INTO student(Name,CID) VALUES('Yue Buqun','2'),('Zhang Wuji','3');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0
  • 将两张表进行笛卡尔乘积
mysql> SELECT Name,Course FROM student,courses WHERE student.CID=courses.CID;
+------------+--------+
| Name       | Course |
+------------+--------+
| Yue Buqun  | shuxue |
| Zhang Wuji | huaxue |
+------------+--------+
2 rows in set (0.00 sec)
  • 添加外键约束[只能够用在支持事物的存储引擎上面,并且两个字段的属性必须是一样的],并且需要定义数据不匹配时的行为
mysql> ALTER TABLE courses ENGINE=InnoDB;
Query OK, 3 rows affected (0.39 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE student ADD FOREIGN KEY (CID) REFERENCES courses(CID);
ERROR 1005 (HY000): Can't create table 'studentsdb.#sql-ac4_1' (errno: 150)
mysql> ALTER TABLE student MODIFY CID TINYINT UNSIGNED NOT NULL;
Query OK, 2 rows affected (0.08 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE student ADD FOREIGN KEY foreign_cid (CID) REFERENCES courses(CID);
Query OK, 2 rows affected (0.77 sec)
Records: 2  Duplicates: 0  Warnings: 0
  • 查看外键索引
  • SQL server 服务器属性 处理器的线程设置_存储引擎_23

  • 索引是不能够被删除的;
  • 接下来尝试插入课程名不存在的课程ID
mysql> INSERT INTO student (name,cid) VALUES('Chen xiaoxiao','5');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`studentsdb`.`student`, CONSTRAINT `student_ibfk_2` FOREIGN KEY (`CID`) REFERENCES `courses` (`Cid`))

mysql> INSERT INTO student (name,cid) VALUES('Chen xiaoxiao','3');
Query OK, 1 row affected (0.00 sec)
  • 如果擅长已经存在学生选修的课程,会执行默认的策略,这里的默认的策略是不允许进行删除的
mysql> DELETE FROM courses WHERE CID=3;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`studentsdb`.`student`, CONSTRAINT `student_ibfk_2` FOREIGN KEY (`CID`) REFERENCES `courses` (`Cid`))
  • 创建索引
mysql> CREATE INDEX name_on_student ON student (Name) USING BTREE;
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0
  • 查看索引
  • SQL server 服务器属性 处理器的线程设置_数据库_24

  • 删除索引
  • SQL server 服务器属性 处理器的线程设置_Mysql数据类型和基本操作_25

mysql> DROP INDEX name_on_student ON student;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0
//创建索引是为了实现比较和排序,索引的长度只需要能够用于区分索引,就可以了,不需要太长;
//索引不能够太大,索引是为了加速查找,
mysql> CREATE INDEX name_on_student ON student (Name(5) DESC) USING BTREE;

SQL server 服务器属性 处理器的线程设置_mysql_26

  • 单表,多表查询操作
  • SELECT查询
  • 简单查询:
  • SELECT * FROM tb_name;
  • SQL server 服务器属性 处理器的线程设置_存储引擎_27


  • [投影]SELECT field1 field2 FROM tb_name;
  • SQL server 服务器属性 处理器的线程设置_Mysql数据类型和基本操作_28


  • [选择]SELECT [DISTINCT(表示相同的值只显示一次)] * FROM tb_name WHERE qualification;
  • SQL server 服务器属性 处理器的线程设置_数据库_29


SQL server 服务器属性 处理器的线程设置_数据库_30

  • FROM子句:表示要查询的关系 表,多个表,其他SELECT语句;
  • WHERE子句:bool关系表达!= | > | < | <= | >=,数值比较不能够加引号,字符串比较必须添加引号;
  • <=>:表示存在空值也能够进行正确比较的;
  • 对于字符使用''进行引用,对于数值型,不能够使用引号;
mysql> SELECT Name,Age FROM students WHERE Age+1>20;
+-------------+------+
| Name        | Age  |
+-------------+------+
| DingDian    |   25 |
| HuFei       |   31 |
| ZhangWuji   |   20 |
| Xuzhu       |   26 |
| LingHuchong |   22 |
+-------------+------+
5 rows in set (0.00 sec)
  • 对于直接在搜索码上面进行表达式计算,是不能够有效的使用索引的;
  • 表达式之间的逻辑关系:AND OR NOT;
  • AND
mysql> SELECT Name,Gender,Age FROM students WHERE Age>20 AND Gender='M';
+-------------+--------+------+
| Name        | Gender | Age  |
+-------------+--------+------+
| DingDian    | M      |   25 |
| HuFei       | M      |   31 |
| Xuzhu       | M      |   26 |
| LingHuchong | M      |   22 |
+-------------+--------+------+
4 rows in set (0.00 sec)
  • OR
mysql> SELECT Name,Gender,Age FROM students WHERE Age>20 OR Gender='M';
+-------------+--------+------+
| Name        | Gender | Age  |
+-------------+--------+------+
| GuoJing     | M      |   19 |
| YangGuo     | M      |   17 |
| DingDian    | M      |   25 |
| HuFei       | M      |   31 |
| ZhangWuji   | M      |   20 |
| Xuzhu       | M      |   26 |
| LingHuchong | M      |   22 |
+-------------+--------+------+
7 rows in set (0.00 sec)
  • NOT
mysql> SELECT Name,Age,Gender FROM students WHERE NOT Age>20 AND NOT Gender='M';
+--------------+------+--------+
| Name         | Age  | Gender |
+--------------+------+--------+
| HuangRong    |   16 | F      |
| YueLingshang |   18 | F      |
| YiLin        |   19 | F      |
+--------------+------+--------+
3 rows in set (0.00 sec)

mysql> SELECT Name,Age,Gender FROM students WHERE NOT (Age>20 OR Gender='M');
+--------------+------+--------+
| Name         | Age  | Gender |
+--------------+------+--------+
| HuangRong    |   16 | F      |
| YueLingshang |   18 | F      |
| YiLin        |   19 | F      |
+--------------+------+--------+
3 rows in set (0.00 sec)
  • BETWEEN AND
mysql> SELECT Name,Age,Gender FROM students WHERE Age BETWEEN 20 AND 25;
+-------------+------+--------+
| Name        | Age  | Gender |
+-------------+------+--------+
| DingDian    |   25 | M      |
| ZhangWuji   |   20 | M      |
| LingHuchong |   22 | M      |
+-------------+------+--------+
3 rows in set (0.00 sec)
  • 字符串的匹配操作:
  • %:任意长度任意字符;
mysql> SELECT Age,Name,Gender FROM students WHERE Name LIKE 'Y%';
+------+--------------+--------+
| Age  | Name         | Gender |
+------+--------------+--------+
|   17 | YangGuo      | M      |
|   18 | YueLingshang | F      |
|   19 | YiLin        | F      |
+------+--------------+--------+
3 rows in set (0.00 sec)
  • _:任意单个字符:
mysql> SELECT Age,Name,Gender FROM students WHERE Name LIKE 'Y____';
+------+-------+--------+
| Age  | Name  | Gender |
+------+-------+--------+
|   19 | YiLin | F      |
+------+-------+--------+
1 row in set (0.00 sec)
  • 正则表达式匹配
mysql> SELECT Age,Name,Gender FROM students WHERE Name RLIKE '^[XY].*';
+------+--------------+--------+
| Age  | Name         | Gender |
+------+--------------+--------+
|   17 | YangGuo      | M      |
|   18 | YueLingshang | F      |
|   26 | Xuzhu        | M      |
|   19 | YiLin        | F      |
+------+--------------+--------+
4 rows in set (0.00 sec)
  • IN
mysql> SELECT Age,Name,Gender FROM students WHERE Age IN(18,20,25);
+------+--------------+--------+
| Age  | Name         | Gender |
+------+--------------+--------+
|   25 | DingDian     | M      |
|   18 | YueLingshang | F      |
|   20 | ZhangWuji    | M      |
+------+--------------+--------+
3 rows in set (0.00 sec)
  • NULL进行比较的时候,是不能够使用=号的
mysql> SELECT Name,CID2 FROM students WHERE CID2 IS NULL;
+-------------+------+
| Name        | CID2 |
+-------------+------+
| LingHuchong | NULL |
| YiLin       | NULL |
+-------------+------+
2 rows in set (0.00 sec)
  • Order By
mysql> SELECT Name,CID2 FROM students WHERE CID2 IS NOT NULL ORDER BY Name;
+--------------+------+
| Name         | CID2 |
+--------------+------+
| DingDian     |    1 |
| GuoJing      |    7 |
| HuangRong    |    9 |
| HuFei        |   10 |
| Xuzhu        |    4 |
| YangGuo      |    3 |
| YueLingshang |    4 |
| ZhangWuji    |    7 |
+--------------+------+
8 rows in set (0.01 sec)

mysql> SELECT Name,CID2 FROM students WHERE CID2 IS NOT NULL ORDER BY Name DESC;
+--------------+------+
| Name         | CID2 |
+--------------+------+
| ZhangWuji    |    7 |
| YueLingshang |    4 |
| YangGuo      |    3 |
| Xuzhu        |    4 |
| HuFei        |   10 |
| HuangRong    |    9 |
| GuoJing      |    7 |
| DingDian     |    1 |
+--------------+------+
8 rows in set (0.00 sec)
  • 字段别名
mysql> SELECT Name AS Student_Name FROM students;
+--------------+
| Student_Name |
+--------------+
| GuoJing      |
| YangGuo      |
| DingDian     |
| HuFei        |
| HuangRong    |
| YueLingshang |
| ZhangWuji    |
| Xuzhu        |
| LingHuchong  |
| YiLin        |
+--------------+
10 rows in set (0.00 sec)
  • LIMIT
mysql> SELECT Name AS Student_Name FROM students LIMIT 2;
+--------------+
| Student_Name |
+--------------+
| GuoJing      |
| YangGuo      |
+--------------+
2 rows in set (0.00 sec)
  • LIMIT 2,3表示偏移两个之后,取三个;
mysql> SELECT Name AS Student_Name FROM students LIMIT 2,3;
+--------------+
| Student_Name |
+--------------+
| DingDian     |
| HuFei        |
| HuangRong    |
+--------------+
3 rows in set (0.00 sec)
  • 聚合运算:通常包括MAX MIN SUM AVG COUNT
mysql> SELECT AVG(age) FROM students;
+----------+
| AVG(age) |
+----------+
|  21.3000 |
+----------+
1 row in set (0.02 sec)
  • Group By用于进行分组,分组的目的多用于进行聚合运算
mysql> SELECT AVG(Age) FROM students GROUP BY Gender;
+----------+
| AVG(Age) |
+----------+
|  17.6667 |
|  22.8571 |
+----------+
2 rows in set (0.00 sec)
  • 对于GROP BY的结果进行过滤使用HAVING qualification(条件等同于Where);
mysql> SELECT CID2, COUNT(CID2) AS Person_choice FROM new_student GROUP BY CID2  HAVING Person_choice>=2;
+------+---------------+
| CID2 | Person_choice |
+------+---------------+
|    6 |             2 |
|    7 |             2 |
+------+---------------+
2 rows in set (0.00 sec)
  • 多表查询
  • 交叉连接:也成为迪卡尔乘积;
  • 内连接:自然连接,两张表上面的值,进行等值比较,只有保持了等值关系的才连接起来;对应字段必须存在等值关系;
mysql> SELECT students.Name,courses.Cname FROM students,courses WHERE students.CID1=courses.CID;
+--------------+------------------+
| Name         | Cname            |
+--------------+------------------+
| GuoJing      | TaiJiquan        |
| YangGuo      | TaiJiquan        |
| DingDian     | Qishangquan      |
| HuFei        | Wanliduxing      |
| HuangRong    | Qianzhuwandushou |
| YueLingshang | Wanliduxing      |
| ZhangWuji    | Hamagong         |
| Xuzhu        | TaiJiquan        |
+--------------+------------------+
8 rows in set (0.00 sec)
  • 表的别名机制
mysql> SELECT s.Name,c.Cname FROM students AS s,courses AS c WHERE s.CID1=c.CID;
+--------------+------------------+
| Name         | Cname            |
+--------------+------------------+
| GuoJing      | TaiJiquan        |
| YangGuo      | TaiJiquan        |
| DingDian     | Qishangquan      |
| HuFei        | Wanliduxing      |
| HuangRong    | Qianzhuwandushou |
| YueLingshang | Wanliduxing      |
| ZhangWuji    | Hamagong         |
| Xuzhu        | TaiJiquan        |
+--------------+------------------+
8 rows in set (0.00 sec)
  • 外连接:不需要在两个表之间建立完全的等值关系,比如说,按照右表为标准,显示右表的所有字段和左表的某些字段;或者使用左表为标准,显示左表的某些字段和右表的所有字段;
  • 左外连接: ... LEFT JOIN ...ON...;
mysql> SELECT s.Name,c.Cname FROM students AS s LEFT JOIN courses AS c ON s.CID1=c.CID;
+--------------+------------------+
| Name         | Cname            |
+--------------+------------------+
| GuoJing      | TaiJiquan        |
| YangGuo      | TaiJiquan        |
| DingDian     | Qishangquan      |
| HuFei        | Wanliduxing      |
| HuangRong    | Qianzhuwandushou |
| YueLingshang | Wanliduxing      |
| ZhangWuji    | Hamagong         |
| Xuzhu        | TaiJiquan        |
| LingHuchong  | NULL             |
| YiLin        | NULL             |
+--------------+------------------+
10 rows in set (0.00 sec)
  • 右外连接:..... RIGHT JOIN ...ON...
mysql> SELECT s.Name,c.Cname FROM students AS s RIGHT JOIN courses AS c ON s.CID1=c.CID;
+--------------+------------------+
| Name         | Cname            |
+--------------+------------------+
| ZhangWuji    | Hamagong         |
| GuoJing      | TaiJiquan        |
| YangGuo      | TaiJiquan        |
| Xuzhu        | TaiJiquan        |
| NULL         | Yiyangzhi        |
| NULL         | Jinshejianfa     |
| HuangRong    | Qianzhuwandushou |
| DingDian     | Qishangquan      |
| NULL         | Qiankundanuoyi   |
| HuFei        | Wanliduxing      |
| YueLingshang | Wanliduxing      |
| NULL         | Pixiejianfa      |
| NULL         | Jiuyinbaiguzhua  |
+--------------+------------------+
  • 全外连接:MySQL不支持;
  • 自连接:需要连接的数据项依然在自己的这张表上面;
mysql> SELECT c.Name AS stu,s.Name AS teacher FROM students AS s, students AS c WHERE c.TID=s.SID;
+-----------+-------------+
| stu       | teacher     |
+-----------+-------------+
| GuoJing   | DingDian    |
| YangGuo   | GuoJing     |
| DingDian  | ZhangWuji   |
| HuFei     | HuangRong   |
| HuangRong | LingHuchong |
+-----------+-------------+
5 rows in set (0.00 sec)
  • 子查询:在某一张表内部进行的查询,一个查询中嵌套另外一个查询的方式,在比较操作中使用子查询,子查询的返回值只有一个;
mysql> SELECT Name FROM students WHERE Age > (SELECT AVG(age) FROM students);
+-------------+
| Name        |
+-------------+
| DingDian    |
| HuFei       |
| Xuzhu       |
| LingHuchong |
+-------------+
4 rows in set (0.00 sec)
  • IN中使用子查询
mysql> SELECT Name FROM students WHERE Age IN(SELECT Age FROM tutors);
Empty set (0.01 sec)
  • FROM中使用子查询
mysql> SELECT Name,Age FROM (SELECT Name,Age FROM students) AS t WHERE t.Age >=20;
+-------------+------+
| Name        | Age  |
+-------------+------+
| DingDian    |   25 |
| HuFei       |   31 |
| ZhangWuji   |   20 |
| Xuzhu       |   26 |
| LingHuchong |   22 |
+-------------+------+
5 rows in set (0.00 sec)
  • UNION联合两个SELECT查询结果
mysql> (SELECT Name,Age FROM students) UNION (SELECT Tname,Age FROM tutors);
+--------------+------+
| Name         | Age  |
+--------------+------+
| GuoJing      |   19 |
| YangGuo      |   17 |
| DingDian     |   25 |
| HuFei        |   31 |
| HuangRong    |   16 |
| YueLingshang |   18 |
| ZhangWuji    |   20 |
| Xuzhu        |   26 |
| LingHuchong  |   22 |
| YiLin        |   19 |
| HongQigong   |   93 |
| HuangYaoshi  |   63 |
| Miejueshitai |   72 |
| OuYangfeng   |   76 |
| YiDeng       |   90 |
| YuCanghai    |   56 |
| Jinlunfawang |   67 |
| HuYidao      |   42 |
| NingZhongze  |   49 |
+--------------+------+
19 rows in set (0.00 sec)
  • 挑选出courses表中没有被students中的CID2学习的课程的名称;挑选出courses表中没有被students中的CID2学习的课程的名称;
mysql>  SELECT Cname FROM courses WHERE CID NOT IN (SELECT DISTINCT CID2 FROM students WHERE CID2 IS NOT NULL);
+------------------+
| Cname            |
+------------------+
| TaiJiquan        |
| Qianzhuwandushou |
| Qishangquan      |
| Wanliduxing      |
+------------------+
4 rows in set (0.00 sec)
  • 挑选出没有教授任何课程的老师
mysql>  SELECT Tname FROM tutors WHERE TID NOT IN (SELECT DISTINCT TID FROM courses);
+-------------+
| Tname       |
+-------------+
| NingZhongze |
+-------------+
1 row in set (0.00 sec)
  • 找出students表中CID1有两个或者两个以上同学学习了同一门课程的课程名称
mysql> SELECT Cname FROM courses WHERE CID IN (SELECT CID1 FROM students GROUP BY CID1 HAVING COUNT(1)>=2);
+-------------+
| Cname       |
+-------------+
| TaiJiquan   |
| Wanliduxing |
+-------------+
2 rows in set (0.00 sec)
  • 显示每一位老师及其所教授的课程,没有教授的课程显示为NULL;
mysql> SELECT t.Tname, c.Cname FROM tutors AS t LEFT JOIN courses AS c on t.TID=c.TID;
+--------------+------------------+
| Tname        | Cname            |
+--------------+------------------+
| HongQigong   | Jinshejianfa     |
| HuangYaoshi  | Hamagong         |
| Miejueshitai | TaiJiquan        |
| Miejueshitai | Pixiejianfa      |
| OuYangfeng   | Qianzhuwandushou |
| YiDeng       | Qishangquan      |
| YuCanghai    | Yiyangzhi        |
| Jinlunfawang | Qiankundanuoyi   |
| Jinlunfawang | Jiuyinbaiguzhua  |
| HuYidao      | Wanliduxing      |
| NingZhongze  | NULL             |
+--------------+------------------+
11 rows in set (0.00 sec)
  • 显示每一个课程及其相关的老师,没有老师教授的课程将其老师显示为空
mysql> SELECT t.Tname, c.Cname FROM tutors AS t RIGHT JOIN courses AS c on t.TID=c.TID;
+--------------+------------------+
| Tname        | Cname            |
+--------------+------------------+
| HuangYaoshi  | Hamagong         |
| Miejueshitai | TaiJiquan        |
| YuCanghai    | Yiyangzhi        |
| HongQigong   | Jinshejianfa     |
| OuYangfeng   | Qianzhuwandushou |
| YiDeng       | Qishangquan      |
| Jinlunfawang | Qiankundanuoyi   |
| HuYidao      | Wanliduxing      |
| Miejueshitai | Pixiejianfa      |
| Jinlunfawang | Jiuyinbaiguzhua  |
+--------------+------------------+
10 rows in set (0.00 sec)
  • 显示每位同学CID1课程的课程名称,以及其讲授的相关课程的老师的名称;
mysql> SELECT Name, Cname , Tname FROM students, courses, tutors WHERE students.CID1=courses.CID AND courses.TID=tutors.TID;
+--------------+------------------+--------------+
| Name         | Cname            | Tname        |
+--------------+------------------+--------------+
| GuoJing      | TaiJiquan        | Miejueshitai |
| YangGuo      | TaiJiquan        | Miejueshitai |
| DingDian     | Qishangquan      | YiDeng       |
| HuFei        | Wanliduxing      | HuYidao      |
| HuangRong    | Qianzhuwandushou | OuYangfeng   |
| YueLingshang | Wanliduxing      | HuYidao      |
| ZhangWuji    | Hamagong         | HuangYaoshi  |
| Xuzhu        | TaiJiquan        | Miejueshitai |
+--------------+------------------+--------------+
8 rows in set (0.00 sec)
  • 视图
  • 表示的含义就是存储下来的SELECT语句,基于基表的查询结果,视图就是保存的SELECT语句,但是可以使用SHOW TABLES进行查看,查看这张表数据的过程其实就是在执行存储的SELECT语句的过程;
  • 创建视图
mysql> CREATE VIEW sct AS SELECT t.Tname, c.Cname FROM tutors AS t LEFT JOIN courses AS c on t.TID=c.TID;
Query OK, 0 rows affected (0.02 sec)

mysql> SHOW TABLES;
+------------------+
| Tables_in_jiaowu |
+------------------+
| courses          |
| scores           |
| sct   [视图]      |
| students         |
| tutors           |
+------------------+
5 rows in set (0.00 sec)
  • 查询SELECT * FROM sct
mysql> SELECT * FROM sct;
+--------------+------------------+
| Tname        | Cname            |
+--------------+------------------+
| HongQigong   | Jinshejianfa     |
| HuangYaoshi  | Hamagong         |
| Miejueshitai | TaiJiquan        |
| Miejueshitai | Pixiejianfa      |
| OuYangfeng   | Qianzhuwandushou |
| YiDeng       | Qishangquan      |
| YuCanghai    | Yiyangzhi        |
| Jinlunfawang | Qiankundanuoyi   |
| Jinlunfawang | Jiuyinbaiguzhua  |
| HuYidao      | Wanliduxing      |
| NingZhongze  | NULL             |
+--------------+------------------+
11 rows in set (0.00 sec)
  • 查询的得到的结果就是存储的SELECT语句的执行过程,视图在不违反基表的存储法则的情况下,是可以插入数据的,但是是不建议这么做的;
  • 删除视图
DROP VIEW VIEW_NAME
  • 物化视图,可以将SELECT的执行结果,保存下来,用于加快查询结果,否则SELECT的查询结果就在缓存中,如果缓存的的结果非常大,会导致资源的浪费,所以物化视图可以降低缓存的利用率,但是如果基表更新,那么物化下来的视图也要随之更新,更新不频繁的使用物化视图,MySQL是不支持物化视图的,也不支持在视图上面创建索引;
  • 视图可以用于限定某些用户茶看到的字段,这是为了安全的考虑;
  • 查看创建表的语句或者是过程
mysql> SHOW CREATE TABLE courses\G;
*************************** 1. row ***************************
       Table: courses
Create Table: CREATE TABLE `courses` (
  `CID` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `Cname` varchar(100) NOT NULL,
  `TID` smallint(6) NOT NULL,
  UNIQUE KEY `CID` (`CID`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
  • 查看视图的创建过程
mysql> SHOW CREATE VIEW sct\G;
*************************** 1. row ***************************
                View: sct
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `sct` AS select `t`.`Tname` AS `Tname`,`c`.`Cname` AS `Cname` from (`tutors` `t` left join `courses` `c` on((`t`.`TID` = `c`.`TID`)))
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)
  • 使用AUTO_INCREMENT字段属性,可以通过计数器进行查看
  • 这是一个计数器,即使表清空,但是计数器没有清空,那么仍然是从这里面计数值的下一个开始;
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|               10 |
+------------------+
1 row in set (0.00 sec)
  • 如果需要删除表,并且清空计数器,那么应该使用DRUNCATE table_name来删除某张表;
  • 将查询的数据批量插入另一个表中
mysql> INSERT INTO tutors (Tname,Gender,Age) SELECT Name,Gender,Age FROM students WHERE Age >= 20;
Query OK, 5 rows affected (0.77 sec)
Records: 5  Duplicates: 0  Warnings: 0
  • UPDATE
  • SQL server 服务器属性 处理器的线程设置_数据库_31


  • 物化视图:就是用于保存select语句的执行结果,坏处,基表更新,物化视图不建议使用,
  • mysql不支持物化视图;
  • 数据的插入操作:
  • insert into tb_name (coli,coli,..) values(vali,vali,...)[,(vali,vali,...)]
  • 对于字符串使用单引号,否则不使用,对于空值,使用NULL,不能够使用''来表示;
  • 还可以使用这种方式进行插入
  • 还支持将一个表的查询结果,插入到另一张表中;
  • replace into:如果原来的表中存在数据,就进行替换,否则进行插入,用法和上面的一样;
  • DELETE:操作很危险,可以通过限定条件来删除表中的数据;
  • truncate:同时是一个函数,用于截取字符串;这里是用来清空表,并且重置计数器;
  • update:用于更新数据,
update tb_name set coli=...,coli=... where