SQL71501 :: Foreign Key: [dbo].[FK_AddressBook_Country] has an unresolved reference to Column [dbo].[AddressBook].[CountryID] - sql

hope someone can help me. I have an error
" SQL71501 :: Foreign Key: [dbo].[FK_AddressBook_Country] has an unresolved reference to Column [dbo].[AddressBook].[CountryID]. "
It red line the ([CountryID)]. I can't find where its fault. I actually followed this site http://demo.dotnetawesome.com/mvc/mycontactbook/part2 , I just changed the tables. Hopefully, someone can help me with this. Thank you so much!
CREATE TABLE [dbo].[AddressBook] (
[Id] INT NOT NULL,
[Name] VARCHAR (50) NOT NULL,
[Surname] VARCHAR (50) NOT NULL,
[Address1] VARCHAR (200) NOT NULL,
[Address2] VARCHAR (100) NULL,
[Postcode] VARCHAR (6) NOT NULL,
[Town] VARCHAR (20) NOT NULL,
[Country] VARCHAR (50) NOT NULL,
[Email] VARCHAR (50) NOT NULL,
[MobileNumber] VARCHAR (20) NOT NULL,
[PictureUser] VARCHAR (200) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_AddressBook_Country] FOREIGN KEY ([CountryID]) REFERENCES [Country]([CountryID])
);
CREATE TABLE [dbo].[Country] (
[CountryID] INT IDENTITY (1, 1) NOT NULL,
[CountryName] VARCHAR (100) NOT NULL,
PRIMARY KEY CLUSTERED ([CountryID] ASC)
);

this should work fine how ever there are two mistakes first in "FOREIGN KEY ([CountryID])" you have no field in the AdresseBook named "CountryId" i think you wanted the field "Country" as a foreign key if this is the case the the Country field in adressBook must have the same type with CountryID of the table Country
CREATE TABLE [dbo].[AddressBook] (
[Id] INT NOT NULL,
[Name] VARCHAR (50) NOT NULL,
[Surname] VARCHAR (50) NOT NULL,
[Address1] VARCHAR (200) NOT NULL,
[Address2] VARCHAR (100) NULL,
[Postcode] VARCHAR (6) NOT NULL,
[Town] VARCHAR (20) NOT NULL,
[Country] INT NOT NULL,
[Email] VARCHAR (50) NOT NULL,
[MobileNumber] VARCHAR (20) NOT NULL,
[PictureUser] VARCHAR (200) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_AddressBook_Country] FOREIGN KEY ([Country]) REFERENCES [Country]([CountryID])
);
CREATE TABLE [dbo].[Country] (
[CountryID] INT IDENTITY (1, 1) NOT NULL,
[CountryName] VARCHAR (100) NOT NULL,
PRIMARY KEY CLUSTERED ([CountryID] ASC)
);

Related

Could not create constraint or index. See previous errors

CREATE TABLE BuyInsurance(
ID INT primary key IDENTITY (1, 1) NOT NULL,
Cus_ID AS ('CUS' + RIGHT('0000' + CONVERT (VARCHAR (5), ID), (5))) PERSISTED,
us_Name VARCHAR (50) NULL,
Cus_ADD VARCHAR (50) NULL,
Cus_phone BIGINT NULL,
Pol_Num AS ('POL' + RIGHT('0000' + CONVERT (VARCHAR (5), ID), (5))) PERSISTED,
Pol_Date VARCHAR (50) NULL,
Pol_Duration VARCHAR (50) NULL,
Veh_Num BIGINT NULL,
Veh_Name VARCHAR (50) NULL,
Veh_Model VARCHAR (50) NULL,
Veh_Version VARCHAR (50) NULL,
Veh_Rate BIGINT NULL,
Veh_Warranty VARCHAR (50) NULL,
Veh_BodyNum VARCHAR (50) NULL,
Veh_EngineNum VARCHAR (50) NULL,
Cus_Add_Prove VARCHAR (50) NULL,
);
CREATE TABLE Vehicle_Information (
Vehicle_ID INT PRIMARY KEY IDENTITY (1, 1) NOT NULL,
Vehicle_Name VARCHAR (50) NULL,
Vechicle_Owner_Name VARCHAR (50) NULL,
Vehicle_Model VARCHAR (50) NULL,
Vehicle_Version VARCHAR (50) NULL,
Vehicle_Rate BIGINT NULL,
Vehicle_Body_Number VARCHAR (50) NULL,
Vehicle_Engine_Number VARCHAR (50) NULL,
Vehicle_Number BIGINT NULL,
);
create table BillingInformation (
ID int PRIMARY key identity (1,1),
Cus_ID int foreign key references BuyInsurance (ID),
Pol_Num varchar foreign key references BuyInsurance (ID),
Cus_phone bigint foreign key references BuyInsurance (ID),
BillN0 bigint ,
Vehicle_Name varchar foreign key references Vehicle_Information (Vehicle_ID),
Vehicle_Model varchar foreign key references Vehicle_Information (Vehicle_ID),
Vehicle_Rate varchar foreign key references Vehicle_Information (Vehicle_ID),
Vehicle_Engine_Number varchar foreign key references Vehicle_Information (Vehicle_ID),
Vehicle_Body_Number varchar foreign key references Vehicle_Information (Vehicle_ID),
Date date,
Amount bigint
);SELECT * FROM BillingInformation

