sql delete同时删除多表相关联记录
2019/10/10/17:32:52 阅读:1900 来源:谷歌SEO算法 标签:
SEO优化
sql delete同时删除多表相关联记录,sqlserver 支持级联更新和删除,oracle 只支持级联删除.
删除包含主键值的行的操作,该值由其它表的现有行中的外键列引用,在级联删除中,还删除其外键值引用删除的主键值的所有行,如:
- createdatabasetemp
- go
- usetemp
- go
- createtableUserInfo
- (
- UserIdintidentity(1,1)primarykey,
- UserNamevarchar(20),--用户名
- passwordvarchar(20)notnull--密码
- )
- createtableUserDetails
- (
- idintidentity(1,1)primarykey,
- namevarchar(50)notnull,--真实姓名
- userIdint,
- foreignkey(userId)referencesUserInfo(UserId)ondeletecascade
- )
- insertUserInfovalues('ly','jeff')
- insertUserInfovalues('wzq','wzqwzq')
- insertUserInfovalues('lg','lglg')
- insertUserDetailsvalues('李四',1)
- insertUserDetailsvalues('王五',2)
- insertUserDetailsvalues('刘六',3)
- --phpfensi.com
- altertable表名
- addconstraint外键名
- foreignkey(字段名)references主表名(字段名)
- ondeletecascade
语法:
- ForeignKey
- (column[,...n])
- referencesreferenced_table_name[(ref_column[,...n])]
- [ondeletecascade]
- [onupdatecascade]
注释:
column:列名
referenced_table_name:外键参考的主键表名称
ref_name:外键要参考的表的主键列
on delete:删除级联
on update:更新级联
000,请您对文章做出评价)此时:Delete From UserInfo Where UserId = 1 就可删除UserInfo表和UserDetails表的UserId=1 的内容
看一下MySql数据库教程怎么操作多表删除呢,有时我们可以直接用delete 来删除.
delete 语法:
- DELETE[LOW_PRIORITY][QUICK]FROMtable_name
- [WHEREwhere_definition]
- [ORDERBY...]
- [LIMITrows]
- or
- DELETE[LOW_PRIORITY][QUICK]table_name[.*][,table_name[.*]...]
- FROMtable-references
- [WHEREwhere_definition]
- --phpfensi.com
- or
- DELETE[LOW_PRIORITY][QUICK]
- FROMtable_name[.*][,table_name[.*]...]
- USINGtable-references
- [WHEREwhere_definition]
示例代码为:
1.删除一个表中的数据
delete from department where name='Asset Management';
2.删除两个表中的数据
- deleteemployee,employeeSkills
- fromemployee,employeeSkills,department
- whereemployee.employeeID=employeeSkills.employeeID
- andemployee.departmentID=department.departmentID
- anddepartment.name='Finance';
3.删除两个表中的数据,用using语法
- deletefromemployee,employeeSkills
- usingemployee,employeeSkills,department
- whereemployee.employeeID=employeeSkills.employeeID
- andemployee.departmentID=department.departmentID
- anddepartment.name='Finance';
热门评论