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 7537009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:43:52+00:00 2026-05-30T06:43:52+00:00

I have two dependent tables as CREATE TABLE posts ( post_id int(11) unsigned NOT

  • 0

I have two dependent tables as

CREATE TABLE posts
(
post_id int(11) unsigned NOT NULL AUTO_INCREMENT,
title varchar(255),
PRIMARY KEY(post_id)
) ENGINE=InnoDB

CREATE TABLE post_meta
(
post_id int(11) unsigned REFERENCES posts(post_id) ON DELETE CASCADE,
info varchar(255),
PRIMARY KEY(post_id)
) ENGINE=InnoDB

Question 1: After INSERTing into posts, post_meta does not accept value with error Duplicate entry XX for key 'PRIMARY. How should I modify the table structure?

Question 2: How can I set to create a corresponding row in post_meta upon INSERT INTO posts? I mean creating an empty row (only having id of the FK) in post_meta when creating a row in posts. In other words having the same number of rows in two columns without any INSERT into the second column.

  • 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-30T06:43:53+00:00Added an answer on May 30, 2026 at 6:43 am

    Your current implementation looks as out of normalized form. Are you sure you need to keep the data separated to two different tables? Maybe

    CREATE TABLE posts
    (
    post_id int(11) unsigned NOT NULL AUTO_INCREMENT,
    title varchar(255),
    info varchar(255),
    PRIMARY KEY(post_id)
    ) ENGINE=InnoDB
    

    will do?

    Speculating: if you’re doing it that way because of security issues then MySQL supports Column-level privileges.

    If normalizing your data is unacceptable for some reasons then you can just make post_id primary key in both tables (don’t make it foreign key!) and add INSERT and DELETE triggers on table posts which will insert or delete corresponding rows from post_meta.

    UPDATE

    You said in comment that you can have multiple records in post_meta table corresponding to single record in posts. That changes a lot:

    1. You MUST NOT use primary key on post_id in table post_meta. Primary key MUST be unique in table scope.
    2. You can set up your foreign key (the one you already tried to) – this will ensure that metadata is deleted automatically with posts.
    3. If you do need PRIMARY KEY defined on post_meta then you shoud add a new (possibly auto_increment field) to post_meta table and use it as primary key. (Also, table can exist even without primary key but it’s against most DB guidelines)
    4. If you need to create a meta record(s) automatically for each post then you can add an INSERT trigger on posts as I’ve already suggested. Another approach is using a stored procedure (and only stored procedure) for adding records to posts – and in this SP you can write some SQL to insert necessary records to post_meta.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two tables: CREATE TABLE `table1` ( `ID` int(11) NOT NULL
I have two identical tables and need to copy rows from table to another.
From the regular aspnet_Users table I have created two sub-tables with one to one
I have two tables: 1) Streams 2) Comments Inside Streams I want to create
I have two SQL Server tables. One table has four nvarchar(50) nullable columns and
There are two ways to create a constraint, Method A: ALTER TABLE dbo.<tablename> ADD
I have two dependent models with one to many association class Post < ActiveRecord::Base
My data model contains two tables with composite primary keys and an associative table.
I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
I have two tables, we'll call them Foo and Bar , with a one

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.