日期:2014-05-16  浏览次数:20464 次

创建数据库、表、主外键、各种约束、存储过程、视图、索引、事务使用、触发器、创建登录账号、数据库用户 Sql语句示例
  1. -----------创建数据库----------------??
  2. user?master??
  3. go??
  4. if ?exists?(select?*?from?sysdatabases?where?name= 'bankSystem' )??
  5. ????drop?database?bankSystem??
  6. go??
  7. create?database?bankSystem??
  8. on?primary??
  9. (??
  10. ????name='bank_data' ,??
  11. ????filename='D:\bank\bank_data.mdf' ,??
  12. ????size=5,??
  13. ????filegrowth=15%??
  14. )??
  15. log?on??
  16. (??
  17. ????name='bank_log' ,??
  18. ????filename='D:\bank\bank_log.ldf' ,??
  19. ????size=5,??
  20. ????filegrowth=15%??
  21. )??
  22. go??
  23. ??
  24. ----------------建表并添加约束----------------??
  25. use?bankSystem??
  26. go??
  27. ---------------------表userInfo--------------??
  28. if ?exists?(select?*?from?sysObjects?where?name= 'userInfo' )??
  29. ????drop?table?userInfo??
  30. go??
  31. create?table?userInfo??
  32. (??
  33. ????customerID?int ?identity(1,1)?not? null ,??
  34. ????customerName?varchar(30)?not?null ,??
  35. ????PID?varchar(18)?not?null ,??
  36. ????telephone?varchar(13)?not?null ,??
  37. ????address?varchar(50)??
  38. )??
  39. go??
  40. alter?table?userInfo??
  41. add?constraint?PK_customID?primary?key?(customerID)??
  42. alter?table?userInfo??
  43. add?constraint?UQ_pid?unique?(PID)??
  44. alter?table?userInfo??
  45. add?constraint?CK_pid?check?(pid?like?'4206[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' ?or??
  46. ????????????????????????????pid?like?'4206[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' )??
  47. ??
  48. alter?table?userInfo??????
  49. add?constraint?CK_telephone?check?(telephone?like?'13[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' ?or??
  50. ????????????????????????????????????telephone?like?'%-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' )??
  51. ------------表cardInfo---------------??
  52. if ?exists?(select?*?from?sysObjects?where?name= '