• SQL创建数据库、建表、填入内容


    --创建数据库
    create database Information
    
    go
    
    --使用数据库
    use Information
    
    go
    
    --创建表
    create table Student
    (
     Sno nvarchar(50) primary key not null,
     Sname nvarchar(50) not null,
     Ssex bit not null,
     Sbirthday datetime,
     Class nvarchar(50),
    )
    
    create table Course
    (
        Cno nvarchar(50) primary key not null,
        Cname nvarchar(50) not null,
        Tno nvarchar(50) not null,
    )
    
    create table Score
    (
        Sno nvarchar(50) not null,
        Cno nvarchar(50) not null,
        Degree decimal(4,1),
    )
    
    create table Teacher
    (
        Tno nvarchar(50) primary key not null,
        Tname nvarchar(50) not null,
        Tsex bit not null,
        Tbirthday datetime,
        Prof nvarchar(50),
        Depart nvarchar(50) not null,
    )
    
    
    --填入数据  Student
    insert into Student values('108','曾华','1','1977-09-01','95033')
    insert into Student values('105','匡明','1','1975-10-02','95031')
    insert into Student values('107','王丽','0','1976-01-23','95033')
    insert into Student values('101','李军','1','1976-02-20','95033')
    insert into Student values('109','王芳','0','1975-02-10','95031')
    insert into Student values('103','陆君','1','1974-06-03','95031')
    
    
    --填入数据  Course
    insert into Course values('3-105','计算机导论','825')
    insert into Course values('3-245','操作系统','804')
    insert into Course values('6-166','数字电路','856')
    insert into Course values('9-888','高等数学','831')
    
    
    --填入数据  Score
    insert into Score values('103','3-245','86')
    insert into Score values('105','3-245','75')
    insert into Score values('109','3-245','68')
    insert into Score values('103','3-105','92')
    insert into Score values('105','3-105','88')
    insert into Score values('109','3-105','76')
    insert into Score values('101','3-105','64')
    insert into Score values('107','3-105','91')
    insert into Score values('108','3-105','78')
    insert into Score values('101','6-166','85')
    insert into Score values('107','6-166','79')
    insert into Score values('108','6-166','81')
    
    
    --填入数据 Teacher
    insert into Teacher values('804','李诚','1','1958-12-02','副教授','计算机系')
    insert into Teacher values('856','张旭','1','1969-03-12','讲师','电子工程系')
    insert into Teacher values('825','王萍','0','1972-05-05','助教','计算机系')
    insert into Teacher values('831','刘冰','0','1977-08-14','助教','电子工程系')
    
    
    
    --主外键关系
    --如表A中的Ids是主键,要约束表B中的Aid列,那么语句应该是:
    --alter table B add constraint A_B_Ids foreign key(Aid)  references A(Ids) 
    
    
    
    --Student 中的Sno    约束      Score  中的  Sno
    alter table Score add constraint Student_Score_Sno foreign key(Sno) references Student(Sno)
    
    
    --Course 中的 Cno     约束     Score  中的  Cno
    alter table Score add constraint Course_Score_Cno foreign key(Cno) references Course(Cno)
    
    
    --Teacher 中的 Tno     约束     Course  中的   Tno
    alter table Course add constraint Teacher_Course_Tno foreign key(Tno) references Teacher(Tno)

    创建好数据库,建表,填入内容后准备开始练习

  • 相关阅读:
    UVa 10055
    UVa 401
    c++中文件应用的一点小变化
    poj2136
    UVa 494
    一台电脑接两个显示器,双屏显示介绍zz
    学习jquery合集
    解决Windows下MinGW显示乱码zz
    QWS_MOUSE_PROTO该如何写
    Qt/e中鼠标设备分析
  • 原文地址:https://www.cnblogs.com/TheJoker/p/7746131.html
Copyright © 2020-2023  润新知