




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
PHP重点-数据库知识-创建数据库 schoolcreate database school-删除数据库drop database schoolgo-可以派出先后执行顺序-更改当前操纵的数据库use schoolgo-创建教师表-主表create table teacher(class_namevarchar(20)primary key,tea_namevarchar(20)not null,t_timedatetimedefault(getdate()go-创建学生表-从表create table student(stu_idintidentity(1,1)primary key,stu_namevarchar(20)not null,sexchar(2)check(sex=男 or sex=女),scoreintdefault(0),class_namevarchar(20)foreign key references teacher(class_name)-class_name字段必须是teacher表的主键)go-添加语句insertinsert into teacher(class_name,tea_name)values(s123,张老师)insert into teacher values(s456,郭老师,2008-1-2 15:51:30)insert into teacher(class_name,tea_name)values(t123,李老师)insert into teacher(class_name,tea_name)values(t456,赵老师)goinsert into student(stu_name,sex,score,class_name)values(李刚,男,68,s123)insert into student(stu_name,sex,score,class_name)values(李小明,男,80,t123)insert into student(stu_name,sex,score,class_name)values(李大刚,男,97,t456)insert into student(stu_name,sex,score,class_name)values(王菲,女,100,t123)insert into student(stu_name,sex,score,class_name)values(赵刚,男,90,s123)insert into student(stu_name,sex,score,class_name)values(李宁,男,85,t123)insert into student(stu_name,sex,score,class_name)values(赵姗,女,56,s456)insert into student(stu_name,sex,score,class_name)values(郭蕊,女,78,t456)insert into student(stu_name,sex,score,class_name)values(孙小旭,男,60,s123)insert into student(stu_name,sex,score,class_name)values(孙津,女,72,t123)insert into student(stu_name,sex,score,class_name)values(丁磊,男,23,t456)-更新语句update student set score=80 where stu_name=孙津update student set score=81,class_name=t456 where stu_name=孙津-删除语句delete from student-清空表truncate table student-清空表 不走日志 快delete from student where stu_name=丁磊delete from student where stu_name=丁磊 and stu_name=孙津-查询语句select * from teacherselect * from studentselect * from student where stu_name=李刚select stu_name,score from student where stu_name=李刚select * from student where score!=60select * from student where score60select * from student where score60 and sex=女-排序 order byselect * from student order by score -升序select * from student order by score desc -降序select * from student order by stu_nameselect * from student order by stu_name,score-desc 降序排序的语法 select * from 表名 order by 字段名 +排序方式-聚合函数 sum、avg、max、min、count-所有学生成绩总和select sum(score) as 总和 from student-所有学生成绩平均分select avg(score) as 平均分 from student-s123班级所有学生成绩的平均分select avg(score) as 平均分 from student where class_name=s123-最高分select max(score) as 最高分 from student-最低分select min(score) as 最低分 from student-参加考试学生人数select count(*) as 总数 from studentselect count(*) as 总数 from student where score0-分组函数 group by 配合着聚合函数一起使用-注意:查询结果中出现的列,要么出现在聚合函数中,要么出现在group by中-所有男生和女生的平均分select avg(score)as 平均分,sex from student group by sex-查询所有学生的成绩总和,但是去掉不及格的学生成绩select sum(score) from student where score=60-查询每个班级所有学生的平均分select avg(score) from student group by class_name-查询班级的个数,平均分不小于60的班级的个数select count(*) as 学生人数,class_name from student group by class_name having avg(score)=60-注意:where、having后边都可以跟条件-where: 普通的条件查询-having: 一般都是配合着聚合函数、分组函数-模糊查询 like- % 代表任意长度的字符- _ 代表任意一个字符- 代表任意一个位置,可以是多个字符-查询所有姓李的学生select * from student where stu_name like 李%-查找所有姓李的学生,名字有两个字组成select * from student where stu_name like 李_-查找所有姓张和姓李的学生select * from student where stu_name like 李郭%-名字中代“小”select * from student where stu_name like %小%-多表查询-内联接 inner join.on (将多张表的字段合为一张表)-查询张老师班级所有的学生select * from student inner join teacher on student.class_name=teacher.class_name and teacher.tea_name=张老师-外联接 outer join.on-左外连接 left outer join.on (以左表为主)-查询张老师班级所有的学生select * from student s left outer join teacher tons.class_name=t.class_name and t.tea_name=张老师 -右外连接 right outer join.on (右表为主)select * from student s right outer join teacher tons.class_name=t.class_name and t.tea_name=张老师 -全外联接 full outer join.on select * from student s full outer join teacher tons.class_name=t.class_name and t.tea_name=张老师-外键的关键字foreign key references涉及外键的语法格式 字段名+类型+foreign key+ references +表名(字段)create table student(stu_id int primary key identity(1,1) not null,stu_name varchar(20) not null,sex char(2) not null,score float not null,class_name varchar(10) foreign key references teacher(class_name)-子查询 (在查询条件中还包含另外一个sql语句)select * from student where class_name=(select class_name from teacher where tea_name=张老师)select * from student where class_name in(select class_name from teacher where tea_name=张老师 or tea_name=李老师)select * from student where class_name !=(select class_name from teacher where tea_name=张老师)select * from student where class_name not in(select class_name from teacher where tea_name=张老师 or tea_name=李老师)-联合 union (把多张表的记录合为一张表)-去掉重复记录select class_name from studentunionselect class_name from teacher-所有记录全部显示select class_name from studentunion allselect class_name from teacher-多表查询 (相同的字段相等)select * from student,teacher where teacher.tea_name=张老师 and student.class_name=teacher.class_name-pubs案例数据库 图书作者sp_tables -显示该数据库所有的表名select * from authors -作者select * from titles -图书select * from titleauthor -图书作者-查询Johnson作者的所有的图书select a.au_id,b.title from authors a,titles b,titleauthor cwhere a.au_id=c.au_id and b.title_id=c.title_id and a.au_fname=Marjorie-northwind 订单查询sp_tablesselect * from customers -客户表select * from orders -订单表select * from products -产品表select * from order details -订单明细表-查询Alfreds Futterkiste订购的所有商品select a.CustomerID,c.ProductName from Customers a,Orders b,Products c,Order Details dwhere a.CustomerID=b.CustomerID and b.OrderID=d.OrderID andc.ProductID=d.ProductID and a.CompanyName=Alfreds Futterkiste-视图 view 视图一旦创建,始终存在 -表-创建视图create view myviewasselect a.CustomerID,c.ProductName from Customers a,Orders b,Products c,Order Details dwhere a.CustomerID=b.CustomerID and b.OrderID=d.OrderID andc.ProductID=d.ProductID and a.CompanyName=Alfreds Futterkistego-修改视图alter view myviewasselect * from ordersgo-删除视图drop view myviewselect * from myview where productName=Spegesild-触发器 trigger-创建create trigger mytrion studentfor deleteasselect * from studentgo-修改alter trigger mytrion studentfor insert,delete,updateasselect * from studentgo-删除drop trigger mytridelete from student where stu_id=1update student set score=100 where stu_name=李小明-存储过程 proc-创建create proc myprocasselect * from studentgoalter proc myproc stuId intasselect * from stude
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 仙居拓展活动方案
- 浙江省宁波市镇海区2023-2024学年四年级下学期数学期末试卷(含答案)
- 令牌抽奖活动方案
- 以自我为中心活动方案
- 仪态展示活动方案
- 任务激励活动方案
- 2024年贵州省高考历史真题试卷(含答案)
- 企业EAP活动方案
- 企业五四植树活动方案
- 企业党员示范岗活动方案
- 人教版六年级语文下册期末摸底考试及答案
- 中国大学mooc《分子生物学实验(北京师范大学) 》章节测试答案
- 化验室6S管理培训
- 教师口语艺术智慧树知到期末考试答案2024年
- 药品偏差处理程序
- 消防系统联动检测及检修方案
- 2024年国家能源集团宁夏煤业公司招聘笔试参考题库含答案解析
- 公立医院绩效考核微创手术目录(第2版)
- 华鲁恒升六定全员考试安全环保试题库1
- 老年人中常见的消化系统疾病及预防措施
- 钢琴音乐会的邀请函
评论
0/150
提交评论