Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7491425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:03:10+00:00 2026-05-29T16:03:10+00:00

I have these database === Invoices === id status description === Invoice Items ===

  • 0

I have these database

  === Invoices ===
   id
   status
   description

   === Invoice Items ===
   id
   invoice_id (FK)
   item_name
   description

To make this table I have made this MySQL command

CREATE TABLE IF NOT EXISTS `nt_invoices` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `status` varchar(45) NOT NULL DEFAULT '',
  `description` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=24 ;


CREATE TABLE IF NOT EXISTS `nt_invoice_items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `invoice_id` int(11) NOT NULL,
  `item_name` varchar(45) NOT NULL,
  `description` text NOT NULL,
  PRIMARY KEY (`id`),
  KEY `invoice_id` (`invoice_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

My problem is that I want to declare a foreign key in the invoice_items table and to make the invoice_id the foreign key of invoices table id. So how to write that command? Any help and suggestions will be highly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:03:11+00:00Added an answer on May 29, 2026 at 4:03 pm

    You should have innodb engine type for using foreign keys.

    CREATE TABLE IF NOT EXISTS `nt_invoice_items` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `invoice_id` int(11) NOT NULL references nt_invoices(id)
      `item_name` varchar(45) NOT NULL,
      `description` text NOT NULL,
      PRIMARY KEY (`id`),
      KEY `invoice_id` (`invoice_id`)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8;
    

    OR if you want to use cascaded update delete:

    CREATE TABLE IF NOT EXISTS `nt_invoice_items` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `invoice_id` int(11) NOT NULL,
      `item_name` varchar(45) NOT NULL,
      `description` text NOT NULL,
      PRIMARY KEY (`id`),
      KEY `invoice_id` (`invoice_id`),
     FOREIGN KEY (invoice_id) REFERENCES  nt_invoices(id)
       ON DELETE CASCADE
       ON UPDATE CASCADE,
    
    ) ENGINE=INNODB DEFAULT CHARSET=utf8;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a database called Poll and have these two table structures. poll
I have a database table (named Topics) which includes these fields : topicId name
I have a database with Users. Users have Items. These Items can change actively.
i have a stack of messages in database table. i want to send these
I have a table 'invoices' in my development database (sqlite3) populated with a small
In a MySQL database I have a table with the following primary key PRIMARY
Say I have a database containing Books and Users and these users have certain
Say you have a string 3.4564. A sample set in the database has these
I have a field in my database that contain comma separated values these values
I have a DataTable which I select from database (Well, these data cross several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.