MySQL教程
一、了解SQL
1.1 数据库基础
数据库(database):保存有组织的数据的容器(通常是一个文件或一组文件)
表(table):某种特定类型数据的结构化清单
备注:数据库中的每个表用表名标识自己,此名字是唯一的。
数据类型(datatype):所容许的数据的类型。每个表列都有都有相应的数据类型,它限制(或容许)该列中存储的数据
SQL(发音为字母S-Q-L或sequel)是结构化查询语言(Structured Query Language)的缩写。SQL是一种专门用来与数据库通信的语言
MySQL是一个客户机—服务器DBMS
表头(header):每一列的名称
行(row):表中的一个记录
值(value):行的具体信息,每个值必须与该列的数据类型相同
主键(primary key):一列(或一组列),其值能够唯一区分表中每一行
列(column):表中的一个字段,所有表都是由一个或多个列组成的
外键(foreign key):外键为某个表中的一列,它包含另一个表中的主键值
1.2 MySQL脚本的基本组成
MySQL通过执行SQL脚本来完成对数据库的操作,该脚本由一条或多条MySQL语句(SQL语句+扩展语句)组成,保存时脚本文件后缀名一般为.sql,在控制台下,MySQL客户端也可以对语句进行单句的执行而不用保存为.sql文件
标识符
标识符用来命名一些对象, 如数据库、表、列、变量等,以便在脚本中的其他地方引用。MySQL标识符命名规则稍微有点繁琐, 这里我们使用万能命名规则: 标识符由字母、数字或下划线(_)组成, 且第一个字符必须是字母或下划线。
对于标识符是否区分大小写取决于当前的操作系统, Windows下是不敏感的, 但对于大多数 linux\unix 系统来说, 这些标识符大小写是敏感的。
关键字:
MySQL的关键字众多, 这里不一一列出, 在学习中学习。这些关键字有自己特定的含义,尽量避免作为标识符。
语句:
MySQL语句是组成MySQL脚本的基本单位, 每条语句能完成特定的操作,他是由 SQL 标准语句 + MySQL 扩展语句组成。
函数:
MySQL函数用来实现数据库操作的一些高级功能, 这些函数大致分为以下几类: 字符串函数、数学函数、日期时间函数、搜索函数、加密函数、信息函数。
1.3 MySQL中的数据类型
MySQL有三大类数据类型, 分别为数字、日期\时间、字符串, 这三大类中又更细致的划分了许多子类型:
数字类型
整数: tinyint、smallint、mediumint、int、bigint
浮点数: float、double、real、decimal
日期和时间: date、time、datetime、timestamp、year
字符串类型
字符串: char、varchar
文本: tinytext、text、mediumtext、longtext
二进制(可用来存储图片、音乐等):
tinyblob、blob、mediumblob、longblob
二、使用MySQL数据库
2.1.创建一个数据库
基本形式:create database 数据库名
例如我们需要创建一个名为test的数据库, 在命令行下执行以下命令:
createdatabase test character set gbk;
为了便于在命令提示符下显示中文, 在创建时通过 character set gbk 将数据库字符编码指定为 gbk。创建成功时会得到 Query OK, 1 row affected(0.02 sec) 的响应。
2.2.选择要操作的数据库
基本形式:use database;
use 语句可以不加分号, 执行 use samp_db 来选择刚刚创建的数据库, 选择成功后会提示: Database changed
2.3创建数据库表
基本形式: createtable表名称(列声明);

2.4.删除数据库
基本形式:drop database 数据库名;
drop database test;

三、操作MySQL数据库
3.1.增
3.1.1插入数据
插入完整的行
基本形式:Insert into 表名值()

插入完整的行更安全的方法:

插入多个行

插入检索出的数据

3.1.2 增加一列数据
基本形式:alter table 表名 add 列名列的数据类型 [after 插入位置]
alter table customers add telchar(11);

在cust_id之后增添列sex:
alter table customers add sexchar(11) after cust_name;

3.2.查
3.2.1 查询语句
基本形式:select 列名 from 表名[查询条件];
查询顾客姓名和城市
select cust_name,cust_city fromcustomers;

使用通配符*查询表中所有的内容,基本形式:select * from 表名;
Select * from customers;

按照特定条件查询:基本形式 select * from 表名 where 条件;
查询产品中产品名称是fuses的信息
select * from products whereprod_name = 'fuses';

查询产品中产品价格<=10的信息
select * from products whereprod_price <=10;

3.2.2操作符
And 操作符
检索匹配and连接的所有条件的行
查询产品中产品id是1003 and产品价格<=10的信息
select * from products where vend_id=1003 and prod_price <=10;

or 操作符
检索匹配任一条件的行
查询产品中产品id是1003 或者产品价格<=10的信息
select * from products where vend_id=1003 or prod_price <=10;

In操作符
用来指定条件范围,范围中的每个条件都可以进行匹配
order by 操作符
用来指定列以字母顺序排序数据
select *from products where vend_id in (1002,1003)
order by prod_name;

3.2.3通配符进行操作
like操作符
通配符:用来匹配值的一部分的特殊字符
百分号(%)通配符:匹配0到多个字符
select *from products where prod_name like '%anvil%'
order by prod_name;

下划线(_)通配符:只匹配单个字符而不是多个字符

3.2.4正则表达式进行搜索
.表示匹配任一一个字符
|表示匹配两个串之一
[123]表示匹配1或2或3,[123]ton表示匹配1ton或2ton或3ton

匹配特殊字符
\\.表示查找., \\-表示查找- 。

匹配多个实例
select *from products where prod_name regexp '[[:digit:]]{4}'
order by prod_name;

定位符
select *from products where prod_name regexp '^[0-9\\.]'
order by prod_name;

3.2.5创建计算字段
3.2.6使用数据处理函数
3.2.7汇总数据
3.2.8分组数据
3.2.9子查询
3.2.10联结表
创建联结表
selectvendors.vend_id,vend_name,prod_name,prod_price from vendors,products
where vendors.vend_id =products.vend_id
order by vend_name,prod_name;

内部联结

创建3个连接表
selectvendors.vend_id,vend_name,prod_name,prod_price,order_num,quantity fromvendors,products,orderitems
where vendors.vend_id = products.vend_id andorderitems.prod_id = products.prod_id and order_num = 20005
order by vend_name,prod_name;

高级联结
自联结


内部联结

外部联结

3.2.11组合查询

3.3.改
3.3.1更新数据
基本形式:updata 表名称 set 列名称 = 新值 where 更新条件
更新一个列的值
updatecustomers set cust_email = 'elmer@fudd.com'
where cust_name = 'm.martian';

更新多个列的数据
updatecustomers
setcust_name = 'the fudds',
cust_email= 'elmer@fudd.com'
where cust_id = 2;

3.3.2重命名表
rename table
customers2 to customers; 重命名表customers2变成customers。

3.3.3修改列名
基本形式:alter table 表名 change 列名称列新名称新列数据类型;
alter tablecustomers change cust_email email char(5);

3.4.删
3.4.1删除特定的行
基本形式:delete from 表名 where 删除条件
delete fromcustomers
where cust_id =4;

删除所有的行
truncate table 表名
3.4.2删除表中的一列
基本形式:alter table 表名 drop 列名称;
alter tablecustomers drop sex;

3.4.3删除整个表
基本形式:drop table 表名;
drop tablecustomers;
