Could not create constraint or index. See previous errors - sql

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

Related

How can I add two different features as a foreign key to my SQL Column?

I have a Car table in my Sql Data and every car has own BrandId that declared every Brands in "Brands" table as a foreign key. I'm just wondering what if I want to add two different BrandId together in my one Car>BrandId Column, what would I do? How can it be possible for adding two features as a foreign key in one spesific column?
Here is my Car Tables Code
CREATE TABLE [dbo].[Cars] (
[CarId] INT IDENTITY (1, 1) NOT NULL,
[CarName] VARCHAR (255) NULL,
[ColorId] INT NULL,
[BrandId] INT NULL,
[ModelYear] VARCHAR (255) NULL,
[DailyPrice] DECIMAL (18) NULL,
[Details] VARCHAR (255) NULL,
PRIMARY KEY CLUSTERED ([CarId] ASC),
FOREIGN KEY ([ColorId]) REFERENCES [dbo].[Colors] ([ColorId]),
FOREIGN KEY ([BrandId]) REFERENCES [dbo].[Brands] ([BrandId])
);
And here te code of my Brands table;
CREATE TABLE [dbo].[Brands] (
[BrandId] INT IDENTITY (1, 1) NOT NULL,
[BrandName] VARCHAR (255) NULL,
PRIMARY KEY CLUSTERED ([BrandId] ASC)
);
You can simply create your table as follows :
CREATE TABLE [dbo].[Cars] (
[CarId] INT IDENTITY (1, 1) NOT NULL,
[CarName] VARCHAR (255) NULL,
[ColorId] INT NULL,
[BrandId1] INT NULL,
[BrandId2] INT NULL,
[ModelYear] VARCHAR (255) NULL,
[DailyPrice] DECIMAL (18) NULL,
[Details] VARCHAR (255) NULL,
PRIMARY KEY CLUSTERED ([CarId] ASC),
FOREIGN KEY ([ColorId]) REFERENCES [dbo].[Colors] ([ColorId]),
FOREIGN KEY ([BrandId1]) REFERENCES [dbo].[Brands] ([BrandId]),
FOREIGN KEY ([BrandId2] REFERENCES [dbo].[Brands] ([BrandId])

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

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)
);

foreign key not adding to booking table, can anyone spot the issue

Create table Hotel (
Hotel_no varchar (10) NOT NULL,
Hotel_name varchar (10) NOT NULL,
Address varchar (600) NOT NULL,
Primary key (Hotel_no) );
Create table Room (
Room_no varchar (10) NOT NULL,
Hotel_no varchar (10) NOT NULL,
type varchar (40) NOT NULL,
price varchar (20) NOT NULL,
Primary key (Room_no, Hotel_no) );
Create table Guest (
Guest_no varchar (10) NOT NULL,
Guest_name varchar (10) NOT NULL,
Address varchar (600) NOT NULL,
Primary key (Guest_no) );
Create table Booking (
date_from date,
date_to date,
Hotel_no varchar (10) NOT NULL,
Guest_no varchar (10) NOT NULL,
Room_no varchar (10) NOT NULL,
Primary key (date_from, Hotel_no, Guest_no),
Foreign key(Room_no)references Room(Room_no));
when i try adding the foreign key to the booking table it gives an ORA-02270 error, and i cant seem to figure out the problem
any assistance would be greatly appreciated.
Your foreign key on Room should reference the primary key Room(Room_no, Hotel_no)
If the primary key is a set of columns then the foreign key should also be a set of columns corresponding to the composite key columns.
Try below code :
Create table Booking ( date_from date, date_to date, Hotel_no varchar (10) NOT NULL, Guest_no varchar (10) NOT NULL, Room_no varchar (10) NOT NULL, Primary key (date_from, Hotel_no, Guest_no), Foreign key(Room_no,Hotel_no)references Room(Room_no,Hotel_no);

Movie Ticket System (DB)

I'm making a online movie reservation system on MVC and I am stuck at the database part on how the tables would link to each other. Does it seem alright?
ERD
CREATE TABLE [dbo].[Member]
(
[memberID] INT IDENTITY (1, 1) NOT NULL,
[memFirstName] VARCHAR (50) NULL,
[memLastName] VARCHAR (50) NULL,
[memEmailAddress] VARCHAR (50) NULL,
[memPassword] VARCHAR (15) NULL,
[memTelephone] CHAR (10) NULL,
[memAddress] VARCHAR (70) NULL,
PRIMARY KEY CLUSTERED ([memberID] ASC)
);
CREATE TABLE [dbo].[Movie]
(
[movieID] INT IDENTITY (1, 1) NOT NULL,
[movieName] VARCHAR (100) NULL,
[movieCategory] CHAR (10) NULL,
PRIMARY KEY CLUSTERED ([movieID] ASC)
);
CREATE TABLE [dbo].[Reservation]
(
[resID] INT IDENTITY (1, 1) NOT NULL,
[memberID] INT NOT NULL,
[showID] INT NOT NULL,
PRIMARY KEY CLUSTERED ([resID] ASC),
FOREIGN KEY ([memberID]) REFERENCES [dbo].[Member] ([memberID]),
FOREIGN KEY ([showID]) REFERENCES [dbo].[Show] ([showID])
);
CREATE TABLE [dbo].[Show]
(
[showID] INT IDENTITY (1, 1) NOT NULL,
[showDateTime] DATETIME NOT NULL,
[movieID] INT NULL,
CONSTRAINT [PK_Show] PRIMARY KEY CLUSTERED ([showID] ASC),
FOREIGN KEY ([showID]) REFERENCES [dbo].[Movie] ([movieID])
);

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.