常用SQL语句查询分享

--创建数据库(文件:主要数据文件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='李老师' ) ) );

时间: 2024-07-31 02:49:29

常用SQL语句查询分享的相关文章

常用SQL语句查询分享_MsSql

--创建数据库(文件:主要数据文件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 stud

MySQL中优化sql语句查询常用的30种方法

本篇文章是对MySQL中优化sql语句查询常用的30种方法进行了详细的分析介绍,需要的朋友参考下   1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 3.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如: select id from t where num is null 可以

DBA常用SQL语句

语句    自己总结的常用SQL语句,发现对自己工作帮助挺大的!   查看表空间的名称及大小: SQL>select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size from dba_tablespaces t, dba_data_files d where t.tablespace_name = d.tablespace_name group by t.tablespace_name;   查看表空间物理文件的名称及大小:

[SQL Server]管理常用SQL语句

server|语句 [SQL Server]管理常用SQL语句 1. 查看数据库的版本        select @@version 2. 查看数据库所在机器操作系统参数        exec master..xp_msver 3. 查看数据库启动的参数         sp_configure 4. 查看数据库启动时间         select convert(varchar(30),login_time,120) from master..sysprocesses where spi

php通过odbc用sql语句查询时无法查询中文,应该怎么转码?

问题描述 php通过odbc用sql语句查询时无法查询中文,应该怎么转码? 数据库是informix,编码是iso-8859-1,php编码是gbk.从网上找了各种方式转换,都不行.代码如下,其中$licenseno是车牌号,中间有省份缩写是汉字. $sql.=""and b.licenseno = """".$licenseno.""""""; 会返回错误如下: Warning: od

Oracle常用sql语句

Oracle数据库常用sql语句 ORACLE 常用的SQL语法和数据对象 一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, --) VALUES ( 值1, 值2, --); INSERT INTO 表名(字段名1, 字段名2, --) SELECT (字段名1, 字段名2, --) FROM 另外的表名; 字符串类型的字段值必须用单引号括起来, 例如: 'GOOD DAY' 如果字段值里包含单引号' 需要

oracle-Oracle中如何只用一条sql语句查询下面的例子

问题描述 Oracle中如何只用一条sql语句查询下面的例子 解决方案 select_statement UNION [ALL] selectstatement [UNION [ALL] selectstatement][-n]其中selectstatement为待联合的SELECT查询语句.用联合查询足以 解决方案二: 这个只能在后台代码判断并组装语句,然后联表查询 解决方案三: Oracle SQL语句查询例子用一条SQL语句查询分组前三名数据常用的sql语句查询例子

mysql命令-常用sql语句命令代码

mysql命令-常用sql语句命令代码 使用G按行垂直显示结果 如果一行很长,需要这行显示的话,看起结果来就非常的难受.在SQL语句或者命令后使用G而不是分号结尾,可以将每一行的值垂直输出.这个可能也是大家对于MySQL最熟悉的区别于其他数据库工具的一个特性了. mysql> select * from db_archivelogG *************************** 1. row ***************************         id: 1  chec

oracle常用sql语句_oracle

正在看的ORACLE教程是:oracle常用sql语句.SQL*Plus system/manager 2.显示当前连接用户 SQL> show user 3.查看系统拥有哪些用户 SQL> select * from all_users; 4.新建用户并授权 SQL> create user a identified by a;(默认建在SYSTEM表空间下) SQL> grant connect,resource to a; 5.连接到新用户 SQL> conn a/a