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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:05:52+00:00 2026-06-02T03:05:52+00:00

Suppose this is the SQL code of the table: CREATE TABLE `user_is_in` ( `id_user`

  • 0

Suppose this is the SQL code of the table:

CREATE  TABLE `user_is_in` (
  `id_user` INT NULL ,
  `id_city` INT NULL ,
  `when` DATETIME NULL ,
  INDEX `fk_user` (`id_user` ASC) ,
  INDEX `fk_city` (`id_city` ASC) ,
  CONSTRAINT `fk_user`
    FOREIGN KEY (`id_user` )
    REFERENCES `user` (`id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_city`
    FOREIGN KEY (`id_city` )
    REFERENCES `city` (`id` )
    ON DELETE SET NULL
    ON UPDATE CASCADE)
ENGINE = InnoDB;

The use of the table is to store kind of Foursquare checkins (A user was registered on one place at one time).

Now I have 2 options:

  • Create an unique index with the 3 fields and no primary key:

    UNIQUE INDEX id_user_is_in_UNIQUE (id_user ASC, id_city ASC, when ASC)

  • Create an additional classic id autoincrement field

I don’t like the 2nd option because I want to create queries over users and cities (i.e.: search all users who made a checkin in a city on one date)

Thanks in advance.

  • 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-02T03:05:53+00:00Added an answer on June 2, 2026 at 3:05 am

    You should do both. Add an auto-incrementing primary key and add a uniqueness constraint against all three columns. With a separate system assigned keys you make it easier to define foreign keys to this new table, if and when eventually required. And having the uniqueness constraint against all three columns will ensure that duplicate data is not created.

    Some quick side notes:

    • Since this is an association table, your columns really should be
      NOT NULL.
    • You probably want to CASCADE delete’s of a city down to the
      association table.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have this table.. CREATE TABLE `appointments` ( `idappointments` int(11) NOT NULL AUTO_INCREMENT,
I have this SQL Server 2008 UDT: CREATE TYPE [dbo].[IdentityType] AS TABLE( [Id] [int]
Suppose, I have a table like this- Code | something ------------------ C01 abc C02
Suppose I have this database table (some sample code below) that stores the relationship
Suppose I have a table like CREATE TABLE associacao ( id bigserial NOT NULL,
Suppose this C# code: using (MemoryStream stream = new MemoryStream()) { StreamWriter normalWriter =
I've seen second one in another's code and I suppose this length comparison have
I'm still a learning user of SQL-SERVER2005. Here is my table structure CREATE TABLE
Please look at this query. select name form table Let suppose this returns 100
Suppose this is my URL route: (r'^test/?$','hello.life.views.test'), How do I make it so that

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.