Inserting Char value into SQL table - sql

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.

Related

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.

SQL Error: The Insert statement conflicted with the CHECK constraint

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 '.

How to name NOT NULL constraint in CREATE TABLE query

For a test tomorrow we're told to name our constraints
I know it's possible to create a constraint when you use ALTER TABLE
but can you add a name to a not null constraint when you CREATE TABLE?
f.e.
CREATE TABLE test (
test1 VARCHAR
CONSTRAINT nn_test1 NOT NULL (test1)
)
I get an error when trying to run this query. Am I writing it wrong?
The error I get is
ERROR: syntax error at or near "NOT"
LINE 3: CONSTRAINT nn_test1 NOT NULL (test1))
^
SQL state: 42601
Character: 56
You have two options to define a named not null constraint:
Inline with the column:
CREATE TABLE test
(
test1 VARCHAR CONSTRAINT nn_test1 NOT NULL,
test2 integer --<< no comma because it's the last column
);
Or at the end of columns as an out-of-line constraint. But then you need a check constraint:
CREATE TABLE test
(
test1 VARCHAR,
test2 integer, --<< comma required after the last column
constraint nn_test1 check (test1 is not null)
);
This has become irrelevant, since you're not using SQL Server
First of all, you should always specify a length for a VARCHAR. Not doing so (in SQL Server variables, or parameters) may result in a string of just exactly ONE character in length - typically NOT what you want.
Then, you need to just specify the NOT NULL - there's no need to repeat the column name (actually this is the error) - if you're specifying the CONSTRAINT "inline" with the column definition (which is a perfectly legal and in my opinion the preferred way of doing this).
Try this code:
CREATE TABLE test
(
test1 VARCHAR(50)
CONSTRAINT nn_test1 NOT NULL
)
At least this is the CREATE TABLE statement that works in T-SQL / SQL Server - not sure about PostgreSQL (don't know it well enough, don't have it at hand to test right now).
I, a_horse_with_no_name, the two syntax:
constraint nn_test1 check (test1 is not null)
and
test1 VARCHAR CONSTRAINT nn_test1 NOT NULL
are equivalent ? performance correctly ecc.
Because in first case the SQL server exception return the name nn_test so the system know exactly error.

SQL error primary key incorrect values

I want to import data with an SQL query. Here is my very stripped down version of the code, which still gives me the error #1062 - Duplicate entry '2147483647' for key 'PRIMARY':
CREATE TABLE mytable (`uid` INTEGER PRIMARY KEY);
INSERT INTO `mytable` VALUES (30046454912);
INSERT INTO `mytable` VALUES (30057490115);
INSERT INTO `mytable` VALUES (30061940182);
INSERT INTO `mytable` VALUES (30078940114);
There are other columns, which I have excluded from this example since it seems to go wrong with the primary key.
Server type is MariaDB, executing SQL from phpMyAdmin.
I'm sure I am overlooking something ... Any help is appreciated, thanks!
30046454912 is greater than the maximum integer value of 2147483647, so the insert changes the value to the maximum integer value. In order to get this to work, change your uid column to a long type.

Duplicate key error using nvarchar when adding new row with same number but leading 0

I want to insert a row into a table where in the primary key column I have a value of 3719. However, in the same table I want to add details with 03719 but I am getting an error:
The duplicate key value is (3719).
The datatype of that column is nvarchar.
I think your problem arises from the fact that you forgot to wrap the string value to be inserted in single quotes, so it is treated like a number, which is auto-converted to nvarchar.
INSERT INTO Table (Column) VALUES (03719)
is equivalent to
INSERT INTO Table (Column) VALUES (3719)
The value (because of the column type) is then converted to the string '3719'. Of course you then get a duplicate key error.
Check the quotes. If you're calling this from an application, use parameterized queries!
Correct statement to insert the records is like
Create table test (id nvarchar(25) primary key, name varchar(20))
insert into test values('111','hello')
insert into test values('0111','hello')
select * from test
You're forgetting to put the quotes.
If you do not put the quotes, you will get the below kind of error
Violation of PRIMARY KEY constraint 'PK__test__3213E83F2C904DEB'.
Cannot insert duplicate key in object 'dbo.test'. The duplicate key value is (111).