关键字使用
show databasesuse dmshow tables-- 需要两张表,再建一张stu表-- 手动建,比较快select * from class_fSELECT * from stu# 关键字的使用-- in-- in 满足查询子集中的某一个即可select * from class_f WHERE age not in(20,22)-- in 后面的子集也可通过SQL语句查询结果select * from stu where cidin(select cid from class_f where age in (20,22));-- any some all 三个关键字-- 三个与in类似,查询是否满足后面的子集中的条件-- 但是,关键字后面不允许写固定值 只允许写SQL语句(只能通过嵌套获取子集)-- >any 满足查询子集中的某一个即可 >any <any =any(结果与in) !=anyselect * from class_f where cid > 2;select * from stu where cid >any(select cid from class_f where cid > 2);-- some 与any完全一致-- all 满足子集中的全部才可以select * from stu where cid >all (select cid from class_f where cid > 2);select * from stuwhere cid <all(select cid from class_f where cid > 2);
集合操作
-- 集合操作-- 并集(union)-- 1.要求前后两个查询子集的列数是一致的-- 2.类型没有要求-- 3.拼接后显示的列名是前一个子集默认的列名-- 4.union与union all的区别 建议使用union all-- union合并后做去重复的处理 性能慢,出现重复元素,记录首次出现那一行-- union all 将两个查询的字节直接做合并 不做任何处理 性能比较快create table newclass_f as select * from class_f;show TABLESSELECT * FROM newclass_fselect * from class_f union select * from newclass_f;select * from class_f union all select * from newclass_f;
主键,外键
表与表之间的关系

-- 1.主键约束-- 每一个表格内 只能有一个列被设置为主键约束-- 主键约束通常是用来标记表格中的数据的唯一存在-- 主键约束要求当前的列 不能为null 值-- 主键约束要求当前的列 值是唯一存在的 不能重复select * from stu-- 添加主键-- constraint 约束-- 添加主键修改列,DDL数据定义语言(create创建 drop删除 alter修改)-- alter table 表名 add constraint 约束名 约束类型(列)-- 简写 alter table 表名 add primary key(classid);alter table stu add constraint pk_stu primary key(sid)-- desc 表名;描述表格信息desc stu-- 描述 添加主键约束后表格信息show keys from stu-- 添加主键并自增alter table class_f modify cid int(4) auto_increment;alter table class_f change cid cid int(4) auto_increment;-- 没有起始值的说明 主键列的值会从一开始alter table class_f auto_increment = 10;-- 从十开始alter table class_f modify cid int(4);-- 取消自增alter table class_f drop primary key; -- 删除主键-- 注意:删除主键约束以后 不重复的特性取消了 非空特性还在alter table class_f modify cid int(4) null;-- 设置为空-- 唯一约束(unique[key])-- 可以为表格中的某一个列添加唯一约束,与主键约束类似-- 唯一约束 表示的是列的值不能重复,可以为空-- 唯一约束 在表格中可以存在多个列-- alter table 表名 add constraint 约束名 约束类型(列);alter table class_f add constraint uk_class_f unique key(score);-- 可以简写-- 删除唯一约束 alter table 表名 drop index 约束名;alter table class_f drop index uk_class_f;-- 非空约束(not null)-- 在表格的某一个列上添加非空约束,当前列不能为空not null-- alter table 表名 modify 原列名 原类型 原长度 [not] null;-- alter table 表名 change 原列名 新列名 新类型 新长度alter table class_f modify age varchar(10) not null;alter table class_f modify age varchar(10) null;-- 相当于修改为默认null-- 不能为空时,不想添加数据,但想给个默认值 default关键字alter table class_f modify age varchar(10) not null default 'man';-- 外键约束(foreign key)-- 表格中可以有多个列被设置为外键-- 当前列值可以为空 可以重复-- 当前列的值不能随便填写 值去另一张表格内去寻找-- ***外键是当前列的值受到另外一张表格某一个列的影响-- 另外一张表格的列 是唯一的约束(主键 唯一)-- 添加外键约束# alter table 表名 add constraint fk_当前表_关联表 foreign key(列) references 另一个表(列);alter table stu add constraint fk_stu_class_f foreign key(cid) references class_f(cid);-- 简写-- alter table 表名 add foreign key(列) references 另一个表(列);-- 删除外键约束-- alter table 表名字 drop foreign key 约束名字;alter table stu drop foreign key fk_stu_class_f;alter table stu drop key fk_stu_class_f;-- 将生成的key删除-- 通过上述语句已经删除外键约束,自动在当前表格添加一个新的key-- 需要再次手动将生成的key删除,外键约束才算真正删除干净-- 查看表的信息-- show keys from 表;-- desc 表;-- show create table 表名;
联合查询
-- 表格之间联合查询-- 广义笛卡尔积 select * from stu,class_f;-- 等值连接 select * from stu,class_f where stu.cid=class_f.cid; > < =都行-- 广义笛卡尔积 将两张或多张表格进行无条件的拼接 进行where筛选 ;-- 任何表格都可以拼接-- 外连接-- 外连接最早的目的是替代笛卡尔积,查询效率快-- select * from A left/right [outer] join B on 条件-- 1.两张表格A和B 取决于谁的数据在左边显示-- A表格先出现 A左边显示 B表格后出现 B右边显示-- 2.left和right 来控制以哪一个表格的数据作为基准-- 作为基准的表格数据必须全部显示出来 非基准的表格按照on条件与之拼接-- 若找到条件拼接 则正常显示 若找不到满足条件的 则 nullselect * from stu s left outer join class_f cf on s.cid = cf.cid;-- 内连接(自连接)select * from A inner join B on 条件;-- 可以在当前表格中再次查询当前表格的信息select * from stu s inner join class_f cf on s.cid = cf.cid;
文章转载自运维小白超凡奇才,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




