Sql server error : The INSERT statement conflicted with the CHECK constraint - sql

The INSERT statement conflicted with the CHECK constraint "ck_str_member_no". The conflict occurred in database "C:\DOCUMENTS AND SETTINGS\KARTHIKEYAN\DESKTOP\KOK\DB\INFT3009_ASS1_C3104855.MDF", table "dbo.Members", column 'str_member_no'.
The statement has been terminated.
I am using .MDF file in my visual studio 2008 Express. How do I solve it?
My insert Procedure :
ALTER PROCEDURE [dbo].[AddNewAGCMember]
-- Add the parameters for the stored procedure here
#str_member_no varchar(6) = '',
#str_member_name varchar(50) = '',
#str_member_password varchar(10) = '',
#str_addr_apartment_no varchar(10) = NULL,
#str_addr_building_name varchar(50) = NULL,
#str_addr_street_name varchar(50) = NULL,
#int_postal_code int = NULL,
#str_country_name varchar(50) = NULL,
#int_contact_no int = NULL,
#str_email_addr varchar(100) = '',
#date_registration date = ''
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO Members
(str_member_no,
str_member_name,
str_member_password,
str_addr_apartment_no,
str_addr_building_name,
str_addr_street_name,
int_postal_code,
str_country_name,
int_contact_no,
str_email_addr,
date_registration)
VALUES
(#str_member_no,
#str_member_name,
#str_member_password,
#str_addr_apartment_no,
#str_addr_building_name,
#str_addr_street_name,
#int_postal_code,
#str_country_name,
#int_contact_no,
#str_email_addr,
#date_registration);
END
Table structure :
str_member_no, varchar(6), Unchecked
str_member_name, varchar(50), Unchecked
str_member_password, varchar(10), Unchecked
str_addr_apartment_no, varchar(10), Checked
str_addr_building_name, varchar(50), Checked
str_addr_street_name, varchar(50), Checked
int_postal_code, int, Checked
str_country_name, varchar(50), Checked
int_contact_no, int, Checked
str_email_addr, varchar(100), Unchecked
date_registration, date, Unchecked
Unchecked

Obviously, your insert is violating a check constraint. This is a constraint in your database the performs a specific check - that a numeric value is in a particular range, that a string is at most n characters long, or whatever.
To find out what the check constraint is, try this:
SELECT
name, definition
FROM
sys.check_constraints
WHERE
name = 'ck_str_member_no'
This will give you the expression that is being checked in the "definition" column.
From that expression, you should be able to determine why your insert is being rejected. Fix the problem and insert again.
If you really cannot fix your data, and if you do not need / want that check constraint in place, you can drop it:
ALTER TABLE dbo.Members
DROP CONSTRAINT ck_str_member_no
Marc

Apparently the INSERT is supplying a value for column 'str_member_no' which SQL has been instructed to refuse given the CHECK constraint. A check constraint is simply a condition imposed on one or several values of fields, before they can be added or inserted. For example, maybe the CHECK requires that all str_member_no be more than 1500 (the lower values being used for, say, administators...) or some other odd "business rule".
By inspecting, or posting here, the DDL for the underlying table creation, as well as the INSERT statement in question, you could get specific understanding of what is wrong with the value (or with the check predicate).
Reading the added code in the question, the str_member_no doesn't seem to be checked, then it may just be that the value passed is too long or null or some other generic reason for refusing it; the error would typically be different but since there is a check, the error shown is based on the assumption that the [custom] check failed.
At any rate, you should check what values are passed to the AddNewAGCMember() StoredProcedure; the problem probably lies there, in a field value that is "bad".

Check the following MySQL bug that has not yet been solved-
MySQL CHECK constraint bug

Related

Cannot insert the value NULL into column X, column X does not allow nulls. INSERT fails.

I'm new to SQL Server and I am getting this error "Cannot insert the value NULL into column 'Occupied', table 'DBProjectHamlet.dbo.tblGrave'; column does not allow nulls. INSERT fails. The statement has been terminated."
This is my code for the insert followed by the code to create the table
INSERT INTO tblGrave (GraveName)
SELECT Grave
FROM tblPlotsandOccupants
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'tblGrave' AND TABLE_SCHEMA = 'dbo')
DROP TABLE dbo.tblGrave;
GO
CREATE TABLE tblGrave
(
GraveID INT IDENTITY (1,1),
GraveName VARCHAR(MAX) NULL,
GraveTypeID INT NOT NULL,
PlotID INT NOT NULL,
Occupied BIT NOT NULL
)
I'm not trying to insert anything into column Occupied, I don't know why this is happening or how to fix it. I just want to insert values into tblGrave (GraveName). Any help would be great.
Exactly! You aren't doing anything with Occupied and that is the problem. The column is specified to be NOT NULL but has no default value. You are not inserting a value, so it gets the default. The default default is NULL, and that is not allowed.
One simple solution is:
INSERT INTO tblGrave (GraveName, Occupied)
SELECT Grave, 0
FROM tblPlotsandOccupants;
This fixes your immediate problem, but will then you will get an error on PlotId.
A more robust solution would add a default value for the NOT NULL columns and declare the rest to be nullable (the default). Something like this:
CREATE TABLE tblGrave (
GraveID INT IDENTITY (1,1) PRIMARY KEY,
GraveName VARCHAR(MAX),
GraveTypeID,
PlotID INT,
Occupied BIT NOT NULL DEFAULT 0
);
When you created your table, you defined that column as "NOT NULL" rather than allowing it to be null.
You need to either allow "Occupied" to be null, set a default value, or define the value that you want upon inserting.
You can even set the value to be ' ' which is blank but isn't null.
EDIT
Note #Gordon's answer for sql examples.

