Unable to get a Foreign key - sql-server

USE Kudler_FF
ALTER TABLE Employee_Tbl
ADD CONSTRAINT FK_Employee_Tbl
FOREIGN KEY (JobTitle) REFERENCES Job_Tbl (JobTitle);
Message I am getting:
Msg 547, Level 16, State 0, Line 2
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Employee_Tbl". The
conflict occurred in database "Kudler_FF", table "dbo.Job_Tbl", column
'JobTitle'.
What did I add or not add

You usually only get an error adding a foreign key constraint when the constraint would be violated by the current data.
In other words, you probably have a value in Employee_Tbl(JobTitle) that does not exist in Job_Tbl(JobTitle).
You would not be able to add such a constraint until your data is modified so that a violation would not occur.
Find the values for JobTitle in Employee_Tbl that don't exist in Job_Tbl and then add them to that latter table.
I'm not sure of the exact syntax for SQL Server but you could start with:
select distinct JobTitle from Employee_Tbl
where JobTitle not in (
select distinct JobTitle from Job_Tbl
)

Related

Keep getting incorrect syntax near keyword CONSTRAINT in ALTER TABLE when trying to create foreign keys for a table?

I am currently I am working in sql server and have the following code. I am trying to create two foreign keys under the 'ASSIGNMENT' table, as well as create a primary key for the 'ASSIGNMENT' table. However, I keep running into problems when trying to execute my codes for creating the foreign keys and when trying to create the primary key.
For the foreign keys, my code is:
ALTER TABLE dbo.ASSIGNMENT ALTER COLUMN ProjectID INTEGER;
ADD CONSTRAINT PROJECT_FK FOREIGN KEY (ProjectID) REFERENCES dbo.
PROJECT (ProjectID);
ALTER TABLE dbo.ASSIGNMENT ALTER COLUMN EmployeeNumber CHAR(25);
ADD CONSTRAINT EMPLOYEE_FK FOREIGN KEY (EmployeeNumber) REFERENCES
dbo.EMPLOYEE (EmployeeNumber);
The message that pops up is:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'CONSTRAINT'.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'CONSTRAINT'.
My instructor wants us to use ALTER table in order to create the foreign keys, but I am unsure how to create them for the assignment table specifically. I have been able to create foreign keys for employee and project tables under their respective tables, but am unsure how to create them for this specific table.
For trying to create the primary key, I was not given a primary key and am unsure how to proceed. My code thus far is:
ALTER TABLE dbo.ASSIGNMENT ADD CONSTRAINT ASSIGNMENT_PK PRIMARY KEY
(ID);
The error message that pops up is:
Msg 1911, Level 16, State 1, Line 1
Column name 'ID' does not exist in the target table or view.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint or index. See previous errors.
I am unsure how to create a primary key for this 'ASSIGNMENT' table because within the ERD provided by the instructor, there are 3 columns for ProjectID, EmployeeNumber, and Hours Worked. The first two are also foreign keys, but there is no explicit assignment pk given. Thus, I am unsure how to create a primary key for this table, and what the command would be under the ALTER table, as instructed by my instructor.
Any help understanding where I am going wrong on any of my codes, and guidance would be greatly appreciated! Thanks!
You cannot have ADD and also ALTER keywords in an ALTER TABLE !
The right syntax to add a constraint is :
ALTER TABLE dbo.ASSIGNMENT
ADD CONSTRAINT EMPLOYEE_FK
FOREIGN KEY (EmployeeNumber)
REFERENCES dbo.EMPLOYEE (EmployeeNumber);
This came from SQL ISO Standard SQL...

How to delete a column which is the primary key in SQL Server

I'm getting an error while trying to delete the primary key column from the table.
id - primary key column
Table OLTMS_0B8DF2
Query
ALTER TABLE OLTMS_0B8DF2
DROP CONSTRAINT id;
GO
Error
Msg 3728, Level 16, State 1, Line 1
'id' is not a constraint.
Msg 3727, Level 16, State 0, Line 1
Could not drop constraint. See previous errors.
You need to specify the existing constraint name rather than the column name to drop the constraint.
You can determine the PK constraint name using the SSMS object browser or with the T-SQL script below. A best practice is to explicitly name constraints (e.g. PK_OLTMS_0B8DF2) rather than rely on auto-generated constraint names. That makes subsequent DDL easier.
SELECT name
FROM sys.key_constraints AS pk
WHERE
pk.parent_object_id = OBJECT_ID(N'OLTMS_0B8DF2')
AND type = 'PK';
If you have foreign key constraints referencing the table, you'll need to similarly drop those too.

Error while adding a foreign key constraint

Msg 547, Level 16, State 0, Line 2
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint
"AB_TRANS_FK3". The conflict occurred in database "DEV", table
"dbo.PRODUCT", column 'ID'.
Hello all,
I need your help with this error. I am trying to add a foreign key constraint here using the following query
ALTER TABLE [dbo].[AB_TRANS] WITH CHECK ADD CONSTRAINT [AB_TRANS_FK3] FOREIGN KEY([ID])
REFERENCES [dbo].[PRODUCT] ([ID])
Check your table to see which row(s) are causing the issue, then you'll need to fix them. This should help:
SELECT *
FROM
dbo.AB_Trans A
WHERE
NOT EXISTS (SELECT * FROM dbo.Product P WHERE P.ID = A.ID)
If there isn't a match, are you using "0" for the ID or some other dummy value? This could certainly cause the problem as you should be using NULL in those cases.

How do I get rid off error on adding Foreign Key to a table which contains data

I am using the below written query to add a foreign key to an Existing Field with data:
ALTER TABLE ServiceDetails
ADD CONSTRAINT fk_ServiceDetails
FOREIGN KEY (OCF_ID)
REFERENCES OCF_Commerce(OCF_Internal_ID);
But I am getting an error after executing the query. What am I missing?
The error message is:
Msg 547, Level 16, State 0, Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "fk_ServiceDetails". The conflict occurred in database "TrulyDB", table "dbo.OCF_Commerce", column 'OCF_Internal_ID'.
You probably have inconsistent data in your tables
try running
select *
from ServiceDetails
where OCF_ID not in (select OCF_Internal_ID from OCF_Commerce)
This will output all rows with ID absent in master table

SQL Constraint-Insert Fails

I have two table table1 and table2. I have a record in table1. I want to insert record into table2. but it comes up with the following Exception.
Msg 547, Level 16, State 0, Line 2
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Table2_<Column3>". The conflict occurred in database "<databaseName>", table "table1", column 'Id'.
The constraint on the table2 is like this.
ALTER TABLE [dbo].[table2] ADD CONSTRAINT [DF__table2__name__0B91BA14] DEFAULT ((0)) FOR [column4]
The error shows that you also have a foreign key constraint and you are trying to insert a row into a child table that doesn't have corresponding record in master
The exception message is telling you which constraint is causing the issue. It's against Column3 (at least that's what the name suggests) and is a foreign key constraint against table1.
You're not showing any sample SQL but it would appear you're trying to insert data in to table2 where the foreign key value specified for Column3 does not exist in table1.
Your Comment helped me.
I have used this Query to insert data into the new database from the old databse.
INSERT INTO Database.[dbo].[Table2] ([colmn1]
,[colmn2]
,[colmn3]
,[columns4]
)
SELECT [colmn1]
,[colmn2]
,[colmn3]
,[columns4]
FROM [OtherDatabase].[dbo].[Table2]
But there is a constarint on colum3 which is foreign key on Table1.
I have changed the colmn3 of table1 to same Guid as otherDatabase Guid and it works.
Thanks All

Resources