English 中文(简体)
微软 SQL, 外国密钥, 而非参考表格, 看不到导致它的原因
原标题:Microsoft SQL, foreign key not referencing table, can t see what s causing it

我花了好一阵子才试着解开它,但我看不到任何可能导致这个错误的东西。我想也许和“唯一”的说法有关。

1767 Msg 1767,16级,0州,40线

Foreign key FK_Loan_ItemNo__0AD2A005 references invalid table Item .

如果有人想用代码复制问题:

CREATE DATABASE LibrarySystem10
GO
USE LibrarySystem10
GO


CREATE TABLE MemberType(
    MemberTypeNo int NOT NULL,
    Name varchar(50) NOT NULL,
    Description varchar(250) NOT NULL,
    MaxNumberLoans int NOT NULL,
    MaxLoanDuration int NOT NULL
    PRIMARY KEY (MemberTypeNo)
)
insert into MemberType values ( 0 , UnderGraduate , A student at a college or university who has not yet earned a bachelor  s or equivalent degree. , 5 , 10 )
insert into MemberType values ( 1 , PostGraduate , A student undertaking study after completing a first degree. , 10 , 10 )
insert into MemberType values ( 2 , Staff , Staff at the university , 15 , 15 )

CREATE TABLE Member(
    MemberNo int NOT NULL,
    MemberTypeNo int NOT NULL,
    FirstName varchar(150) NOT NULL,
    LastName varchar(150) NULL,
    DateOfBirth varchar (200) NULL,
    HouseNo int NOT NULL,
    Street varchar(50) NOT NULL,
    Suburb varchar(100) NOT NULL,
    PostCode int NOT NULL,
    EmailAddress varchar(250) NULL,
    HomePhoneNo varchar(250) NULL,
    MobileNo varchar(250) NULL,
    MembershipStartDate varchar (200) NOT NULL,
    MembershipEndDate varchar (200) NOT NULL,
    MembershipStatus varchar(100) NOT NULL,
    PinNo int NOT NULL
    PRIMARY KEY (MemberNo)
    FOREIGN KEY (MemberTypeNo)REFERENCES MemberType ON UPDATE CASCADE
) 
insert into member values ( 0 , 0 , Shane , Lindsay , 15-11-1992 , 90 , fake st , FauxTon , 2250 , [email protected] , 0243296356 , 0415657164 , 15-11-2010 , 15-11-2020 , current , 0105 )
insert into member values ( 1 , 0 , Shaune , Lincoln , 18-12-1992 , 92 , faken st , FauxTone , 2350 , [email protected] , 0243253357 , 041565757 , 14-12-2010 , 14-12-2020 , deferred , 0123 )
insert into member values ( 2 , 0 , Sarah , richards , 08-08-1990 , 45 , Small st , Hornsby , 2279 , [email protected] , 02432567154 , 0416451845 , 01-01-2012 , 01-01-2022 , current , 0123 )


CREATE TABLE Loan(
    MemberNo int NOT NULL FOREIGN KEY(MemberNo) REFERENCES Member ON UPDATE CASCADE,
    ItemNo int NOT NULL FOREIGN KEY(ItemNo) REFERENCES Item ON UPDATE CASCADE,
    DateLoaned varchar (50) NOT NULL,
    DueDate varchar (50) NOT NULL,
    Status varchar(50) NOT NULL,
    FinesImposed bit NOT NULL DEFAULT  0  CHECK (finesImposed IN ( 0 , 1 )) ,
    Renewed bit NOT NULL DEFAULT  0  CHECK (Renewed IN ( 0 , 1 )),
    UNIQUE(MemberNo,ItemNo,DateLoaned)
) 
insert into Loan values ( 0 , 0 , 10-10-2012 , 15-10-2012 , loaned , 0 , 0 )
insert into Loan values ( 1 , 0 , 12-10-2012 , 15-10-2012 , loaned , 0 , 1 )

CREATE TABLE Item(
    ItemNo int NOT NULL,
    Title varchar(50) NOT NULL,
    Subject varchar(100) NULL,
    ISBN int NULL,
    PhysicalDescription varchar(150) NULL,
    Author varchar(75) NULL,
    PRIMARY KEY (ItemNo)
)
insert into Item values ( 0 , Book1 , IT , 0501425252 , Big,42pages , John Doe )
insert into Item values ( 1 , Book2 , IT , 0501425253 , Big,42pages , John Doe )


CREATE TABLE ItemCopy(
    ItemNo int NOT NULL,
    CallNumber varchar(50) NOT NULL,
    Condition varchar(50) NULL,
    UNIQUE(ItemNo,CallNumber),
    PRIMARY KEY (CallNumber)
) 
insert into ItemCopy values ( 0 , 0 , good )



CREATE TABLE Hold(
    HoldNo int NOT NULL,
    MemberNo int NOT NULL FOREIGN KEY(MemberNo) REFERENCES Member ON UPDATE CASCADE,
    ItemNo int NOT NULL FOREIGN KEY(ItemNo) REFERENCES Item ON UPDATE CASCADE,
    DateTimeHeld datetime NOT NULL,
    comments varchar(200) NULL,
    Status varchar(50) NOT NULL
    PRIMARY KEY (HoldNo)


)

CREATE TABLE Fine(
    FineNo int NOT NULL,
    MemberNo int NOT NULL FOREIGN KEY(MemberNo) REFERENCES Member ON UPDATE CASCADE,
    Description varchar(50) NULL,
    Amount int NOT NULL, 
    PRIMARY KEY (FineNo)
)


CREATE TABLE AudioRecording(
    Length varchar(50) NULL,
    BitRate varchar(50) NULL,
    Size varchar(50) NULL
)

CREATE TABLE ItemCollection(
    ItemNo int NULL,
    CollectionName varchar(75) NULL
    UNIQUE (ItemNo,CollectionName)
)

CREATE TABLE Collection(
    CollectionName varchar(75)NOT NULL
    PRIMARY KEY (CollectionName)
)

CREATE TABLE Book(
    PublisherInfo varchar(150) NULL,
    Edition int NULL,
    Notes varchar(250) NULL,
    Status varchar(50) NULL
)

CREATE TABLE Journal(
    Series int NULL,
    Notes varchar(250) NULL,
    OtherTitles varchar(150) NULL,
    PriorTitles varchar(250) NULL
)


SELECT m.FirstName, l.Status, l.DueDate
    FROM Member m, Loan l
    WHERE  m.MemberNo =  0  AND  l.MemberNo = m.MemberNo 
最佳回答

贷款引用项目,但您在创建项目前先创建贷款项目。先创建项目表格。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签