0

This is the code that created the table.

CREATE TABLE CUSTOMERS
(
    Customer_ID INT NOT NULL,
    CHECK(Customer_ID <= 11),
    First_Name varchar(20) NOT NULL,
    Last_Name varchar(30),
    Home_Street varchar(30),
    Home_City varchar(20),
    Home_State varchar(2),
    Home_Zip varchar(5),
    PhoneNumber varchar(11) NOT NULL
);

ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTOMERS PRIMARY KEY(Customer_ID);

Then I try to insert data (using this code) into the table and that is where I get this error.

INSERT INTO dbo.CUSTOMERS(Customer_ID, First_Name, Last_Name, Home_Street, Home_City, Home_State, Home_Zip, PhoneNumber)
VALUES (11223344556, 'John', 'Doe', '1234 Hand Street', 'Wahiawa', 'HI', 96786, 2535551267);

What am I doing wrong and what can I do to fix this issue?

5
  • Are you using MySQL or MS SQL Server? Commented Aug 30, 2018 at 6:58
  • I'm using MS SQL Server management studio. Commented Aug 30, 2018 at 7:01
  • 1
    Home_Zip and Phonenumber are VARCHAR , so you need to enclose the values in quotes ,e,g,'96786','2535551267') Commented Aug 30, 2018 at 7:03
  • 3
    Customer_ID is an int. The largest number it can contain is 2147483647. You're trying to insert something longer than that (and, to boot, you have a CHECK constraint that means the only positive numbers it will accept are 1-11) Commented Aug 30, 2018 at 7:05
  • So using CHECK function will only allow me to use numbers 1-10? I'm still trying to understand what I'm doing. I'm trying to set the int to 11. I thought what I was doing limited it to 11 digits. Commented Aug 30, 2018 at 7:09

3 Answers 3

3

AS per my understanding you are checking length of Customer_ID <=11 so you should mention len(Customer_ID)<=11 it will work and you should alter datatype of Customer_ID int to bigint

CREATE TABLE CUSTOMERS
    (
    Customer_ID bigINT NOT NULL,
    CHECK(len(Customer_ID)<=11),
    First_Name varchar(20) NOT NULL,
    Last_Name varchar(30),
    Home_Street varchar(30),
    Home_City varchar(20),
    Home_State varchar(2),
    Home_Zip varchar(5),
    PhoneNumber varchar(11) NOT NULL
    );
    ALTER TABLE CUSTOMERS
    ADD CONSTRAINT PK_CUSTOMERS
    PRIMARY KEY(Customer_ID);

    INSERT INTO dbo.CUSTOMERS(Customer_ID,First_Name,Last_Name,Home_Street,
    Home_City,Home_State,Home_Zip,PhoneNumber)
    VALUES(11223344556,'John','Doe','1234 Hand Street', 
    'Wahiawa','HI',96786,2535551267);
Sign up to request clarification or add additional context in comments.

1 Comment

@Icodin my answer was first and correct. Thank you for marking later posted same answer as right.
1

You need customer id to be bigint:

CREATE TABLE CUSTOMERS
(
Customer_ID BIGINT NOT NULL,
CHECK(Customer_ID<=11),
First_Name varchar(20) NOT NULL,
Last_Name varchar(30),
Home_Street varchar(30),
Home_City varchar(20),
Home_State varchar(2),
Home_Zip varchar(5),
PhoneNumber varchar(11) NOT NULL
);
ALTER TABLE CUSTOMERS
ADD CONSTRAINT PK_CUSTOMERS
PRIMARY KEY(Customer_ID);

1 Comment

how this answer will be correct still after seeing my answer you are not getting what i wrote check it once and comment
1

The problem may be due to CHECK(Customer_ID<=11) since Customer_ID id integer datatype the server may check for integer validation and not length validation. Try to change the validation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.