Auto increment syntax errors

Here's the code to my simple database:
CREATE TABLE [dbo].[User_Info] (
[Username] NVARCHAR (50) NOT NULL,
[Password] NVARCHAR (50) NOT NULL,
[Firstname] NVARCHAR (50) NOT NULL,
[Lastname] NVARCHAR (50) NOT NULL,
[Email] NVARCHAR (50) NOT NULL,
[Country] NVARCHAR (50) NOT NULL,
[Phone] NVARCHAR (50) NOT NULL,
[Gender] NVARCHAR (50) NOT NULL,
[Admin] INT NULL,
PRIMARY KEY CLUSTERED ([Username] ASC)
);
SELECT GO * from User_Info;
Alter table User_Info
add column id int NOT NULL auto_increment Unique Key;
The error for this CREATE is:
Only 1 statement is allowed per batch, such as 'GO', might be required between statements
and the error for the column is:
Incorrect syntax near 'column'
I need an auto ID system so I can progress with my site, how can I fix this mess?
Below code worked for me:
CREATE TABLE user_Info (
[admin] int IDENTITY(1,1) PRIMARY KEY,
[Username] NVARCHAR (50) NOT NULL,
[Password] NVARCHAR (50) NOT NULL,
[Firstname] NVARCHAR (50) NOT NULL,
[Lastname] NVARCHAR (50) NOT NULL,
[Email] NVARCHAR (50) NOT NULL,
[Country] NVARCHAR (50) NOT NULL,
[Phone] NVARCHAR (50) NOT NULL,
[Gender] NVARCHAR (50) NOT NULL,
);
The code for T-SQL is:
CREATE TABLE dbo.UserInfo (
UserName nvarchar(50) not null,
PRIMARY KEY CLUSTERED (UserName ASC)
);
SELECT * FROM UserInfo;
GO
ALTER TABLE dbo.UserInfo ADD id int not null IDENTITY(1,1);
However, it might be preferable to make id your primary key and add a constraint to user name if you want that to be unique.
You need to add a batch separator, and remove column keyword, and replace auto_increment Unique Key with identity(1,1) unique
CREATE TABLE [dbo].[User_Info] (
[Username] NVARCHAR (50) NOT NULL,
[Password] NVARCHAR (50) NOT NULL,
[Firstname] NVARCHAR (50) NOT NULL,
[Lastname] NVARCHAR (50) NOT NULL,
[Email] NVARCHAR (50) NOT NULL,
[Country] NVARCHAR (50) NOT NULL,
[Phone] NVARCHAR (50) NOT NULL,
[Gender] NVARCHAR (50) NOT NULL,
[Admin] INT NULL,
PRIMARY KEY CLUSTERED ([Username] ASC)
);
GO
SELECT * from User_Info;
GO
Alter table User_Info
add id int identity(1,1) not null unique;
GO
When adding the column, specify IDENTITY along the a UNIQUE constraint specification to ensure unique values independent of the primary key. The example below also specifies a constraint name, which is a best practice instead of auto-generated constraint names.
CREATE TABLE [dbo].[User_Info] (
[Username] NVARCHAR (50) NOT NULL,
[Password] NVARCHAR (50) NOT NULL,
[Firstname] NVARCHAR (50) NOT NULL,
[Lastname] NVARCHAR (50) NOT NULL,
[Email] NVARCHAR (50) NOT NULL,
[Country] NVARCHAR (50) NOT NULL,
[Phone] NVARCHAR (50) NOT NULL,
[Gender] NVARCHAR (50) NOT NULL,
[Admin] INT NULL,
PRIMARY KEY CLUSTERED ([Username] ASC)
);
ALTER TABLE dbo.User_Info
ADD id int NOT NULL IDENTITY CONSTRAINT UQ_User_Info_id UNIQUE;
GO

