向表添加主键和关系

时间:2014-12-10 11:51:28

标签: sql sql-server

我编写了以下内容作为我迄今所做的一个例子,并想知道我应该如何继续:

CREATE TABLE tblMembers
(
Member_ID int,
Name varchar(255)
);

CREATE TABLE tblHorses
(
Horse_ID int,
Name varchar(255),
Age int(10),
Member_ID int(10)
);

所以我想指定Member_ID和Horse_ID作为PK,并使用Member_ID在tblMembers和tblHorses之间创建关系

我还想让ID列自动增量

提前谢谢

6 个答案:

答案 0 :(得分:1)

使用它。的 Fiddler Demo

请参阅此内容以创建Primary KeyForeign KeyIdentity

CREATE TABLE tblMembers
(
    Member_ID int IDENTITY(1,1) Primary Key,
    Name varchar(255)
);

CREATE TABLE tblHorses
(
    Horse_ID int IDENTITY(1,1) Primary Key,
    Name varchar(255),
    Age int,
    Member_ID int Foreign key (Member_ID) REFERENCES tblMembers(Member_ID)
);

注意: MS SQL doesn't support length in Integer Type.

答案 1 :(得分:1)

这是你要问的吗?

CREATE TABLE tblMembers (
    Member_ID int identity(1, 1) not null primary key
    Name varchar(255)
);

CREATE TABLE tblHorses (
    Horse_ID int identity(1, 1) not nullprimary key
    Name varchar(255),
    Age int,
    Member_ID int references tblMembers(member_id)
);

在列中存储类似“年龄”的内容是一个非常糟糕的主意。毕竟,年龄不断变化。你应该存储类似出生日期的东西。

答案 2 :(得分:1)

CREATE TABLE tblMembers
(
Member_ID int AUTO_INCREMENT,
Name varchar(255)
PRIMARY KEY (MEMBER_ID)
);

CREATE TABLE tblHorses
(
Horse_ID int,
Name varchar(255),
Age int(10),
FOREIGN KEY (MEMBER_ID) REFERENCES tblMembers(MEMBER_ID)
PRIMARY KEY (HORSE_ID)
);

遵循W3Schools的例子。

答案 3 :(得分:1)

尝试以下

CREATE TABLE tblMembers (
Member_ID int identity(1, 1) not null 
Name varchar(255)
PRIMARY KEY (Member_ID )
);

CREATE TABLE tblHorses (
Horse_ID int identity(1, 1) not null 
Name varchar(255),
Age int,
PRIMARY KEY (Horse_ID) 
Member_ID int references tblMembers(member_id)
);

答案 4 :(得分:1)

试试这个

  CREATE TABLE [dbo].[tblMembers](
    [Member_ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](255) NOT NULL,
 CONSTRAINT [PK_tblMembers] PRIMARY KEY CLUSTERED 
(
    [Member_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[tblHorses](
    [Horse_ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](255) NULL,
    [Age] [int] NULL,
    [Member_ID] [int] NOT NULL,
 CONSTRAINT [PK_tblHorses] PRIMARY KEY CLUSTERED 
(
    [Horse_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[tblHorses]  WITH CHECK ADD  CONSTRAINT [FK_tblHorses_tblMembers] FOREIGN KEY([Member_ID])
REFERENCES [dbo].[tblMembers] ([Member_ID])
GO

ALTER TABLE [dbo].[tblHorses] CHECK CONSTRAINT [FK_tblHorses_tblMembers]
GO

答案 5 :(得分:1)

对于Auto Incremental,您必须将PK列设置为identity(seed, value)

CREATE TABLE tblMembers (
    Member_ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY
    Name VARCHAR(255)
);

CREATE TABLE tblHorses (
    Horse_ID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY
    Name VARCHAR(255),
    Age INT,
    Member_ID int REFERENCES tblMembers(Member_id)
);