SQL Server创建数据库及表;增,删,改,查方法介绍

1. 创建数据库的方法:

use master
go
--判断当前数据库是否存在
if exists(select * from sysdatabases where name = 'StudentManageDB')
drop database StudentManageDB
go

--创建数据库
create database StudentManageDB
ON primary
(
name  = 'StudentManagementDB_data',--数据库文件的逻辑名
filename = 'D:\Program File\Microsoft SQL Server\MSSQL15.SQLEXPRESS\MSSQL\DATA\StudentManagementDB_data.mdf',--数据库物理文件名(绝对路径)
size=10MB,--数据库文件初始大小
filegrowth=5MB--数据库扩充,数据文件增长量
),
(
name  = 'StudentManagementDB_data1',--数据库文件的逻辑名
filename = 'D:\Program File\Microsoft SQL Server\MSSQL15.SQLEXPRESS\MSSQL\DATA\StudentManagementDB_data1.ndf',--数据库物理文件名(绝对路径)
size=10MB,--数据库文件初始大小
filegrowth=5MB--数据库扩充,数据文件增长量
)
log on 
(NAME = 'StudentManagementDB_log',
filename='D:\Program File\Microsoft SQL Server\MSSQL15.SQLEXPRESS\MSSQL\DATA\StudentManagementDB_log.ldf',
size = 5MB,
filegrowth = 2MB
),
(NAME = 'StudentManagementDB_log1',
filename='D:\Program File\Microsoft SQL Server\MSSQL15.SQLEXPRESS\MSSQL\DATA\StudentManagementDB_log1.ldf',
size = 5MB,
filegrowth = 2MB
)
go

2. 创建表:

use StudentManageDB
go
if exists(select * from sysobjects where name = 'Students')
drop table Students
go
create table Students
(
	StudentId int identity(10000,1),--学号
	StudentName varchar(20) not null,--姓名
	Gender char(2) not null,--性别
	Birthday datetime not null,--出生日期
	StudentIdNo numeric(18,0) not null,--身份证号
	Age int not null,--年龄
	PhoneNumber varchar(50),
	StudentAddress varchar(500),
	ClassId int -- 班级外键
)
go
--创建班级表
if exists(select * from sysobjects where name = 'StudentClass')
drop table StudentClass
create table StudentClass
(
	ClassId int primary key,--班级编号
	ClassName varchar(20) not null,--班级姓名
)

--创建成绩表
if exists(select * from sysobjects where name = 'ScoreList')
drop table ScoreList
create table ScoreList
(
	Id int identity(1,1) primary key,--班级编号
	StudentId int not null,--班级姓名,好好外键
	CShar int null,
	SQLServe int null,
	UpdateTime datetime not null
)
--创建管理员表
if exists(select * from sysobjects where name = 'Admins')
drop table Admins
create table Admins
(
	LoginId int identity(1000,1) primary key,
	LoginPwd varchar(20) not null,--登录密码
	AdminName varchar(20) not null
)

3.增、 删、改、查方法

use StudentManageDB
go
insert into Students(StudentName,Gender,Birthday,Age,StudentIdNo,PhoneNumber,StudentAddress,ClassId)
values('李小璐','女','1989-01-12','26',372425198608109075,'022-88997766','天津和憋屈',1)

insert into Students(StudentName,Gender,Birthday,Age,StudentIdNo,PhoneNumber,StudentAddress,ClassId)
values('王璐','女','1980-01-12','40',372425198608109079,'022-88997767','山东',1)

select StudentName,Gender,Birthday from Students where Age >22

update Students set Gender = '女',Age=36,StudentAddress='天津河北区' where age > 22
select * from Students

delete from Students where StudentId = 10003

truncate table Students

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值