Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

MYSQL: How to insert NULL values in Foreign Key field

$
0
0
How can I insert a null value in foreign key field using MySql database

I have the following data model and environment is, Hibernate 3.0(with annotations) and MySQL 5.5

DB Model:
Create table ROLE(
role_id bigint(10) auto_increment,
primary key(role_id)
);
Create table user(
userId bigint(10) not null auto_increment,
role_id bigint(10) null,
Primary key(userid),
Constraint fk1 foreign key(role_id) references ROLE(role_id)
);

I am trying to insert a USER record in database and leaving the role field empty though my role field references ROLE.ROLE_ID. While creating the User I will not have Role information. I am using Hibernate 3.0 with annotations and it is throwing error when I leave USER.ROLE_ID field empty.

Is there a way to leave foreign key blank/insert null values in it?

Viewing all articles
Browse latest Browse all 13329

Trending Articles