SQL Error: The Insert statement conflicted with the CHECK constraint - sql

Just learning the SQL language. Trying to insert data into a table but keep getting the following error:
"The INSERT statement conflicted with the CHECK constraint
"JOB_JOBCODE". The conflict occurred in database "qwerty", table
"dbo.Job", column 'jobCode'."
Code:
This is the table I'm creating, nothing fancy
CREATE TABLE Job(
jobCode char(4),
jobdesc varchar(50),
--ADD CONSTRAINT PK JPB CODE
CONSTRAINT PK_JobCode PRIMARY KEY(jobCode) ,
CONSTRAINT JOB_JOBCODE CHECK (jobCode in ('CAST’, ‘ENGI’, ‘INSP’, ‘PMGR')) );
This is the data that I'm inserting
INSERT INTO Job VALUES ('CAST', 'Cast Member);
Any help would be appreciated, Im not sure what I'm doing wrong

Use this query for inserting values into Job table
INSERT INTO Job VALUES ('CAST', 'Cast Member');

Run this to fix your check literal error:
ALTER TABLE Job DROP JOB_JOBCODE
ALTER TABLE Job ADD CONSTRAINT JOB_JOBCODE CHECK (jobCode IN ('CAST', 'ENGI', 'INSP', 'PMGR'))
Then use the explicit column form of the insert:
INSERT INTO Job (jobCode, jobdesc)
VALUES ('CAST', 'Cast Member')
Make sure to use the proper literal delimiter '.

Related

Azure SQL DB allow NULL values for UNIQUE constraint columns

How can I set a UNIQUE constraint in Azure SQL database that allows NULL values in the column?
The below workaround from SQL Server doesn't seem to work in Azure SQL:
CREATE UNIQUE NONCLUSTERED INDEX [IX_Email] ON [dbo].[Users]([Email] ASC)
WHERE ([Email] IS NOT NULL);
The error, which is in the comments is telling you the problem:
Violation of UNIQUE KEY Constraint "UQ_Users_5120...". Cannot insert duplicate key in object 'dbo.Users'. The duplicate key value is ().
Firstly, notice that the error states "UNIQUE KEY Constraint", emphasis mine. Not Index, Constraint. Also note that name of said constraint: UQ_Users_5120... That isn't the name of the object you created (IX_Email).
You can replicate this problem with the following:
CREATE TABLE dbo.SomeTable (SomeColumn varchar(10) NULL);
GO
--Create a UNIQUE Constraint
ALTER TABLE dbo.SomeTable ADD CONSTRAINT UQ_SomeColumn UNIQUE (SomeColumn);
GO
--Create a filtered unique index
CREATE UNIQUE NONCLUSTERED INDEX UX_SomeColumn_NotNull ON dbo.SomeTable(SomeColumn)
WHERE SomeColumn IS NOT NULL;
GO
--Initial Insert
INSERT INTO dbo.SomeTable (SomeColumn)
VALUES('asdfasd'),
('asdasd'),
(NULL);
GO
--Insert another dupe, non NULL value. Fails
--This violates constraint 'UQ_SomeColumn'
INSERT INTO dbo.SomeTable (SomeColumn)
VALUES('asdfasd');
GO
--Insert another dupe, NULL value. Fails
--This violates constraint 'UQ_SomeColumn'
INSERT INTO dbo.SomeTable (SomeColumn)
VALUES(NULL);
GO
You can fix this my dropping your UNIQUE CONSTRAINT. We don't have the full name, but for the above example it would be the following:
ALTER TABLE dbo.SomeTable DROP CONSTRAINT UQ_SomeColumn;
GO
And then we can test again:
--Insert another dupe, non NULL value. Fails
--Cannot insert duplicate key row in object 'dbo.SomeTable' with unique index 'UX_SomeColumn_NotNull'. The duplicate key value is (asdfasd).
INSERT INTO dbo.SomeTable (SomeColumn)
VALUES('asdfasd');
--Insert another dupe, NULL value. Success
INSERT INTO dbo.SomeTable (SomeColumn)
VALUES(NULL);
GO
Notice as well, that the error for the duplicate value is completely different now. It mentions a unique index, not a unique key constraint, and has the name of the unique filtered index created, not something else.

My INSERT statement conflicted with the FOREIGN KEY constraint

enter image description here
Those are my diagrams - I create procedure
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[hasta]
#Ad nchar(50),
#Soyadı nvarchar(100),
#TcKimlik nchar(11),
#DogumTarihi varchar(8),
#TelefonNo nvarchar(11)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].Hastalar ([Ad], [Soyadı], [TcKimlik], [DogumTarihi], [TelefonNo])
VALUES (#Ad, #Soyadı, #TcKimlik, #DogumTarihi, #TelefonNo)
END
After I want add data in Hastalar table, I write
exec hasta 'Emir','Yılmaz','35635993564','1995.11.19','05347331085'
but I get this error
Msg 547, Level 16, State 0, Procedure hasta, Line 13
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Hastalar_Testler". The conflict occurred in database "hastane", table "dbo.Testler", column 'TestID'. The statement has been terminated.
In your table [dbo].Hastalar, it has a foreign key reference to another table. The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.
If you have SQL Server Management Studio, open it up and sp_help '[dbo].Hastalar'. See which column that FK is on, and which column of which table it references. You're inserting some bad data.
What part of the error do you not understand?
The message is saying that one of the columns -- "TestId" -- is referring to another table. And the value being inserted is not already in that table.
You are not explicitly inserting a value for TestId, so that value is being set using a default constraint or trigger. I would suggest that you explicit provide the value in the insert -- even if that value is NULL.

Is adding a column and a constraint at the same time while modifying an Oracle table possible?

I have an assignment asking me to add a column to a table and give it a constraint.
"Add a column named Base_Salary with a datatype of NUMBER(7,2) to the
store_reps table. Ensure that the amount entered is above 0."
I thought it was pretty straight forward, but maybe I have to ALTER the table twice, once for the new column, and again for adding the constraints? That seems redundant and inefficient.
I've tried taking off the second 'ADD' and moving around the parentheses, and moving the commas, it seems there is a similar error for everything I do.
This is my attempt:
ALTER TABLE store_reps;
ADD (Base_Salary NUMBER(7, 2)),
ADD CONSTRAINT store_reps_Base_Salary CHECK (Base_Salary>0);
I get an error
ADD CONSTRAINT store_reps_Base_Salary CHECK (Base_Salary>0)
ERROR report -
UNKNOWN COMMAND
and another:
Error starting at line : 74 in command -
ALTER TABLE store_reps
ADD (Base_Salary NUMBER(7, 2)),
CONSTRAINT store_reps_Base_Salary CHECK (Base_Salary>0)
Error report -
Any help is greatly appreciated. I just started SQL a few weeks ago, and am very much noob.
Your query contains two syntax problems that need to be fixed:
get rid of the comma before CONSTRAINT
get rid of the extra pair of braces around Base_Salary NUMBER(7, 2)
Working example:
create table store_reps (id number);
ALTER TABLE store_reps
ADD Base_Salary NUMBER(7, 2)
CONSTRAINT store_reps_Base_Salary CHECK (Base_Salary>0);
create table tr (col1 number);
alter table tr add col2 varchar2(100) constraint tr_cons CHECK (col2 = 'a');
DB Fiddle demo
Cheers!!

Check length constraint is not working in Oracle11g sqlplus

I've been trying to put length constraint , so that it would not take string whose length is more or less than 5
Create Table Statement:
create table exp(id char(10),name varchar(50));
Add Constraint Statement:
alter table exp add constraint exp1 check(length(id)=5);
Insert Statement:
insert into exp(id,name) values('10001','Abhi');
But whenever i try to insert data like the above written it shows
insert into exp(id,name) values('10001','Abhi')
*
ERROR at line 1:
ORA-02290: check constraint (VIT.EXP1) violated
Change char(10) to varchar2(10):
create table exp(id varchar2(10),name varchar(50));
A char(10) column has always a length of 10. Regardsless your insert statement. That's why you get the error.

Inserting Char value into SQL table

I created a SQL table an enforced check constraints on it, but now when I try to insert data I get an error message.
create table BranchTel
(
BrRegNo varchar(10) REFERENCES Branch(BrRegNo),
TelNo char(12)
PRIMARY KEY(BrRegNo)
)
ALTER TABLE BranchTel Add Constraint BranchTelTelNo
Check(TelNo LIKE '[0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
Insert statement
insert into BranchTel values('BG-205','940112571963')
Error message
The INSERT statement conflicted with the CHECK constraint "BranchTelTelNo". The conflict occurred in database "StudentDetails", table "dbo.BranchTel", column 'TelNo'.
The statement has been terminated.
Insert statement
insert into BranchTel values('BG-205','94-011-2571963')
Error message
String or binary data would be truncated.
The statement has been terminated.
Please help me
Your check constraint is 14 characters long (you need to count the - as well), while the field size is 12.
Additionally, 940112571963 does not conform to the pattern xx-xxx-xxxxxxx you have defined in your check constraint.
You need to change the field size to 14 and when inserting make sure the dashes are in the right place:
insert into BranchTel values('BG-205','94-011-2571963')
Insert statement insert into BranchTel values('BG-205','94-011-2571963') Error message String or binary data would be truncated. The statement has been terminated.
Here the value 94-011-2571963 length is greater than 12 which obviously violates the check constraint.