-2

I read that Primary key should be Unique and not null. Now when I create a table checking with depid as primary key, the table description says this according to me : the Null column value NO indicates that depid can't be Null but at the same time the Default column value Null says that default is Null

mysql> desc checking;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| depid | int(11) | NO   | PRI | NULL    |       |
| deppp | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.05 sec)

mysql> insert into checking values(1,2);
Query OK, 1 row affected (0.12 sec)

mysql> insert into checking(deppp) values(1);
ERROR 1364 (HY000): Field 'depid' doesn't have a default value

If that is the case why is the last query producing an error?

Edit The error got resolved once primary key column is labelled as Auto Increment. But that still doesn't explain why the Default column shows Null for a primary key column.

arjun gulyani
  • 669
  • 2
  • 8
  • 23
  • Possible duplicate of [MySQL columns with default null - stylistic choice, or is it?](https://stackoverflow.com/questions/12253415/mysql-columns-with-default-null-stylistic-choice-or-is-it) – Sam M Jan 19 '18 at 17:18

1 Answers1

2

I think you forgot adding the "auto increment" to your primary key, so there is no value added. Create your table like this :

CREATE TABLE checking (
    depid int NOT NULL AUTO_INCREMENT,
    deppp int(11),
    PRIMARY KEY (depid)
);
Léo R.
  • 2,620
  • 1
  • 10
  • 22