SQL数据库的基础 *** 作

SQL数据库的基础 *** 作,第1张

SQL数据库的基础 *** 作

一,认识SQL数据库

  美国Microsoft公司推出的一种关系型数据库系统。


SQLServer是一个可扩展的、高性能的、为分布式客户机/服务器计算所设计的数据库管理系统,实现了与WindowsNT的有机结合,提供了基于事务的企业级信息管理系统方案。


其主要特点如下:
(1)高性能设计,可充分利用WindowsNT的优势。



(2)系统管理先进,支持Windows图形化管理工具,支持本地和远程的系统管理和配置。



(3)强壮的事务处理功能,采用各种方法保证数据的完整性。



(4)支持对称多处理器结构、存储过程、ODBC,并具有自主的SQL语言。


SQLServer以其内置的数据复制功能、强大的管理工具、与Internet的紧密集成和开放的系统结构为广大的用户、开发人员和系统集成商提供了一个出众的数据库平台

二,SQL数据库的基本 *** 作

--打开系统数据库
use master
go
--判断是否已经有这个数据库,如果有就删除
if exists(select 1 from sys.databases where name='StudentDB')
 drop database StudentDB
go
--新建数据库StudentDB
create database StudentDB
on
(
 name='StudentDB',
 filename='F:\SQL数据库\Student.mdf',
 size=5MB,
 filegrowth=20%
)
log on
(
 name='StudentDB_log',
 filename='F:\SQL数据库\StdentDB_log.ldf',
 size=5MB,
 filegrowth=20%
)
go
--打开StudentDB数据库
use StudentDB
go
-----------------------创建表-----------------------------------------------------------
--判断是否已经有这个这个表,如果有就删除
if exists(select 1 from sys.tables where name='Classes')
 drop database Classes
go
/*
* 表名: 班级
* 作用: 存放班级数据
* 设计者: ***
* 设计说明:暂无
*/
create table Classes
(
 --班级编号
 ClassNo  varchar(8)  primary key,--主键
 --班级名称
 ClassName nvarchar(10) not null
)
go
--判断是否已经有这个这个表,如果有就删除
if exists(select 1 from sys.tables where name='Students')
 drop database Students
go
/*
* 表名: 学生表
* 作用: 存放学员信息
*/
create table Students
(
 --学号
 StudentNo  varchar(10)  not null primary key ,--主键  自增长:identity(为int类型时可以使用)
 --姓名
 StudentName  nvarchar(6)  not null,
 --性别
 StudentGender char(1)   not null,
 --生日
 StudentBirth smalldatetime not null,
 --所在班级
 ClassNo   varchar(8)  not null
)
go --判断是否已经有这个这个表,如果有就删除
if exists(select 1 from sys.tables where name='Courses')
 drop database Courses
go
/*
* 表名: 课程表
* 作用: 存放课程信息
*/
create table Courses
(
 --课程编号
 CourseNo  varchar(10)  not null,
 --课程名称
 CourseName  varchar(50)  not null,
 --先修课程编号
 CoursePrev  varchar(10)  null
) --判断是否已经有这个这个表,如果有就删除
if exists(select 1 from sys.tables where name='Achievements')
 drop database Achievements
go
/*
* 表名: 成绩表
* 作用: 存放成绩信息
*/
create table Achievements
(
 --学号
 StudentNo varchar(10)  not null,
 --课程编号
 CourseNo varchar(10)  not null,
 --成绩
 Score  decimal(5,2) not null
)
go
-------------------------------创建表约束--------------------------------------------------------------
/*学号为主键*/
alter table Students
 add constraint PK_StudentNo primary key(StudentNo)
/*性别检查约束,只能是“M”或“F”*/
alter table Students
 add constraint CK_StudentGender check(StudentGender in ('F','M'))
/*性别默认为男*/
alter table Students
 add constraint DF_StudentGender default('M') for StudentGender
/*引用班级编号*/
alter table Students
 add constraint FK_Students_Classes_ClassNo foreign key(ClassNo) references Classes(ClassNo)
go
/*社会自课程编号为主键*/
alter table Courses
 add constraint PK_CourseNo primary key(CourseNo)
/*课程名称唯一*/
alter table Courses
 add constraint UQ_CourseName unique(CourseNo)
/*先修课程引用自身表为外键*/
alter table Courses
 add constraint FK_CoursePrev foreign key(CoursePrev) references Courses(CourseNo)
go
/*学号和课程组合主键*/
alter table Achievements
 add constraint PK_StudentNo_CourseNo primary key(StudentNo,CourseNo)
/*分数在0-100之间*/
alter table Achievements
 add constraint CK_Score check(Score between 0 and 100)
/*学号外键*/
alter table Achievements
 add constraint FK_Achievements_Students_StudentNo foreign key(StudentNo) references Students(StudentNo)
/*课程外键*/
alter table Achievements
 add constraint FK_Achievements_Students_CourseNo foreign key(CourseNo) references Courses(CourseNo)
go
----------------------------添加值-----------------------------------------------------------------
--班级表(方法一)
insert into Classes(ClassNo,ClassName) values('20100301','T100')
insert into Classes(ClassNo,ClassName) values('20100308','T101')
insert into Classes(ClassNo,ClassName) values('20100322','T102')
go
--学生表(方法二)
insert into Students(StudentNo,StudentName,StudentGender,StudentBirth,ClassNo)
     select '2010030101','张强','M','1988-10-19','20100301'
 union all select '2010030102','李晓芸','F','1989-11-29','20100301'
 union all select '2010030103','程向润','M','1989-05-11','20100301'
 union all select '2010030104','吴超','M','1990-06-03','20100301'
 union all select '2010030804','蒋永涛','M','1989-10-19','20100308'
go
--课程表(方法二)
insert into Courses(CourseNo,CourseName,CoursePrev)
  select 'CTB','计算机基础',null union all
  select 'STB','软件技术基础','CTB' union all
  select 'C','程序基础和C语言实现','STB' union all
  select 'SQL BASE','SQL SERVER应用开发','STB' union all
  select 'JAVA','JAVA面向对象程序设计','C' union all
  select 'SQL ADV','SQL SERVER数据库设计和实现','SQL BASE'
go
--成绩表(方法二)
insert into Achievements(StudentNo,CourseNo,Score)
  select '2010030101','CTB',85 union all
  select '2010030101','STB',87 union all
  select '2010030101','C',90 union all
  select '2010030102','CTB',80 union all
  select '2010030102','STB',91 union all
  select '2010030102','C',82 union all
  select '2010030103','CTB',77 union all
  select '2010030103','STB',95 union all
  select '2010030103','C',84 union all
  select '2010030104','CTB',65 union all
  select '2010030104','STB',77 union all
  select '2010030104','C',95 union all
  select '2010030801','CTB',83 union all
  select '2010030801','STB',86 union all
  select '2010030801','C',91
go
--------------------------查询表-----------------------------------------------------------
select * from Classes
select * from Students
select * from Courses
select * from Achievements

以上就是SQL的基本 *** 作

大家可以留言,因为我可能有些地方写得不是很好,也有可能有错

  

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/586576.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存