两个关联表插入一条数据

friendoyc 2007-05-10 11:01:18
create table students(no number(4),name varchar(10),sex varchar(4) age number(4));
create table detail(no number(4),subject varchar(10),score number(4));

其中no是学号,关联两个表,性别有两个字段m和f,科目有三个字段chinese,english,math,现在要插入一条新的学生信息。

姓名是张三,男,15岁,学号是21,考试成绩为语文80,外语60,数学90。

最后在删除这条字段,请问如何使用一条sql语句来实现这两个要求?
...全文
395 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gahade 2007-05-10
  • 打赏
  • 举报
回复
用exec行不?
exec('insert into students
select 21,''张三'',''男'',15
insert into detail
select 21,''语文'',80
union all select 21,''外语'',60
union all select 21,''数学'',90')

exec('delete from students where no=21
delete from detail where no=21')
OracleRoob 2007-05-10
  • 打赏
  • 举报
回复
不能。
friendoyc 2007-05-10
  • 打赏
  • 举报
回复
谢谢,使用一条纯sql语句不能做到吗?
gahade 2007-05-10
  • 打赏
  • 举报
回复
insert into students
select 21,'张三','男',15

insert into detail
select 21,'语文',80
union all select 21,'外语',60
union all select 21,'数学',90

GO
create trigger td_students on students
for delete
as
delete from detail where no in (select no from deleted)
GO

delete from students where no=21


如果必须使用一条语句插入或删除的话,做个存储过程,只需执行一下存储过程就可以了.
OracleRoob 2007-05-10
  • 打赏
  • 举报
回复
使用触发器固然可以实现自动追加到表中。

但没有什么现实意义。
friendoyc 2007-05-10
  • 打赏
  • 举报
回复
是写两条语句,第一条语句是插入新记录,第二条是删除这条记录。
gahade 2007-05-10
  • 打赏
  • 举报
回复
使用触发器后再写一条语句可以.
OracleRoob 2007-05-10
  • 打赏
  • 举报
回复
一条语句无法追加两条记录到两个表中。

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