SQL 2005 命令创建表 详细的程序代码

作者&投稿:鬱迹 (若有异议请与网页底部的电邮联系)
sqlserver2005用语句创建表~

新建查询,然后输入如下sql语句:
create table 表名(列名1 类型 [not null] [primary key],列名2 类型 [not null],..)
以上是模板,列属性根据具体要做的表来一一设置,建完后刷新一下数据库

详细步骤如下:
  1、点击【新建查询】按钮,打开SQL命令编辑框,对数据库表的操作以及维护都可以通过编辑SQL命令实现。

  2、在编辑框内编辑创建数据库表的代码,确认代码无误后,单击【执行】按钮,创建数据表。


  3、创建数据表的源代码如下:
  use test
  go
  if exists(select name from sys.tables where name='Student')
  drop table Student
  go
  create table Student
  (sname nchar(10) primary key,
  sex nchar(2) not null,
  bir datetime
  )

/*创建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)--外键

差不多,你改一下吧!

CREATE TABLE demo
GO
最简单的创建方式,系统默认分配大小与位置。

这种提你拿上来问是在侮辱你自己。 不想学就不学 有必要这样做么


铁山港区15865371142: 如何在sql server 2005中建表 -
琴咐凯韦: 在 SQL Server 2005 中创建数据表与其他SQL数据库创建数据表是一样的,无非是界面有一定的差别而已,必须首先创建数据库,例如你需要的数据库名称是 ABCD,在创建数据库后应该在界面出现ABCD数据库,右击数据库,就可以创建数据表.创建数据表必须有数据表的名称,数据表中有字段,各字段的字段名、字段的数据类型、字段的大小、字段的默认值等,一个数据表中可以包含有多个字段,最好有自动编号的字段ID,并且将ID设置为主键.

铁山港区15865371142: sqlserver 2005中怎么用语句创建表 -
琴咐凯韦: 在SQL Server 2005 Management Studio里随便打开一个数据库,随便选中一个表,鼠标右键->编写表脚本为->创建到->新查询编辑器窗口(见附图) 就会打开查询编辑器窗口,把创建表的语句给你复制在里面了,自己照着改改就行了.

铁山港区15865371142: 在SQL Server2005怎么建立数据表 -
琴咐凯韦: 可以这样: SQL server组----local-----数据库-----双击打开---建立自己的数据库-------打开数据库-----表----右键----新建表---进入设计视图--就可以设置字段属性了.

铁山港区15865371142: 如何在sql server 2005 中建表 -
琴咐凯韦: sql server 2005 中建表:在对象资源管理器中找到你要添加表的数据库,打开数据库后:表---右键---新建表,然后在窗体中输入你要添加的字段,设置表名后保存就行

铁山港区15865371142: sqlserver2005用语句创建表 -
琴咐凯韦: 点击菜单栏上的“新建查询”按钮,然后在弹出的输入区域输入 USE jxcbook --跳到jxcbook意思就是在jxcbook下创建表 go create table [商品清单] 创建表( id int identity(1,1) primary key, --一下是表内容,可以增加其它字段 Goods nvarchar(30))

铁山港区15865371142: SQL Server 2005 提供了几种方法创建数据库表 -
琴咐凯韦: 新建查询//创建数据库 create database e go//创建表 create table m go

铁山港区15865371142: SQL Server 2005中把查询结果生成表的命令是什么 -
琴咐凯韦: 可以使用函数【into】 例如查询表:Table_Temp中Col1为1,2,3的记录保存到新表:Table_New中 select * into Table_New from Table_Temp where Col1 in(1,2,3)这样即会在数据库内产生新表:Table_New,表的内容行就是上面语句的查询结果.延伸:在使用into生成新表时,列的类型会复制到新表,但列的标识和标识值不会带过来.例如,原表中ID列为 自增INT类型的,并且为标识列,在新表中的ID列就只会是Int类型,标识和标识的相关属性不会复制过来.

铁山港区15865371142: 如何SQL2005中创建新表
琴咐凯韦: CREATE TABLE b表名字(列名1,数据类型,列名2,数据类型,....................注意别忘了设置主键啊)

铁山港区15865371142: 在sql server 2005中怎么创建表 -
琴咐凯韦: sqlserver2005的企业管理器的名字改成了SQL Server Management Studio 找到这个建表就没问题了 另一种是在vs2005开发环境中的服务器资源管理器中进行创建.

铁山港区15865371142: 简述sql server 2005中创建表的操作步骤 ?表的约束如何设置? -
琴咐凯韦: 先建立简单的 然后在弄复杂的 可以数据库右键新建 或用sql新建

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网