永发信息网

SQL建表问题

答案:2  悬赏:0  手机版
解决时间 2021-05-10 04:57
  • 提问者网友:嘚啵嘚啵
  • 2021-05-09 04:52

为什么执行第2次会报错?

use master
go

if exists(select * from sysdatabases where name='s2bbsDB')
drop database s2bbsDB
go

create database s2bbsDB
on
(
name='s2bbsDB_db',
filename='D:\project\s2bbsDB_db.mdf',
size=10mb,
filegrowth=20%
)
log on
(
name='s2bbsDB_log',
filename='D:\project\s2bbsDB_db.log',
size=3mb,
maxsize=20mb,
filegrowth=20mb
)
go

use s2bbsDB
go

if exists(select * from sysobjects where name='s2bbsUsers')
drop table s2bssUsers
go

create table s2bbsUsers
(
UID int Identity(1,1) NOT NULL, --自动编号
Uname varchar(15) not null, --昵称
Upassword varchar(10) not null, --密码
Uemail varchar(20), --Email
Usex bit not null, --性别
Uclass int, --等级
Uremark varchar(20), --备注
UregDate datetime not null, --注册日期
Ustate int, --状态
Upoint int --用户的积分
)
go

alter table s2bbsUsers
add constraint pk_UID primary key(UID),
constraint DF_Upassword default(888888) for Upassword,
constraint DF_Usex default(1) for Usex,
constraint DF_Uclass default(1) for Uclass,
constraint DF_UregDate default(getdate()) for Uregdate,
constraint DF_Ustate default(0) for Ustate,
constraint DF_Upoint default(20) for Upoint,
constraint CK_Ueamil check ('Ueamil' like '%@%'),
constraint CK_Upassword check (len(Upassword)>=6)
go

use s2bbsDB
go

if exists(select * from sysobjects where name='s2bbsSection')
drop table s2bbsSection
go

create table s2bbsSection
(
SID int Identity(1,1) NOT NULL, --自动编号
Sname varchar(15) not null, --昵称
SmasterID int not null, --版主的用户id
Sprofile Varchar(50), --版面简介
SclickCount int, --点击率
StopicCount int --发贴数
)
go

alter table s2bbsSection
add constraint PK_SID primary key(SID),
constraint DF_SclickCount default(0) for SclickCount,
constraint DF_StopicCount default(0) for StopicCount
go
alter table s2bbsSection
add constraint FK_s2bbsUsers_s2bbsSection foreign key(SmasterID) references s2bbsUsers(UID)
go

最佳答案
  • 五星知识达人网友:从此江山别
  • 2021-05-09 05:34
你想问什么
全部回答
  • 1楼网友:第四晚心情
  • 2021-05-09 07:05

数据库不能有相同数据表存在

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