创建数据库、创建表

本文介绍如何在SQL Server环境中从零开始搭建数据库,包括启用xp_cmdshell功能以创建必要的文件夹、定义数据库结构及其文件存放路径,并创建用户表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. Use master
  2. go
  3. If Exists (Select * From sysdatabases Where name = 'bbs') --检查是否以存在bbsDB数据库
  4.     Drop Database bbs
  5. go
  6. /*---- 创建文件夹 ----*/
  7. --sql 2005如何使用被禁止的"xp_cmdshell"
  8. USE master 
  9. EXEC sp_configure 'show advanced options', 1 
  10. RECONFIGURE WITH OVERRIDE 
  11. EXEC sp_configure 'xp_cmdshell', 1 
  12. RECONFIGURE WITH OVERRIDE 
  13. EXEC sp_configure  'show advanced options', 0 
  14. go
  15. Exec xp_cmdshell 'mkdir D:/bbsDB' --调用DOS命令创建文件夹
  16. go
  17. /*------   建库   ------*/
  18. Create Database bbs
  19. on
  20. (
  21.     /*--------  数据文件具体描述   ----------*/
  22.     Name = 'bbs_data',                  --主数据文件的逻辑名称
  23.     FileName = 'D:/bbsDB/bbs_data.mdf', --数据文件的物理名称
  24.     Size = 5Mb,                         -- 主数据文件的初始大小
  25.     FileGrowth = 20%                    --主数据文件增长率
  26. )
  27. log on
  28. (
  29.     /* ------ 日志文件的具体描述,各参数含义同上 ------ */
  30.     Name = 'bbs_log',
  31.     FileName = 'd:/bbsDB/bbs_log.ldf',
  32.     Size = 3Mb,
  33.     FileGrowth = 10%
  34. )
  35. go
  36. -----------------------------------------------
  37. /*--- 创建表 ---*/
  38. Use bbs
  39. go
  40. /* --- 检查是否存在表TBL_USER  ---*/
  41. If Exists (Select * From Sysobjects where name = 'TBL_USER')
  42.     Drop Table TBL_USER
  43. Create Table TBL_USER
  44. (
  45.     uId int primary key identity(1,1),   --主键,标识列
  46.     uName varchar(20) Unique(uName) not null,
  47.     uPass varchar(20) not null,
  48.     head varchar(100) not null,
  49.     regTime dateTime not null,
  50.     gender smallint not null
  51. )
  52. go
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值