1

不知道是数据库问题还是什么。对于我数据库中的所有表,对于声明为主键的列在查询时采用 NULL 值或默认值

INSERT INTO TABLE (emp_name,emp_addr,emp_contact,salary) 
values ('Jack','127 N F 14 Stillwater','3456786543',8)

表由 组成emp_id(primary key), emp_name, emp_addr, emp_contact and salary

如果我想插入一个元组

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)
4

2 回答 2

2

您不能将主键字段设为空。并且您必须将 not null 作为主键。否则,拥有主键有什么意义?:) 请将您的创建查询更改为:

CREATE table tblblah(ID NOT NULL PRIMARY KEY,...等等

;

INSERT INTO table tblblah(ID, ....等等

;

根据您的表架构填充它。

于 2012-11-13T05:40:53.030 回答
0

在创建表时添加AUTO_INCREMENT选项

create table Employee
(
emp_id int(11) AUTO_INCREMENT primary key,
emp_name varchar(25) not null,
emp_addr varchar(40) not null,
emp_contact varchar(10),
salary float(2)
)ENGINE=INNODB;
于 2012-11-13T05:39:50.250 回答