码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-10-28 12:56:05      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:style   class   alter   creat   primary   student   数学   info   ima   

--创建数据库
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)

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

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

标签:style   class   alter   creat   primary   student   数学   info   ima   

原文地址:http://www.cnblogs.com/TheJoker/p/7746131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!