简体   繁体   English

无法在SQL中获取此表和约束

[英]Can't get this table and constraint right in SQL

I'm doing this exercise and and I can't get it right - can anybody help me this? 我正在做这个练习而且我做不到 - 有人可以帮助我吗?

Write a second script called 2_CreateTables.sql that will create the Department and Employees tables in the FinalExam database with the following information: 编写第二个名为2_CreateTables.sql脚本,该脚本将使用以下信息在FinalExam数据库中创建DepartmentEmployees表:

Department table Information: Department表信息:

Column Name     Data Type                         Allow Nulls?
--------------------------------------------------------------
DepartmentID    Integer                           No
DepartmentName  Variable length character data    No
GroupName       Variable length character data    Yes
ModifiedDate    Small date and time               No

AutoNumber: 自动编号:

Make the DepartmentID automatically number starting with 100 and increment by 5. 使DepartmentID自动从100开始,并以5递增。

Constraints : 制约因素

  1. The DepartmentID column will be defined as a clustered primary key called PK_Department_DepartmentID . DepartmentID列将定义为名为PK_Department_DepartmentID的聚簇主键。

  2. The ModifiedDate column will have a default constraint called DF_DeptModified with the system date and time as the default value. ModifiedDate列将具有名为DF_DeptModified的默认约束,系统日期和时间为默认值。

This is what I have so far. 这就是我到目前为止所拥有的。

CREATE TABLE Deparment
    DeparmentID  int not null,
    DeparmentName varchar(100) not null,
    GroupName varchar(50) null,
    ModifiedDate datetime  not null,

Assuming you're using SQL Server - try this: 假设您正在使用SQL Server - 试试这个:

CREATE TABLE dbo.Department
    (DepartmentID INT IDENTITY(100, 5) NOT NULL,
     DepartmentName VARCHAR(100) NOT NULL,
     GroupName VARCHAR(50) NULL,
     ModifiedDate SMALLDATETIME NOT NULL
        CONSTRAINT DF_DeptModified DEFAULT(GETDATE()),

     CONSTRAINT PK_Department_DepartmentID
       PRIMARY KEY CLUSTERED (DepartmentID)
    )
GO

Changes made: 做出的改变:

  • make sure you have starting and ending parenthesis ( ) after your CREATE TABLE (name) statement 确保在CREATE TABLE (name)语句之后有开始和结束括号( )
  • added the IDENTITY(100, 5) clause to the DepartmentID to make it auto-numbering according to the requirements IDENTITY(100, 5)子句添加到DepartmentID ,使其根据要求自动编号
  • changed the datatype of ModifiedDate to be SMALLDATETIME as defined in the textual requirements (you had DATETIME ) ModifiedDate的数据类型更改为文本要求中定义的SMALLDATETIME (您有DATETIME
  • fixed all the Deparment to Department 固定所有的Deparment ,以Department
  • added the default constraint for ModifiedDate ModifiedDate添加了默认约束
  • added the table constraint for the clustered primary key 为集群主键添加了表约束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM