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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:54:48+00:00 2026-05-19T12:54:48+00:00

Allow my to elaborate on the question with an example. I am writing from

  • 0

Allow my to elaborate on the question with an example. I am writing from the
perspective that

(1) bytecodes should not be used to implement the logic already
implemented (hopefully more efficiently) in the database engine (e.g., if we
need to filter out 20 lines out of 500 coming out as the result of an SQL query,
we should be writing a better where clause), and that

(2) I have only a conceptual
understanding of foreign keys (e.g., they automatically create, manage and enforce
the constraints required to maintain data integrity across different tables).

Now, lets consider a simple schema with 4 tables and 15 columns in them
as follows (assume all columns are not null):

people
    pid bigint autoinc PK
    fname   varchar(32)
    lname   varchar(32)
    dob date
    sex char(1)
    addyID  bigint FK to address(aid)

relationship
    relof   bigint FK to people(pid)
    relto   bigint FK to people(pid)
    relis   tinyint

address
    aid bigint autoinc PK
    street  varchar(128)
    zipcode int FK to z2cs(zip)

z2cs
    zip int PK
    city    varchar(64)
    state   char(2)

Expectations (Please answer True/False to the following 6)

  1. It is possible to create the table relationship as above with foreign keys
    pointing to different rows of the same table
  2. Adding a row to relationship throws SQLException if either relof or relto
    is missing in people
  3. If we add a row to address, we do not need to check if the zipcode is there in z2cs –
    missing zip throws an SQLException
  4. Attempt to delete a row from address throws an exception if aid is used in people
  5. Attempt to delete a row from people causes error if pid is used in relationship in
    either of the columns relto or relof
  6. An SQL statement exists that lets you find which columns in a table are foreign keys
    and which table(column) do they refer to (I know “describe tablename” doesn’t do it,
    although I feel that it should)

If the answer to any of the above is false, can you please guide me to finding
what exactly is the contract for foreign keys in MySql. Thank you.

PS: If you answer True to [6], would you please share what is that statement.

Bonus question (unrelated to FK):

How would you write the create statement for the relationship table that satisfies the following constraints:

  1. relof and relto cannot be the same
  2. Combination of relof and relto is unique, i.e.,
    if their is a row with [relof=584,relto=7823], you cannot insert another row with
    either [relof=584,relto=7823] or [relof=7823,relto=584]
  • 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-19T12:54:49+00:00Added an answer on May 19, 2026 at 12:54 pm

    1. It is possible to create the table relationship as above with foreign keys pointing to different rows of the same table

    NO, a FK can only point to one row. But a row can be referenced by many other rows, like, an adress can be referenced by many people.

    2. Adding a row to relationship throws SQLException if either relof or relto is missing in people

    YES.

    3. If we add a row to address, we do not need to check if the zipcode is there in z2cs – missing zip throws an SQLException

    YES.

    4. Attempt to delete a row from address throws an exception if aid is used in people

    YES, if there is no Cascade Delete in the FK, which then automatically would delete the
    entry in people instead of failing.

    5. Attempt to delete a row from people causes error if pid is used in relationship in either of the columns relto or relof

    YES, see 4.

    6. An SQL statement exists that lets you find which columns in a table are foreign keys and which table(column) do they refer to (I know “describe tablename” doesn’t do it, although I feel that it should)

    AFAIK. There should be an information schema in MySQL where you can query the information from.

    B1. relof and relto cannot be the same

    Use a Trigger on update or insert to validate this. PostgreSQL knows CHECK constraints, which might ba also available in MySQL, but else a trigger is your choice.

    B2. Combination of relof and relto is unique

    A combined UNIQUE INDEX on both columns is your friend here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Interfaces allow you to create code that defines the methods of classes that implement
The caption may sound a little weird. Allow me to elaborate using an example.
I am not sure exactly what I should name this question. I just started
I've been told that it's not good design to allow nulls in field definitions...
I allow users to submit a question, and they should be able to have
To allow caching a PHP generated file, I want to make sure, that the
I am sorry if the title is unclear. Allow me to elaborate further. Firstly,
OK, here's a rather elaborate code golf challenge: Implement a game of Reversi (Othello).
I have been told that a handle is sort of a pointer, but not,
I know that many people, at a first glance of the question, may immediately

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.