Can a value be a primary and a foreign key?

I have the following tables in my SQL Server database.
Customer is a subtype of User thus I made a reference from Customer to User.
I also want to link my customer to my reservation. I'd like to use the Foreign key in the Customer table as a PK for this relation. When I write this out in SQL Server, I get this error under "(userid)":
Invalid column 'userid'
How can I make this relation?
Create table [dbo].[User]
(
[id] int PRIMARY KEY IDENTITY (1,1) NOT NULL,
[name] varchar(50) NOT NULL,
[password] nvarchar(100) NOT NULL,
)
Create table [dbo].[Customer]
(
[userid] int FOREIGN KEY REFERENCES [dbo].[User](id) NOT NULL,
[street] varchar(40) NOT NULL,
[housenumber] varchar(10) NOT NULL,
[postalcode] varchar(10) NOT NULL,
[phonenumber] varchar(20) NOT NULL,
[email] varchar(30) NOT NULL,
)
Create table [dbo].[Reservation]
(
[id] int PRIMARY KEY IDENTITY (1,1) NOT NULL,
[datum] date NOT NULL,
[prijs] decimal NOT NULL,
[levertijd] datetime NOT NULL,
[ophaaltijd] datetime NOT NULL,
[leverAdres] varchar(60) NOT NULL,
[klantId] int FOREIGN KEY REFERENCES [dbo].[Customer](userid) NOT NULL
)
yes this is possible, try it like this
Create table [dbo].[Customer]
(
[userid] int not null,
[street] varchar(40) NOT NULL,
[housenumber] varchar(10) NOT NULL,
[postalcode] varchar(10) NOT NULL,
[phonenumber] varchar(20) NOT NULL,
[email] varchar(30) NOT NULL,
constraint PK_UserID primary key ([userid]),
constraint FK_Customer_User foreign key (userid) references [User] (id)
)
But I have to say that userid seems like an odd primary key for a table called Customer
I would so something like this :
Create table [dbo].[Customer]
(
[CustomerID] int not null identity,
[userid] int not null,
[street] varchar(40) NOT NULL,
[housenumber] varchar(10) NOT NULL,
[postalcode] varchar(10) NOT NULL,
[phonenumber] varchar(20) NOT NULL,
[email] varchar(30) NOT NULL,
constraint PK_CustomerID primary key ([CustomerID]),
constraint FK_Customer_User foreign key (userid) references [User] (id)
)
Create table [dbo].[Reservation]
(
[id] int PRIMARY KEY IDENTITY (1,1) NOT NULL,
[datum] date NOT NULL,
[prijs] decimal NOT NULL,
[levertijd] datetime NOT NULL,
[ophaaltijd] datetime NOT NULL,
[leverAdres] varchar(60) NOT NULL,
[klantId] int FOREIGN KEY REFERENCES [dbo].[Customer](customerid) NOT NULL
)

I need to create a view in the database

