在SQL Server 2000中,有教师表Teachers(TeacherID,Name,LeaderID),其中TeacherID是主码,类型是长度为4的普通编码定长字符串,且每位是0~9的数字字符;Name的类型是长度为10的普通编码可变长字符串;LeaderID是每个教师的领导的TeacherID。
①写出创建Teachers表的SQL语句,要求语句中包含所有的约束。
②现要查询TeacherID为“1234”的教师的领导的TeacherID,请给出相应的SQL语句,要求只使用一条SQL语句实现,且此语句中不允许包含子查询。
答案说明:本题目答案来自网络整理或转载,最终答案请以官网为准。
答 案:①create table Teachers( TeacherID char(4)check(TeacherID like" [0~9][0~9][0~9][0~9]") Name varchar(10) LeaderID char(4) primary key TeacherID foreign key LeaderlD references Teachers (TeacherID) ) ②select T2. LeaderID from Teachers as T1 join Teachers as T2 on T1.LeaderID=T2.TeacherID where T1.TeacherID=‘1234’
①create table Teachers( TeacherID char(4)check(TeacherID like" [0~9][0~9][0~9][0~9]"), Name varchar(10), LeaderID char(4), primary key TeacherID, foreign key LeaderlD references Teachers (TeacherID) ) ②select T2. LeaderID from Teachers as T1 join Teachers as T2 on T1.LeaderID=T2.TeacherID where T1.TeacherID=‘1234’