Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreign key showing null value

Tags:

mysql

I am trying to show the foreign key value that is extracted from another table. The codes that i've used:

Code for first table:

CREATE TABLE trip (
    tripID  INT(10) PRIMARY KEY,
    startTime   TIME,
    endTime     TIME,
    tripDate    DATE,
    price       VARCHAR(5),
    databaseID  INT(10),
    FOREIGN KEY (databaseID)
       REFERENCES databasestorage(databaseID)
);

code for second table:

CREATE TABLE databasestorage (
    databaseID  INT(10) PRIMARY KEY,                    
    location        VARCHAR(40)
);

After inserting the values into both tables, in the trip table, databaseID still shows as null while all other columns are correct. How do i make it such that databaseID in my trip table shows the value of databaseID from the databasestorage table?

like image 268
Dario Ongsono Avatar asked Jan 25 '26 10:01

Dario Ongsono


1 Answers

You need to pass primary key of databasestorage table while inserting data into trip table right?

INSERT INTO databasestorage(databaseID, location) VALUES('1021', 'Caulfield')

For trip table you need to explicit pass the value for databaseID column in trip table, foreign key will not automatically inserted

                                                        ---------v
INSERT INTO trip(tripID, startTime, endTime, tripDate, price,databaseID)
VALUES('101', '20:20:00', '20:40:00', '2016-10-22', '$5.25','1021')
                                                         -----^
like image 181
Jaydip Jadhav Avatar answered Jan 28 '26 03:01

Jaydip Jadhav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!