cannot change null to not null

The column name on table [dbo].[payment_info] must be changed from NULL to NOT NULL. If the table contains data, the ALTER script may not work.
To avoid this issue, you must add values to this column for all rows or mark it as allowing NULL values, or enable the generation of smart-defaults as a deployment option.
CREATE TABLE [dbo].[payment_info]
(
[name] VARCHAR (50) NOT NULL,
[card_no] VARCHAR (50) NULL,
[card_type] VARCHAR (50) NOT NULL,
[tel_no] VARCHAR (50) NULL,
[mob_no] VARCHAR (50) NULL,
[address] VARCHAR (MAX) NULL
);
I cannot change NULLto NOT NULL; when I update it's showing the above warning.
I am using visual studio 2013 asp.net and c#.
If table already exists and is fulfilled with data, you have to update all NULLs in column you want to change on some value which is not NULL. Then ALTER command should work wthout warnings and/or errors.
I am not really sure if I understood your problem correctly, but the warning says it all - you can't switch column to not nullable, if there are nulls already in the column.
You have to update and set some values to empty entries or set DEFAULT value
EDIT:
You should try first:
select *
from [dbo].[payment_info]
where name is null
and check if there are any problems
Right-click on your table in server explorer and click "new query". Type:
ALTER TABLE
table
ALTER COLUMN
column
int NOT NULL;
This error is produced by SSDT. This will happen if you have an existing table and you would like to add a new non-nullable column to it. In order to do so, you must have a default for this new column (the default can be some temporary value).
CREATE TABLE [dbo].[payment_info]
(
[WhateverColumn] VARCHAR (50) NOT NULL DEFAULT 'Foo',
-- and so on
);
If you want to change the default to a more meaningful value, you can write the script to update the table and set the column's value to a more meaningful value in a post deployment. In post deployment you can also now drop the default since it was temporary:
ALTER TABLE WhateverTable
ALTER COLUMN WhateverColumn DROP DEFAULT;
Now your deployment will succeed.
Note: If your column is a foreign-key column, the default has to exist in the parent table even if the value is temporary.

Stop double entry of data in database