I need to create a view in the database when two columns are refer from same table. I can create a view like this:
CREATE VIEW [dbo].[ViewJournal]
AS
SELECT
J. d, J.date, J.drac, L.name as draccount, J.crac,
L.name as craccount, J.dramt, J.cramt, J.lf,
J.description, J.voucherType, J.reg_date, J.last_update,
J.active
FROM
Journal J, Ledger L
WHERE
J.drac = L.Id
But the result doesn't not show actual result.
Here, crac and drac are refered to from Ledger table.
Journal table:
CREATE TABLE [dbo].[Journal]
(
[Id] DECIMAL (18) IDENTITY (1, 1) NOT NULL,
[date] DATETIME NULL,
[drac] DECIMAL (18) NULL,
[crac] DECIMAL (18) NULL,
[dramt] DECIMAL (18, 2) NULL,
[cramt] DECIMAL (18, 2) NULL,
[reg_date] DATETIME NULL,
[last_update] DATETIME NULL,
[active] INT NULL,
[lf] VARCHAR (50) NULL,
[description] NVARCHAR (150) NULL,
[voucherType] VARCHAR (50) NULL,
[sales] VARCHAR (50) NULL,
[purchase] VARCHAR (50) NULL,
[cash_paymentno] VARCHAR (50) NULL,
[cash_receiptno] VARCHAR (50) NULL,
[expense] VARCHAR (50) NULL,
[income] VARCHAR (50) NULL,
[advance] VARCHAR (50) NULL,
[remunaration] VARCHAR (50) NULL,
CONSTRAINT [PK_Journal] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Ledger] FOREIGN KEY ([drac]) REFERENCES [dbo].[Ledger] ([Id]) ON DELETE CASCADE ON UPDATE CASCADE
);
Ledger table:
CREATE TABLE [dbo].[Ledger]
(
[Id] DECIMAL (18) IDENTITY (1, 1) NOT NULL,
[name] NVARCHAR (50) NULL,
[type] NVARCHAR (50) NULL,
[classification] VARCHAR (50) NULL,
[realornominal] VARCHAR (50) NULL,
[reg_date] DATETIME NULL,
[last_update] DATETIME NULL,
[active] DECIMAL (2) NULL,
[depree] VARCHAR (50) NULL,
CONSTRAINT [PK_Ledger] PRIMARY KEY CLUSTERED ([Id] ASC)
);
In your current query, you're joining on J.drac = L.Id, which means that L will always be the record which is referenced in J.drac, regardless of the value of J.crac.
The way I understand it, you want to reference two different records in the Ledger table. You need two joins for that.
SELECT
J.Id, J.date, J.drac, D.name as draccount, J.crac,
C.name as craccount, J.dramt, J.cramt, J.lf,
J.description, J.voucherType, J.reg_date, J.last_update,
J.active
FROM Journal J
INNER JOIN Ledger D ON J.drac = D.Id
INNER JOIN Ledger C ON J.crac = C.Id

Foreign Key confusion

