1. SQL -- 创建数据库和表

(1)     创建数据库和相应的表

create database student  --创建数据库

go

use student

go

-----------------------------创建学生表------------------------

if object_id('student_table') is not null

drop table student_table

create table student_table

(

studentID varchar(20) primary key,

sname varchar(20) not null,

sex varchar(2),

birthday datetime default getdate()

)

go

insert into student_table values('101','王五','','')

insert into student_table values('102','李四','','')

go

select * from student_table

-------------------------------------创建课程表------------------------

go

if object_id('course_table') is not null

drop table course_table

create table course_table

(

courseID varchar(20) primary key,

cname varchar(20) not null,

)

go

insert into course_table values('001','C语言')

insert into course_table values('002','数据结构')

go

select * from course_table

--------------------------------------创建学生课程表--------------------------------

go

if object_id('student_course_table') is not null

drop table student_course_table

create table student_course_table

(

studentID varchar(20) foreign key references student_table(studentID),

courseID varchar(20) foreign key references course_table(courseID),

grade int

)

go

insert into student_course_table values('101','001',67)

insert into student_course_table values('101','002',77)

insert into student_course_table values('102','001',97)

insert into student_course_table values('102','002',57)

go

select * from student_course_table


本文出自 “Ricky's Blog” 博客,转载请与作者联系!

你可能感兴趣的:(database,primary,insert,create,default)