建数据库
create database school
--建表
use school
create table students
(
s_ID int IDentity(1,1) primary key,--设主键,为自增ID
s_name varchar(20) not null,
s_classID int
)
create table class
(
c_ID int IDentity(1,--设主键,为自增ID
c_classname varchar(20) not null
)
create table student_score
(
ss_ID int IDentity(1,--设主键,为自增ID
ss_score varchar(200) not null,
s_ID int
)
--设外键
use school
alter table students add constraint FK_class_studets foreign key(s_classID)references class(c_ID)
--设外键
use school
alter table student_score add constraint FK_students_student_score foreign key(s_ID)references students(s_ID)
--插class数据
insert into class(c_classname) values('08ACCP1班')
insert into class(c_classname) values('08ACCP2班')
insert into class(c_classname) values('08ACCP3班')
insert into class(c_classname) values('08ACCP4班')
insert into class(c_classname) values('08网编1班')
insert into class(c_classname) values('08网编2班')
insert into class(c_classname) values('08软开1班')
insert into class(c_classname) values('08软开2班')
insert into class(c_classname) values('08软开3班')
insert into class(c_classname) values('08信管1班')
--插studets数据
insert into students(s_name,s_classID) values('陈志锦',2)
insert into students(s_name,s_classID) values('何金喜',4)
insert into students(s_name,s_classID) values('黄瑞驰',5)
--单表查询
--查studets表
select * from students
--查class表
select * from class
--多表查询
--内联接查询
--情况1:
select * from students as s,class as c where s.s_classID=c.c_ID
--情况2:
select * from students as s inner join class as c on(s.s_classID=c.c_ID)
--外联接查询
--左外联接
select * from students as s left outer join class as c on s.s_classID=c.c_ID
select * from class as c left outer join students as s on s.s_classID=c.c_ID
--右外联接
select * from students as s right outer join class as c on s.s_classID=c.c_ID
select * from class as c right outer join students as s on s.s_classID=c.c_ID
--全联接
select * from students as s full outer join class as c on s.s_classID=c.c_ID
select * from class as c full outer join students as s on s.s_classID=c.c_ID
--交叉查询
SELECT * FROM students CROSS JOIN class
SELECT * FROM class CROSS JOIN students
--触发器
/*-------触发器------*/
/*触发器是一种特殊的存储过程,
触发器是在对表进行插入、更新或删除 *** 作时自动执行的存储过程
触发器通常用于强制业务规则
触发器是一种高级约束,可以定义比用CHECK 约束更为复杂的约束
可执行复杂的SQL语句(if/while/case)
可引用其它表中的列
*/
/*
DELETE 触发器
INSERT 触发器
UPDATE 触发器
*/
/*----------Insert 插入 触发器------------*/
Create Trigger Tri_insert
on students /*针对某个表,触发器是建立在表关系上的*/
for insert /*采用的是哪种触发器*/
as
declare @stu_ID int;
declare @stu_score varchar(200);
select @stu_ID=s_ID from inserted /*这里的Inserted 是在创建触发器时候 系统自动创建的内存表*/
insert into student_score(s_ID,ss_score)values(@stu_ID,'100')
go
--就是插一条数据进入students中,Tri_insert触发器就会自动在student_score插入相关的学生的一条分数的数据
insert into students(s_name,s_classID) values('黄驰',6)
select * from student_score
--存储过程
/*
存储过程
1.不带参数的存储过程
2.带参数的存储过程
*/
--不带参数的存储过程
create procedure pro_selStu
as
select * from students;
go
--下面是测试存储过程
execute pro_selStu;
--带参数的存储过程
/*(1)带输入参数的存储过程 */
Create procedure pro_stu @stu_ID int
as
select * from students where s_id=@stu_id
go
--下面是测试存储过程
execute pro_stu 1;
/*(2)带输出参数的存储过程*/
Create procedure pro_stu_output @stu_sum int output,@stu_down_count int,@stu_up_count int
as
select @stu_sum=count(*) from students where s_ID between @stu_down_count and @stu_up_count
go
--下面是测试存储过程
declare @stu_sum int
execute pro_stu_output @stu_sum output,1,4 /*在调用含有输出参数的存储过程时 要注意*/
print @stu_sum
带参数的存储过程在Java中的应用:
/**
create procedure ShipVia_Sum
@via int,
@sum money output
as
select @sum = sum(freight) from orders
where shipVia = @via
**/
public static voID procedureOutput() { try { Connection conn = JDBC_ODBC.getConnection(); System.out.println( "conn is: " + conn); String call = "{call ShipVia_Sum(?,?)} "; CallableStatement cs = conn.prepareCall(call); cs.setInt(1, 1); cs.registerOutParameter(2, java.sql.Types.DOUBLE); cs.execute(); System.out.println(cs.getDouble(2)); } catch (Exception ex) { ex.printstacktrace(); } }
以上是内存溢出为你收集整理的SQLServer创建表,插入数据,查询,创建触发器,存储过程全部内容,希望文章能够帮你解决SQLServer创建表,插入数据,查询,创建触发器,存储过程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)