I am new to database and making a gym management system, I implemented a database from the tutorials on www.homeandlearn.co.uk. I have completed the project without foreign keys. Now I have to link tables but I am getting this error:
Update cannot proceed due to validation errors.
Please correct the following errors and try again.
SQL71516 :: The referenced table '[dbo].[member_info]' contains no primary or candidate keys that match the referencing column list in the foreign key. If the referenced column is a computed column, it should be persisted.
I do not know what is this error about. Please tell me how to fix this? Do i have to create a new database now or I can still use the foreign keys in the same database? I am using Visual Studio 2012. All help will be appreciated. Thanks in advance.
Cheers,
I do have a primary key, and I have set it to increment by 1. see this is my table.
CREATE TABLE [dbo].[member_info] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[memberName] NVARCHAR (50) NULL,
[father_name] NVARCHAR (50) NULL,
[age] NCHAR (10) NULL,
[address] NVARCHAR (50) NULL,
[contact] NVARCHAR (50) NULL,
[height] NVARCHAR (50) NULL,
[weight] NVARCHAR (50) NULL,
[chest] NVARCHAR (50) NULL,
[triceps_biceps] NVARCHAR (50) NULL,
[waist] NVARCHAR (50) NULL,
[shoulders] NVARCHAR (50) NULL,
[thighs] NVARCHAR (50) NULL,
[calves] NVARCHAR (50) NULL,
[instructor] NVARCHAR (50) NULL,
[date_of_admission] DATE NULL,
[photo] IMAGE NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
CONSTRAINT [FK_member_info_instructor_info] FOREIGN KEY ([instructor]) REFERENCES
[instructor_info]([instructor])
);
This is my member_info and below is my instructor_info table:
CREATE TABLE [dbo].[instructor_info] (
[InstructorID] INT IDENTITY (1, 1) NOT NULL,
[instructor] NVARCHAR (50) NULL,
[father_name] NVARCHAR (50) NULL,
[age] NCHAR (10) NULL,
[address] NVARCHAR (MAX) NULL,
[contact] NVARCHAR (50) NULL,
[height] NCHAR (10) NULL,
[weight] NCHAR (10) NULL,
[chest] NCHAR (10) NULL,
[triceps_biceps] NCHAR (10) NULL,
[waist] NCHAR (10) NULL,
[shoulders] NCHAR (10) NULL,
[thighs] NCHAR (10) NULL,
[calves] NCHAR (10) NULL,
[memberName] NVARCHAR (50) NULL,
[date_of_admission] DATE NULL,
[photo] IMAGE NULL,
PRIMARY KEY CLUSTERED ([InstructorID] ASC)
);
This is your table instructor_info
[dbo].[instructor_info]
PRIMARY KEY CLUSTERED ([InstructorID] ASC)
So if you want to reference that primary key from your table member_info, you must reference that exact column name (InstructorID).
So your current FK constraint won't work - you need to reference that column name, and you must use the same datatype.
Change your table member_info to use
[Instructor_ID] INT
(instead of the [instructor] NVARCHAR(50) column) and then change your FK constraint to:
CONSTRAINT [FK_member_info_instructor_info]
FOREIGN KEY ([instructor_ID])
REFERENCES [dbo].[instructor_info]([Instructor_ID])
Any foreign key in a table must reference the other table's primary key (or a unique constraint) - it cannot just reference any column you like....
You have to create primary keys for your tables in order to reference them in your foreign keys. Below an example of creating a table with a primary key, so you don't stumble on this error later on.
CREATE TABLE member_info (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM;
Link for MYSQL documentation on this: http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
EDIT: since my answer was posted, the tag changed from mysql to mssql, as well as a snippet to provide more info. For history purposes, I'm adding the code below to answer the new question.
CREATE TABLE [dbo].[instructor_info] (
[InstructorID] INT PRIMARY KEY IDENTITY (1, 1) NOT NULL,
[instructor] NVARCHAR (50) NULL,
[father_name] NVARCHAR (50) NULL,
[age] NCHAR (10) NULL,
[address] NVARCHAR (MAX) NULL,
[contact] NVARCHAR (50) NULL,
[height] NCHAR (10) NULL,
[weight] NCHAR (10) NULL,
[chest] NCHAR (10) NULL,
[triceps_biceps] NCHAR (10) NULL,
[waist] NCHAR (10) NULL,
[shoulders] NCHAR (10) NULL,
[thighs] NCHAR (10) NULL,
[calves] NCHAR (10) NULL,
[memberName] NVARCHAR (50) NULL,
[date_of_admission] DATE NULL,
[photo] IMAGE NULL
);
CREATE TABLE [dbo].[member_info] (
[Id] INT PRIMARY KEY IDENTITY (1, 1) NOT NULL,
[memberName] NVARCHAR (50) NULL,
[father_name] NVARCHAR (50) NULL,
[age] NCHAR (10) NULL,
[address] NVARCHAR (50) NULL,
[contact] NVARCHAR (50) NULL,
[height] NVARCHAR (50) NULL,
[weight] NVARCHAR (50) NULL,
[chest] NVARCHAR (50) NULL,
[triceps_biceps] NVARCHAR (50) NULL,
[waist] NVARCHAR (50) NULL,
[shoulders] NVARCHAR (50) NULL,
[thighs] NVARCHAR (50) NULL,
[calves] NVARCHAR (50) NULL,
[instructor] INT FOREIGN KEY REFERENCES instructor_info(InstructorID),
[date_of_admission] DATE NULL,
[photo] IMAGE NULL
);
For you to have a foreign key referencing a table you must have a primary key on that table. Check your member_info table and make sure there is one.