我不知道它是數據庫還是什麼問題。對於所有在我的數據庫被聲明爲的主鍵的表中,列在表由emp_id(primary key), emp_name, emp_addr, emp_contact and salary
正在NULL值或默認值時,查詢像防止null值被插入到主鍵列的表中
INSERT INTO TABLE (emp_name,emp_addr,emp_contact,salary)
values ('Jack','127 N F 14 Stillwater','3456786543',8)
。
如果我試圖插入一個元組
INSERT INTO EMPLOYEE values (NULL,'Jack','127 N F 14 Stillwater','345678543',8)
,它拋出的錯誤是主鍵不能是被認爲是零。
我的表創建腳本..
create table Employee(
emp_id int(11) primary key,
emp_name varchar(25) not null,
emp_addr varchar(40) not null,
emp_contact varchar(10),
salary float(2)
)ENGINE=INNODB;
This is what is happening in my DB
create table sale(sale_id varchar(10) primary key not null,sold_vehicle varchar(12),sm_id int(11), sale_date date)ENGINE=INNODB;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into sale values('s2389','AP28DJ5093',1290,'05-12-2010');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into sale values('s2420','AP28MY0545',1290,'24-10-2012');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from sale;
+---------+--------------+-------+------------+
| sale_id | sold_vehicle | sm_id | sale_date |
+---------+--------------+-------+------------+
| s2389 | AP28DJ5093 | 1290 | 0000-00-00 |
| s2420 | AP28MY0545 | 1290 | 0000-00-00 |
+---------+--------------+-------+------------+
2 rows in set (0.00 sec)
mysql> update sale set sale_id = null where sale_id = 's2389';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> select * from sale;
+---------+--------------+-------+------------+
| sale_id | sold_vehicle | sm_id | sale_date |
+---------+--------------+-------+------------+
| | AP28DJ5093 | 1290 | 0000-00-00 |
| s2420 | AP28MY0545 | 1290 | 0000-00-00 |
+---------+--------------+-------+------------+
2 rows in set (0.00 sec)
mysql> desc sale;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| sale_id | varchar(10) | NO | PRI | NULL | |
| sold_vehicle | varchar(12) | YES | | NULL | |
| sm_id | int(11) | YES | | NULL | |
| sale_date | date | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
你確定你沒有在主鍵上自動遞增? –
爲什麼不是你的主鍵不爲空 – smk