2

I have this table:

mysql> desc Customers;

+------------+------------------+------+-----+---------+-------+
| Field      | Type             | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| CustomerID | int(10) unsigned | NO   | PRI | NULL    |       |
| Name       | char(50)         | NO   |     | NULL    |       |
| Address    | char(100)        | NO   |     | NULL    |       |
| City       | char(30)         | NO   |     | NULL    |       |
+------------+------------------+------+-----+---------+-------+

Now, If I want to insert sample data:

mysql> insert into Customers values(null, 'Julia Smith', '25 Oak Street', 'Airport West');
ERROR 1048 (23000): Column 'CustomerID' cannot be null

I know I cannot make the ID null, but that should be job of mysql to set it numbers and increment them. So I try to simple not specifying the id:

mysql> insert into Customers (Name, Address, City) values('Julia Smith', '25 Oak Street', 'Airport West');
Field 'CustomerID' doesn't have a default value

Now I am in trap. I cannot make id null (which is saying for mysql "increment my ID"), and I cannot omit it, becuase there is no default value. So how should I make mysql to handle ids for me in new insertions?

3
  • What does your CREATE TABLE query look like? It is the job of MySQL to increment the number only for a field that is set to be auto_increment. If your CustomerID was auto_increment you'd see it in the Extra column. Commented Sep 7, 2020 at 12:15
  • 1
    Your field has no AUTO_INCREMENT. Commented Sep 7, 2020 at 12:16
  • Does this answer your question? Alter a MySQL column to be AUTO_INCREMENT Commented Sep 7, 2020 at 12:16

2 Answers 2

1

Primary key means that every CustomerID has to be unique. and you defined it also as NOT NULL, so that an INSERT of NULL is not permitted

instead of >| CustomerID | int(10) unsigned | NO | PRI | NULL |

Make it

CustomerID  BIGINT AUTO_INCREMENT PRIMARY KEY

and you can enter your data without problem

ALTER TABLE table_name MODIFY CustomerID BIGINT  AUTO_INCREMENT PRIMARY KEY
Sign up to request clarification or add additional context in comments.

Comments

0

@Milan,

Delete the CustomerID var from the table. And add this field again with the following details:

Field: CustomerID, Type: BIGINT(10), Default: None, Auto_increment: tick in the checkbox

Click SAVE button to save this new field in the table. Now I hopefully it will work while inserting the new record. Thanks.

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.