I am using VS 2008 and SQL Server 2005. And the problem is that when I insert a new record which is string data. It continues on entering the same data which is already exiting in the table, again and again. But I want that where my insert query is running. I place the check there that it does not allow similar data in the table.
My scenario:
I have to decide on these two string columns: 'source' and 'destination'
If similar source and destination occur in any record we must stop we the entry on record.
Share the solution.
The easiest way to do it is by putting a 'UNIQUE constraint' on your database. Then, each time an SQL UPDATE or an SQL INSERT is executed, the database server would check the validity of the new SQL action and cancel it if it violates your data integrity constraing.
For example (copying from this SQL tutorial):
CREATE TABLE Persons
(
P_Id int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
If you want to add a UNIQUE constraint on two columns, you could use such a statement:
CREATE TABLE Example
(Col1 int NOT NULL,
Col2 int NOT NULL,
UNIQUE (Col1, Col2)
)
Hope I helped!

SQL Server trigger update column from another table

I am trying to create a fairly simple SQL Server trigger, hope someone can help.
I have a table with structure like this:
Table #1:
CREATE TABLE `teg_priority` (
`UCIDN` BIGINT(50) NULL DEFAULT NULL,
`CIDN` BIGINT(50) NOT NULL,
`CustomerName` VARCHAR(200) NOT NULL,
`NGM` VARCHAR(150) NULL DEFAULT NULL,
`Service_Manager` VARCHAR(150) NULL DEFAULT NULL,
`CBS` LONGTEXT NULL,
`Tag` VARCHAR(50) NOT NULL,
PRIMARY KEY (`CIDN`)
)
and another table (table #2):
CREATE TABLE `custalign` (
`UCIDN` BIGINT(20) NOT NULL,
`CIDN` BIGINT(20) NOT NULL,
`CustomerName` VARCHAR(255) NOT NULL,
PRIMARY KEY (`CIDN`)
)
I am trying to set up a trigger where every time a new record is inserted into the first table that the following query will be run as a trigger to update field UCIDN in table 1
update teg_priority
set teg_priority.UCIDN = (select UCIDN from custalign
where teg_priority.CIDN = custalign.CIDN)
The above query works i just don't know how to write it as a trigger statement.
Please help.
CREATE TRIGGER dbo.Teg_priority_after_insert
ON dbo.teg_priority AFTER INSERT
AS
UPDATE inserted
set inserted.UCIDN = (select UCIDN from custalign
where inserted.CIDN = custalign.CIDN)
That's your answer. You might consider a change in approach; assuming it doesn't require a total re-work of your process-flow. I can't really suggest more without knowing what you're ultimately trying to accomplish.
In SQL Server triggers, there is an inserted and a deleted table automatically-generated to which you may refer. Each respectively contains the new and old records as a result of whatever statement AFTER [INSERT],[UPDATE],[DELETE]. The inserted table is accessible to AFTER INSERT and UPDATE triggers, while the deleted table is accessible to AFTER UPDATE and DELETE triggers.
That might be more than you wanted to know, but I thought you'd benefit from a brief explanation of where the inserted table came from in my code.
[Insert all the usual caveats about trying not to use triggers wherever possible here.]
try out this..hope this will helps you
For MySQL
CREATE TRIGGER teg_priorityTrigger AFTER INSERT ON teg_priority
FOR EACH ROW
BEGIN
UPDATE inserted
set inserted.UCIDN = (select UCIDN from custalign
where inserted.CIDN = custalign.CIDN)
END
For SQL Server
CREATE TRIGGER teg_priorityTrigger ON dbo.teg_priority AFTER INSERT
AS
UPDATE inserted
set inserted.UCIDN = (select UCIDN from custalign
where inserted.CIDN = custalign.CIDN)
hope this will helps you...

Can't create new table

I'm new to SQL Server 2000 and face a problem. I want to make a new table but I encounter an error message with the following code:
create table Buku
(
Kode_Buku char(5) constraint PK_Kode_Buku Primary Key,
Judul_Buku varchar(10)not null,
Nama_Pengarang varchar(30) not null,
Penerbit varchar(30),
Kota_Terbit varchar(30) default,
Tahun_Terbit varchar(4) default,
Bahasa varchar(4) check,
Harga_Jual money,
)
and here's the error code:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'Buku'.
You have three problems:
You have multiple cases where you say default but don't specify anything
You have a check but don't specify anything
Your last column definition says money, with a trailing comma
Now, none of these lead to the exact error message you're getting, so maybe there is more you're not telling us (is there more code before the create table bit?), but these little syntax problems are far too localized to be useful in this Q & A format.
EDIT
I just ran this on a SQL Server 2000 instance and it worked just fine:
create table Buku
(
Kode_Buku char(5) constraint PK_Kode_Buku Primary Key,
Judul_Buku varchar(10) not null, -- added space here
Nama_Pengarang varchar(30) not null,
Penerbit varchar(30),
Kota_Terbit varchar(30), -- removed default here
Tahun_Terbit varchar(4), -- removed default here
Bahasa varchar(4), -- removed check here
Harga_Jual money -- removed comma here
)
So I'm not sure what you're doing differently, but I can't get the error message you are seeing with the information you've provided in the question. If you're still getting an error message with this code (and only this code), you'll need to provide more information, such as ##VERSION, what interface you're using to submit the create table statement to SQL Server, etc.