数据库,新手建表问题! 用的SQLserver2008,网上查的有两种方法,一种直接用sql生成器

数据库,新手建表问题! 用的SQLserver2008,网上查的有两种方法,一种直接用sql生成器,第1张

/*创建bbsDB数据库*/

use master

if exists(select * from sysdatabases where name='bbsDB')

drop database bbsDB

create database bbsDB

on

(

name='bbsDB_data',

filename='D:\project\bbsDB_data.mdf',

size=10,

filegrowth=20%

)

log on

(

name='bbsDB_log',

filename='D:\project\bbsDB_log.ldf',

size=3,

maxsize=20,

filegrowth=10%

)

/*创建bbsUsers表*/

use bbsdb

if exists(select * from sysobjects where name='bbsUsers')

drop table bbsUsers

create table bbsUsers

(

UID int identity(1,1) not null,--学号,标识列

Uname varchar(15) not null,--用户昵称

Upassword varchar(10) not null,--用户密码

Uemail varchar(20),--邮箱地址

Usex bit not null,--用户性别

Uclass int,--等级

Uremark varchar(20),--备注

UregDate datetime not null,--注册日期

Ustate int null, --状态

Upoint int null--用户积分

)

/*创建bbsUsers表中的约束*/

alter table bbsUsers

add constraint PK_uid primary key(uid),--主键

constraint DF_Upassword default(888888) for Upassword,--初始密码为888888

constraint DF_Usex default (1) for Usex,--性别默认为

constraint DF_UregDate default (getdate()) for UregDate,--注册日期默认为系统日期

constraint DF_Ustate default(0) for Ustate,--状态默认为离线

constraint DF_Upoint default(20) for Upoint,--积分默认为20点

constraint CK_Uemail check(Uemail like '%@%'),--电子邮件必须含有@符号

constraint CK_Upassword check (len(Upassword)>=6)--密码至少为六位

/*创建bbsSection表*/

use bbsdb

if exists(select * from sysobjects where name='bbsSection')

drop table bbsSection

create table bbsSection

(

SID int identity(1,1) not null,--板块标号,自动增长

Sname varchar(32) not null,--版块名称

SmasterID int not null,--版主用户ID

Sprofile varchar(20) null,--版面简介

SclickCount int null, --点击率

StopicCount int null--发帖数

)

/*创建bbsSection表中的约束*/

alter table bbsSection

add constraint PK_sid primary key(sid),--主键

constraint DF_SclickCount default(0) for SclickCount,--点击率默认为0

constraint DF_StopicCount default(0) for StopicCount,--发帖数默认为0

constraint DF_SmasterID foreign key(SmasterID)references bbsUsers (UID)--外键

/*创建bbsTopic表*/

use bbsdb

if exists(select * from sysobjects where name='bbsTopic')

drop table bbsTopic

create table bbsTopic

(

TID int identity(1,1) not null,--帖子编号,自动增长

TsID int not null,--发帖人ID

TuID int not null,--版主用户ID

TreplyCount int null,--回复数量

Tface int null, --发帖表情

Ttopic varchar(20) not null,--标题

Tcontents varchar(30) not null,--正文

Ttime datetime null,--发帖时间

TclickCount int null,--点击数

Tstate int not null,--状态

TlastReply datetime null--回复时间

)

/*创建bbsTopic表的约束*/

alter table bbsTopic

add constraint DF_TreplyCount default(0) for TreplyCount,--回复数量默认为0

constraint PK_tid primary key(tid),--主键

constraint DF_TclickCount default (0) for TclickCount,--点击数默认为0

constraint DF_Tstate default (1) for Tstate,--状态默认为1

constraint DF_Ttime default (getdate()) for Ttime,--发帖时间默认为系统日期

constraint CK_Tcontents check (len(Tcontents)>=6),--正文必须大于六个字符

constraint CK_TlastReply check ((TlastReply)>(Ttime)),--最后回复时间必须晚于发帖时间

constraint DF_TsID foreign key(TsID)references bbsSection (SID),--外键

constraint DF_TuID foreign key(TuID)references bbsUsers (UID)--外键

/*创建bbsReply表*/

use bbsdb

if exists(select * from sysobjects where name='bbsReply')

drop table bbsReply

create table bbsReply

(

RID int identity(1,1) not null,--自动编号,帖子编号

RtID int not null,--主贴ID

RsID int not null,--板块ID

RuID int not null,--回帖人ID

Rface int null, --回帖表情

Rcontents varchar(30) not null,--正文

Rtime datetime null,--回帖时间

RclickCount int null--点击数

)

/*创建bbsReply表的约束*/

alter table bbsReply

add constraint DF_Rtime default (getdate()) for Rtime,--回帖时间默认为系统日期

constraint CK_Rcontents check (len(Rcontents)>=6),--正文必须大于六个字符

constraint DF_RtID foreign key(RtID)references bbsTopic (TID),--外键

constraint DF_RsID foreign key(RsID)references bbsSection (SID),--外键

constraint DF_RuID foreign key(RuID)references bbsUsers (UID)--外键

1、先用Navicat for MySQL创建名字为shopping的数据库,如下图所示:

2、创建的shopping数据库上运行已有的.sql文件,要注意上面创建的数据库名字shopping最好和原先.sql文件中的名字一样,也可以不一样,如果不一样的话会自动生成原来.sql文件中SQL语句中所命名的数据库。

Mysql:MySQL 是一个关系型数据库,由瑞典 MySQL AB 公司开发,目前属于 Oracle 旗下公司。MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。

MySQL 是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。MySQL 所使用的 SQL 语言是用于访问数据库的最常用标准化语言。

MySQL 软件采用了双授权政策(本词条"授权政策"),它分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择 MySQL 作为网站数据库。由于其社区版的性能卓越,搭配 PHP ,Linux和 Apache 可组成良好的开发环境,经过多年的web技术发展,在业内被广泛使用的一种web服务器解决方案之一,称之为LAMP。

1、先用Navicat for MySQL创建名字为shopping的数据库,如下图所示:

2、创建的shopping数据库上运行已有的.sql文件,要注意上面创建的数据库名字shopping最好和原先.sql文件中的名字一样,也可以不一样,如果不一样的话会自动生成原来.sql文件中SQL语句中所命名的数据库。

此时,可以把上面创建的shopping数据库删除了就可以,直接用自动生成的就行。

运行.sql步骤如下:右键你刚才创建的shopping数据库——>运行sql文件...——>选择.sql文件位置——执行即可。

创建好的数据库如下所示:


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

原文地址: https://outofmemory.cn/sjk/6764712.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存