SQL语句的种类_外键_表连接(内连接和左外连接)

数据定义语句(DDL:Data Definition Language)
包括create和drop等操作
在数据库中创建新表或删除表(create table或 drop table)

数据操作语句(DML:Data Manipulation Language)
包括insert、update、delete等操作
上面的3种操作分别用于添加、修改、删除表中的数据

数据查询语句(DQL:Data Query Language)
可以用于查询获得表中的数据
关键字select是DQL(也是所有SQL)用得最多的操作
其他DQL常用的关键字有where,order by,group by和having

外键:

利用外键约束可以用来建立表与表之间的联系
外键的一般情况是:一张表的某个字段,引用着另一张表的主键字段

新建一个外键
create table t_student (id integer primary key autoincrement, name text, age integer, class_id integer, constraint fk_t_student_class_id_t_class_id foreign key (class_id) (id)) ; references t_class 
t_student表中有一个叫做fk_t_student_class_id_t_class_id的外键
这个外键的作用是用t_student表中的class_id字段引用t_class表的id字段

表连接(内连接和左外连接):

什么是表连接查询
需要联合多张表才能查到想要的数据

表连接的类型
内连接:inner join 或者 join  (显示的是左右表都有完整字段值的记录)
左外连接:left outer join (保证左表数据的完整性)

示例
查询0316iOS班的所有学生
select s.name,s.age from t_student s, t_class c where s.class_id = c.id and c.name = ‘0316iOS’;

左外连接:

select * from t_student s left outer join t_class c where s.class_id=c.id
select * from t_class c  left outer join t_student s where s.class_id=c.id

 内连接:

//内连接 内连接一定是显示两张表共同符合条件的数据
select s.name sname ,c.name cname from t_student s inner join t_class c on s.class_id=c.id and c.name='ios_1';

 

posted @ 2016-02-24 09:35  张凯泽  阅读(1447)  评论(0编辑  收藏  举报