【HNU数据库】实验1 数据库定义实验
一、实验 1.1 数据库定义实验
软件:sqlserver
(一)实验目的
理解和掌握数据库 DDL 语言,能够熟练地使用 SQL DDL 语句创建、修改和
删除数据库、模式和基本表。
(二)实验内容和要求
理解和掌握 SQL DDL 语句的语法,特别是各种参数的具体含义和使用方法;
使用 SQL 语句创建、修改和删除数据库、模式和基本表。掌握 SQL 语句常见语
法错误的调试方法。
(三)实验过程
实验数据库:SQLServer
1、创建数据库
创建一个数据库:create database 数据库名;
删除一个数据库:drop database 数据库名;
----创建数据库
CREATE DATABASE student
go
USE student
go
2、定义模式
创建模式: create schema 模式名
删除模式: drop schema 模式名;
use student--选择要操作的数据库
go --批处理命令
create schema ST
go
3、在ST模式下创建基本表
进入数据库:use 数据库名;
创建数据库表:create table 表名(列声明);
use student--选择要操作的数据库
go --批处理命令
create schema ST
create table Student(
Sno char(9) primary key,
Sname char(20) unique,
Ssex char(2),
Sage smallint,
Sdept char(20)
)
create table Course
(
Cno char(4) primary key,
Cname char(40) not null,
Cpno char(4),
Ccredit smallint ,
foreign key(cpno) references Course(Cno)
)
create table SC
(
Sno char(9),
Cno char(4),
Grade smallint,
primary key(Sno,Cno),
foreign key (Sno) references Student(Sno),
foreign key (Cno) references Course(Cno)
)
go
4、向表中插入数据
插入数据:
INSERT
INTO <表名> [(<属性列1>[,<属性列2 >…)]
VALUES (<常量1> [,<常量2>] … )
由于此处的Course表中的Cno和Cpno会产生外键冲突,所以在插入数据时先将所有课程的Cpno设为自身,再使用UPDATE语句进行更改。
use student
go
insert into ST.Student(Sno,Sname,Ssex,Sage,Sdept)
values('201215121','李勇','男','20','CS'),
('201215122','刘晨','女','19','CS'),
('201215123','王敏','女','18','MA'),
('201215125','张立','男','19','IS')
go
INSERT INTO ST.Course (Cno, Cname, Cpno, Ccredit) VALUES ('1',