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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:36:43+00:00 2026-06-03T16:36:43+00:00

I created the table t1t2 which connects tables t1 and t2 as follows: CREATE

  • 0

I created the table t1t2 which connects tables t1 and t2 as follows:

CREATE TABLE t1t2(
id integer primary key,
t1_id integer,
t2_id integer,
foreign key(t1_id) references t1(id),
foreign key(t2_id) references t2(id));

Is it possible to define a constraint (restriction) that enables only unique values of tuple (t1_id, t2_id)? Or should I check this in the application?

  • 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-06-03T16:36:46+00:00Added an answer on June 3, 2026 at 4:36 pm
     CREATE UNIQUE INDEX idx_twocols ON t1t2(t1_id, t2_id)
    

    You will probably need to add NOT NULL to the declarations for each of the two columns.

    Alternatively, you could choose to forego the primary key column (if all you’re using it for is uniqueness) and create the primary key on the combination of t1_id and t2_id:

    CREATE TABLE t1t2(
    t1_id integer NOT NULL,
    t2_id integer NOT NULL,
    PRIMARY KEY (t1_id, t2_id),
    foreign key(t1_id) references t1(id),
    foreign key(t2_id) references t2(id));
    

    The PRIMARY KEY is a special case of a UNIQUE index. Using the composite PRIMARY KEY saves you one column and one index, but requires your application to know both t1_id and t2_id to retrieve a single row from the table.

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

Sidebar

Related Questions

I created table using this command : CREATE TABLE projects( p_id integer primary key
I created a table then added a foreign key reference to its primary key
I have just created this table: create table dbo.OrderTypes ( ID smallint primary key
I created a table without a primary key Like this: private static final String
I created a table tbl_Candidate , but I forgot to set the primary key
I'm currently working on the following homework: CREATE TABLE T_1 (COURSE_NO CHAR(8) PRIMARY KEY,
I have the following tables (example) t1 (20.000 rows, 60 columns, primary key t1_id)
I've got silly magic problem in rails! So, I've created table with foreign key
I have created 2 table.Brand and Product.In both table one id is primary key
I have created table and inserted values as below. create table mytable (id INT,

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.