SQL

一、注意点

1. 约束

SQL 约束CONSTRAINT用于规定表中的数据规则。如果存在违反约束的数据行为,行为会被约束终止。

约束可以在创建表时规定(通过 CREATE TABLE 语句),或者在表创建之后规定(通过 ALTER TABLE 语句)。

SQL中约束:1)NOT NULL;2)UNIQUE ;3)PRIMARY KEY - NOT NULL 和 UNIQUE 的结合。

4)FOREIGN KEY - 保证一个表中的数据匹配另一个表中的值的参照完整性。

5)CHECK - 保证列中的值符合指定的条件。6)DEFAULT 

2. 外键

一个表中的 FOREIGN KEY 指向另一个表中的 UNIQUE KEY(唯一约束的键)。

FOREIGN KEY 约束也能防止非法数据插入外键列,因为它必须是它指向的那个表中的值之一。

ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)

3. 仅删除表数据保留表结构

如果我们仅仅需要删除表内的数据,但并不删除表本身,那么我们该如何做呢?

请使用 TRUNCATE TABLE 语句:TRUNCATE TABLE table_name

4. 通配符

% 替代0个或多个字符

_ 替代一个字符

[charlist] 字符列中任何单一字符

[^charlist] 或 [!charlist]  不在字符列中的任何单一字符

5. join

INNER JOIN:如果表中有至少一个匹配,则返回行

LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行

RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行

FULL JOIN:只要其中一个表中存在匹配,则返回行

A inner join B ---- 取交集。

A left join B ---- 取 A 全部,B 没有对应的值为 null。

A right join B ---- 取 B 全部 A 没有对应的值为 null。

A full outer join B ---- 取并集,彼此没有对应的值为 null。

对应条件在 on 后面填写。

6. 事务

MYSQL 事务处理主要有两种方法:

1)用 BEGIN, ROLLBACK, COMMIT来实现

BEGIN 开始一个事务

ROLLBACK 事务回滚

COMMIT 事务确认

2)直接用 SET 来改变 MySQL 的自动提交模式:

SET AUTOCOMMIT=0 禁止自动提交

SET AUTOCOMMIT=1 开启自动提交

二、应用

1.创建sql表
create table student(id integer primary key, name text, score integer);

2.插入一条记录
insert into student(score, id, name) values(100, 1, 'XiaoMing');
insert into student values(2, "XiaoZhang", 90);
//主键没有的给的话,会自动分配一个给你的记录,其他没有not null约束的字段你没有提供的话,默认是可以为空(null)的
insert into student(name) values("XiaoLiu");

3.简单的查询语句
select id, name from student;
select * from student;

4.修改一条记录(where子句很重要,没有where则修改所有记录)
update student set score=80, name="XiaoWu" where id=3;

5.删除一条记录
delete from student; //没有where子句删除所有记录
delete from student where id=3;

6.数据的批量导入
这不是SQL语句,是sqlite3工具的一条命令
.import 1.txt student

7.修改表的结构
alter table student add score2 integer;
可以使用命令.schema student查看student表结构。
alter table student rename to newstudent;修改表名
但是不支持修改某一个现有字段。(没有modify操作)

8.备份一张表的内容(备份表内容,但是表结构可能有差异)
备份student表的所有内容到新的表newstudent
create table newstudent as select * from student;
备份student表的头三列到新的表newstudent中
create table newstudent as select id, name, score from student;

9.删除表
drop table student;删除student表

10.复杂的查询语句
select * from student where score>80;查询成绩大于80分的同学
select * from student where score>87 and score<100;
select * from student where score between 87 and 100;
where score between 87 and 100;
等价于 where score>=87 and score<=100;

模糊查询
select * from student where score like "9%";
select * from student where name like "%g";
select * from student where score like "87";等价于select * from student where score=87;

排序输出
select * from student order by score desc; 降序
select * from student order by score asc;升序
order by默认是升序排列

找80分以上成绩最低的两位学员:
select * from student where score>=80 order by score asc limit 2;

找班上成绩第三名的同学:
select * from student order by score desc limit 1 offset 2;

找班上成绩最高的一位或几位同学:
select * from student where score=(select score from student order by score desc limit 1);

group by子句(having是group by的条件子句)
select dep, sum(salory) from employee where salory>4000 group by dep; //按部门列出每个月每个部门所发薪水总和
select name from employee group by name, salory, dep having count(*)>1;//求出出现重复录入的数据的人的姓名

连接两张表的内容:
sqlite> select * from student;
1|XiaoMing|21
2|XiaoZhang|22
3|XiaoWu|19
sqlite> select * from score;
1|100
2|96

1.where子句连接两张表
select a.id, a.name, a.age, b.score from student a, score b where a.id=b.id;
1|XiaoMing|21|100
2|XiaoZhang|22|96

2.自然连接(要求两张表中要有相同名字的字段,字段值相同的记录被连接到一起输出)
select id, name, age, score from student natural join score;
1|XiaoMing|21|100
2|XiaoZhang|22|96
如果两张表中没有相同名字的字段(student的id,score的id名字相同),连接不能成功,输出两张表的笛卡尔积
select id, name, age, nid, score from student natural join newscore;
1|XiaoMing|21|1|100
1|XiaoMing|21|2|96
2|XiaoZhang|22|1|100
2|XiaoZhang|22|2|96
3|XiaoWu|19|1|100
3|XiaoWu|19|2|96

左外连接(左边的表中,即使在右边表内没有连接成功的项也会输出。)
select a.id, name, age, score from student a left outer join score b on a.id=b.id;
1|XiaoMing|21|100
2|XiaoZhang|22|96
3|XiaoWu|19|     =>这一项因为左外连接而输出,注意和下面的比较

select a.id, name, age, score from score b left outer join student a on a.id=b.id;
1|XiaoMing|21|100
2|XiaoZhang|22|96

 

参考:

1. MySQL(学生表、教师表、课程表、成绩表)多表查询

2. SQL 约束(Constraints) runoob

posted @ 2015-12-17 20:40  yuxi_o  阅读(290)  评论(0编辑  收藏  举报