Visual studio/ Microsoft SQL关系键错误

本文关键字:关系 错误 SQL Microsoft studio Visual | 更新日期: 2023-09-27 18:07:01

CREATE TABLE TBLUSER_INFO
(
ID INT IDENTITY(1,1)not null PRIMARY KEY,
LNAME VARCHAR(50)not null,
FNAME VARCHAR(50)not null,
MNAME VARCHAR (50)not null,
ADDRESS VARCHAR(50)not null,
AGE INT not null,
GENDER VARCHAR(10)not null,
CONTACTNO varchar(50) not null, 
BIRTHDATE datetime
);

CREATE TABLE TBLUSER_ACCOUNT
(
ID INT FOREIGN KEY REFERENCES TBLUSER_INFO(ID) ,
USERNAME VARCHAR(50)not null,
USERPASS NVARCHAR(50)not null,
SECURITYQUESTION VARCHAR(max)not null,
SECURITYANSWER VARCHAR(50)not null
);

错误如下

DBML1011: The Type element 'TBLUSER_ACCOUNT' contains the Association element 'TBLUSER_INFO_TBLUSER_ACCOUNT' but does not have a primary key.  No code will be generated for the association.

当我在visual中运行我的系统时,这将是输出我的系统

请帮助我完成我的项目。我绝望的

Visual studio/ Microsoft SQL关系键错误

SQL SERVER要求在声明任何外键之前在表中有一个主键。

在您的TBLUSER_ACCOUNT需要添加primary key ID和fkey与TBLUSER_INFO ID,像这样。

 CREATE TABLE TBLUSER_INFO
    (
    ID INT IDENTITY(1,1)not null PRIMARY KEY,
    LNAME VARCHAR(50)not null,
    FNAME VARCHAR(50)not null,
    MNAME VARCHAR (50)not null,
    ADDRESS VARCHAR(50)not null,
    AGE INT not null,
    GENDER VARCHAR(10)not null,
    CONTACTNO varchar(50) not null, 
    BIRTHDATE datetime
    );
    CREATE TABLE TBLUSER_ACCOUNT
    (
    ID INT IDENTITY(1,1) not null PRIMARY KEY,
    ID_TBLUSER_INFO INT,
    USERNAME VARCHAR(50)not null,
    USERPASS NVARCHAR(50)not null,
    SECURITYQUESTION VARCHAR(4000) not null,
    SECURITYANSWER VARCHAR(50)not null,
    FOREIGN KEY (ID_TBLUSER_INFO) REFERENCES TBLUSER_INFO(ID)
    );