数据库基础
数据库:保存有组织的数据的容器
表:某种特定类型数据的结构化清单,具有唯一的名字
列:表中的一个字段,表由一个或多个列构成
数据类型:限制列中可存储的数据类型,便于正确的分类数据
行:表中的一条记录,行编号即为记录的编号
主键:一组列,其值能够唯一标识表中的每一行这个列;任意两行的主键值均不相同;
关键字:SQL的保留字,不能用作表或列的名字
结构化查询语言(Structured Query Language, SQL),一种专门与数据沟通的语言;语句全部是由具有很强描述性的英语单词组成,灵活运用可实现复杂的数据库操作。
构建一个初始的数据库
创建数据库:create database mydb; 使用数据库:use mydb;
扫描二维码关注公众号,回复:
4569999 查看本文章
显示数据库中的表:show tables; 创建一个表:create table testtable (id int(3)auto_increment not null primary key,time datetime,name char(10) not null,address varchar(20),year date,s1 char(11) ,s2 int default ‘1’ ); 插入一条记录:insert into testable values('1','2010-10-08', 'bj','深圳一中','2010-10-08','MySQLTest',20); 显示表中记录:select * from testtable; 按字段显示记录:select * from testtable where name = 'bj'; 按字段修改: update testtable set address = '桃园县一中' where name = 'jj'; |
检索数据(select语句)
SELECT name FROM tettable; |
从tettable表中检索一个名为name的列 |
SELECT name, address FROM tettable; |
同时检索两列 |
SELECT * FROM tettable; |
检索所有列 |
SELECT DISTINCT name FROM tettable; |
返回不同的值,过滤到数据库中相同的项目 |
SQL代码的书写方法:
- 多条SQL数据必须以分号分割;
- SQL语句的关键词并不区分大小写,但是习惯的写法是关键字使用大写,列名、表名使用小写;
- SQL语句的书写可以写很长的一行,也可以写在多行,习惯的写法是一个关键字和其操作的对象写在同一行。
- 单行注释,两个连接符--;多行注释,/*…*/
排序检索数据
Select name From tettable Order by name; |
按照name列进行排序 |
Select name, address From tettable Order by name, address; |
按order by语句后字段出现的顺序,进行排序;若前一字段唯一,则其后字段不排序;若不唯一,其后字段依次排序。 |
Select name from tettanle order by name DESC; |
降序排列,DESC只对其前的列名有效 |
Select name, address From tettable Order by 2, 3; |
按照列的相对位置进行排序 |
注:order by后边的语句可能必须是select语句后的一条子句
数据过滤
Select name From tettable Where name = ‘nj’; |
只返回name = ‘nj’的行 |
Select * from tettable where s1 = ‘MySQL’ order by name, address; |
过滤和排序组合使用,where语句与order by 子句同时使用时,order by语句应放在后边 |
Select * from table2 where price > 3; |
基于设定的判定条件进行数据过滤 |
Select * from table2 where price <> 3.98 |
不匹配检查(!=和<>有时不同,需注意) |
Select * from table2 where price between 3 and 4; |
范围检查 |
Select * from table2 where price IS NULL; |
空值检查 |
注:如果用于数值列进行比较时,不需要加引号;如果将值与字符串比较,需要加引号。
Select * from table2 where price > 2 or id = ‘10011’ or name = ‘fruit’; |
按照条件满足顺序检索 |
Select * from table2 where price > 2 and id = ‘10011’ and name = ‘fruit’; |
检索出满足所有约束的行 |
Select * from table2 where (price > 2 or id = ‘10011’) and name = ‘fruit’; |
And的优先级大于or,因此两者结合使用时应加圆括号限定,以消除歧义 |
Select * from table2 where name IN (‘fruit’, ‘water’) order by name, price; |
IN 范围内的条件进行匹配 |
Select * from table2 where NOT name = ‘fruit’ order by name, price; Select * from table2 where NOT (name IN (‘fruit’)) order by name, price; |
否定其后的任何条件,也可以由<>来实现 |
通配符过滤
Select * from table2 where name like ‘f%’; |
任何字符出现任意次数%,匹配不到空值null; Access中使用到的通配符是* |
Select * from table2 where name like ‘_ood’; |
匹配单个字符;Acess中使用的是% |
Select * from table2 where name like ‘[fw]%’; Select * from table2 where name like ‘[^fw]%’; |
指定位置从集合中选择字符匹配,^为否定[]内的字符;仅Acess和SQL Sever支持 |
通配符的使用技巧:不要滥用,其他手段可实现优先其他手段;非用不可时,尽量不要用在搜索模式的开始处;注意通配符的位置。
计算字段
Select name, price, price * 3 as expanded_price from table2 where name = ‘fruit’; |
使用算术运算得到计算字段 |
数据处理函数
函数并不是所有数据库都同等的支持,使用时必须做好注释
文本处理函数 |
TRIM |
清除字符串中的空格 |
UPPER |
小写变大写 |
|
聚集函数 |
AVG |
求均值 |
COUNT |
获得特定列具有值的行数,忽略NULL值;不能与DISTINCT联用 |
|
MAX |
最大值,与DISTINCT联用无意义 |
|
MIN |
最小值,与DISTINCT联用无意义 |
|
SUM |
求和 |
|
三角函数 |
ABS |
绝对值 |
COS |
余弦 |
|
EXP |
指数值 |
|
PI |
圆周率 |
|
SIN |
正弦 |
|
SQRT |
平方根 |
|
TAN |
正切 |
分组数据
Select name, count(*) AS num from table2 group by name; |
Group by 数据分组 |
Select name, count(*) AS num from table2 group by name having count(*) > 2; |
Having过滤分组 |
注:where对数据分组前进行过滤,having对分组数据进行过滤,使用时应与group by结合。不要依赖Group by排序数据。
数据联结
Select Product1.name, price From Product1, tettable Where Product1.name = tettable.name; |
Select后的子句不能为两个表中的共有字段,若是应加上表名 |
Select Product1.name, price From Product1 inner join tettable on Product1.name = tettable.name; |
SQL语言纯正论者建议使用此用法 |
Select Product1.name, price From Product1, product2, product3 Where Product1.name = Product2.name And product2.id = product3.id; |
联结多个表时,where后的子句使用and连接 |
Select Product1.name, price From Product1 as t1, product2 as t2, product3 as t3 Where t1.name = t2.name And t2.id = t3.id; |
使用别名构建联结 |
Select t1.id, t2.price From product as t1, product as t2 Where t1.name = ‘apple’ And t1.price = t2.price; |
自联结 |
Select t1.*, t2.price From product as t1, product as t2 Where t1.name = ‘apple’ And t1.price = t2.price; |
自然联结 |
Select t1.*, t2.price From product left outer join product ON t1.id = t2.id; |
左外连接 |
Select t1.*, t2.price From product right outer join product ON t1.id = t2.id; |
右外连接 |
注:无联结条件的表关系返回的是笛卡尔积,即检索到的行的数目是第一个表行数与第二表行数的乘积。联结的基本条件是:至少有一个列不止出现在一个表中。
组合查询
将两条select语句组合呈一条查询语句;是否使用组合,要根据具体的工作程序。
使用准则为:UNION关键词出现的次数比SELECT语句数少1;所有select语句后的列元素必须相同,表可以不同,where子句的查询条件不同;使用UNION是重复的行会被自动取消;只能使用一条order by子句,位置必须放在最后一个select子句后边。
Select * from product1 Union Select * from product2; |
同时显示出两个表的数据 |
Select * from product1 Union Select * from product2 Order by id; |
组合查询结果排序 |
数据增删改
Insert into tableName values(argList) |
严格按照参数序插入新数据 |
Insert into tableName(colName) values(argList) |
更安全的数据插入操作,colName与argList一一对应,顺序自由指定;避免表的结构发生变化引起数据插入错误;colName和argList可以是部分列,也可以是全部列; |
Insert into table1Name(colName) Select colName from table2; |
将表2的内容插入到表1中;注意主键不能重复;否则会插入失败。 |
Create table productCopy as Select * from product; |
表的复制 |
Update tableName Set memberEmail = ‘**@126.com’ Where memberId = ‘10010’; |
更新表的行(记录) |
Update tableName Set memberEmail = NULL Where memberId = ‘10010’; |
已更新形式将某个列的值设为NULL |
Update tableName Set memberEmail = ‘**@126.com’, memberAddress = ‘xian’ Where memberId = ‘10010’; |
更新多个列 |
Delete form tableName Where id = ‘10010’; |
删除某条记录 |
Truncate tableName; |
删除表格的所有记录 |
Drop table tableName; |
删除表格 |
注:除非要对每一行都要进行更新或删除,否则update和delete语句必须与where语句相结合;使用update和delete语句前,建议先用select语句进行过滤;SQL没有撤销按钮;不能将NULL值作为where子句后的列。
表的创建
Create table tableName (colName type NOT NULL, Prod_desc varchar(100) NOT NULL,…) |
每列定义顺序为:列名称à数据类型,各列以逗号隔开,整个定义语句放在圆括号中,varchar在MySQL中应定义为text,NOT NULL可以阻止没有值的列插入。 |
Create table tableName (vend_id char(10) NOT NULL, Vend_name char(50) NOT NULL, Vend_city char(10) , Vend_cnt integer NOT NULL DEFAULT 1, Vend date NOT NULL DEFAULT CURRENT_DATE(), Order integer not null primary, Id char(10) not null references table2(id), …) |
第三列允许是空值,前两列不允许空值,第四列不允许空值且在不指定值时默认值为1,第五列默认为当前日期;第六列设为主键,第七列设为外键 获取系统时间的函数如下: |
Alter table product2 add price char(20) not null; |
更新列(添加新的列) |
Alter table product2 Add constraint primary key(id); |
定义相同列的主键 |
Alter table product2 Add constraint foreign key(id) References table2(id); |
定义外键 |
Rename table oldName to newname; |
重命名表 |
视图的创建
Create view viewName as Select …; |
利用视图实现特定的检索目的,尽量创建可重用的视图 |
注:视图的用途,重新格式化数据、过滤想要的数据、结合计算字段一起使用。
存储过程
EXECUTE 存储过程 |
创建存储过程 |
START TRANSACTION … |
事务处理 |
ROLLBACK …. |
回退或撤销SQL语句 |
SAVEPOINT …. |
回退到某个保留点 |
DECLARE… |
创建游标 |
OPEN CURSOR …. |
使用游标 |
CLOSE CURSOR … |
关闭游标 |
注:为以后使用而保存的一条或多条SQL语句;Access和SQLite不支持存储过程;一般不允许编写存储过程,但是可以使用它。