常用SQL语句查询分享

作者:hebedich 时间:2024-01-12 16:13:11 

--创建数据库(文件:主要数据文件mdf==1,次要数据文件ndf>=0,日志文件ldf>=1)
--文件组:当1mdf,5个ndf(1,2,2),10个ldf(3,3,4),将它们分成多个组存放

CREATE database studb;

--创建表teacher,student


create table teacher
(
tid int(10) primary key auto_increment,
tname varchar(20),
tage int(10)
);
use studb;

create table student
(
sid int(10) primary key auto_increment,
sname varchar(20),
sage int(10),
tid int(10) REFERENCES teacher(tid)
);

--外键约束:你问张三的老师是谁??


--select teacher.tname from teacher,student where student.sname = '张三'
select t.tname from teacher t,student s where s.sname = '张三' and t.tid = s.tid

--创建课程表


create table course
(
cid int(10) primary key,
cname varchar(20),
tid int(10) REFERENCES teacher(tid)
);

--创建分数表


create table sc
(
scid int(10) primary key,
sid int(10) REFERENCES student(sid),
cid int(10) REFERENCES course(cid),
score int(10)
);

--联合查询:等值查询
--1..


select c.cname from course c,student s,sc where s.sname = '小张'
and s.sid = sc.sid and c.cid = sc.cid;

--2..


select sname from student s,course c,sc where c.cname='android' and sc.score>=60
and s.sid = sc.sid and c.cid = sc.cid;

--3..
--子查询:当条件也要查询的时候,我只知道学号,我不知道"小张"这个字段,那你知道小张的学号吗


delete from sc where sid = (select sid from student where sname = '小张');

--子查询中间的符号一定是父查询与子查询两张表关联的字段(一般是主外键)

--4..


update sc set score=score+5 where cid=????;

select tid from teacher where tname='李老师' ==1
select cname from course where tid = 1 ==课程名字,李老师教的
select cid from course where cname='android' ==课程ID
update sc set score=score+5 where cid=
(
select cid from course where cname=
(
select cname from course where tid =
(
select tid from teacher where tname='李老师'
)
)
);
标签:SQL语句查询
0
投稿

猜你喜欢

  • 解决Server对象 错误 'ASP 0178 : 80070005'

    2008-02-29 13:45:00
  • SQL2008的sa账户被禁用其他账户无法连接的快速解决方法

    2024-01-17 05:12:53
  • opencv python 图像轮廓/检测轮廓/绘制轮廓的方法

    2022-08-13 06:54:59
  • 在vant中如何使用dialog弹窗

    2024-05-22 10:41:50
  • 动态程序防采集的新方法

    2010-04-24 15:24:00
  • Golang打包配置文件的实现示例

    2024-01-30 17:23:25
  • Bootstrap编写一个兼容主流浏览器的受众巨幕式风格页面

    2024-05-02 17:31:43
  • 学习ASP.NET八天入门:第一天

    2007-08-07 13:08:00
  • Python中使用语句导入模块或包的机制研究

    2023-02-21 15:06:40
  • Python fileinput模块使用实例

    2022-02-28 12:02:19
  • python实现创建新列表和新字典,并使元素及键值对全部变成小写

    2023-12-30 00:24:26
  • c#编写的高并发数据库控制访问代码

    2024-01-16 04:57:04
  • ODBC连接数据库以SQLserver为例图文详解

    2024-01-13 04:07:43
  • 浅析Golang切片截取功能与C++的vector区别

    2024-04-23 09:34:51
  • python抽象基类用法实例分析

    2021-03-04 11:06:25
  • Python pandas自定义函数的使用方法示例

    2022-05-13 02:38:35
  • Python网络编程之xmlrpc模块

    2023-04-06 15:19:24
  • python Django连接MySQL数据库做增删改查

    2023-11-14 10:44:35
  • Python之str操作方法(详解)

    2021-07-21 09:14:16
  • Python 微信公众号文章爬取的示例代码

    2021-11-06 22:39:20
  • asp之家 网络编程 m.aspxhome.com