Questions tagged [primary-key]

A key is a set of attributes that is irreducibly unique and non-nullable within a table.

A key is a set of attributes that is irreducibly unique and non-nullable within a table. Irreducible means that all the attributes of the key are necessary to guarantee uniqueness - remove any one attribute and the uniqueness property would be lost. A key may consist of zero, one or more attributes and a relational table (relation variable) must have at least one key and may have more than one.

When a table has more than one key then by convention one of the keys is designated the primary one (meaning it is the preferred identifier or has some other significance for the database designer or user). The others are called secondary keys or alternate keys.

In relational database design the primary key is frequently the one referenced by foreign keys in other tables. That's not always the case however. In principle a primary key is no different in features or function from a non-primary key. The primary-key tag is therefore appropriate for any discussion about keys in general.

Keys are more formally known as candidate keys or minimal superkeys.

Attributes of a key are called prime attributes. Attributes which are not part of any key are called non-prime.

In SQL, keys are optional on tables. Primary keys are defined in the ISO SQL Standard, through the PRIMARY KEY constraint. The syntax to add such a constraint to an existing table is defined in SQL:2003 like this:

ALTER TABLE <TABLE identifier> 
      ADD [ CONSTRAINT <CONSTRAINT identifier> ] 
      PRIMARY KEY ( <COLUMN expression> {, <COLUMN expression>}... )

The primary key can also be specified directly during table creation. In the SQL Standard, primary keys may consist of one or multiple columns. Each column participating in the primary key is implicitly defined as NOT NULL. Note that some DBMS require explicitly marking primary-key columns as NOT NULL.

CREATE TABLE TABLE_NAME (
    id_col  INT,
    col2    CHARACTER VARYING(20),
    ...
    CONSTRAINT tab_pk PRIMARY KEY(id_col),
    ...
)

If the primary key consists only of a single column, the column can be marked as such using the following syntax:

CREATE TABLE TABLE_NAME (
    id_col  INT  PRIMARY KEY,
    col2    CHARACTER VARYING(20),
    ...
)
4532 questions
1
vote
1 answer

SQL - delete two primary keys by reference in join table

I have 3 tables: user --> primary key = user_id account --> primary key = account_id user_account_join_table --> foreign key = user_id, foreign key = account_id How can I delete all associated entries from all 3 tables when I only know…
Peter D.
  • 11
  • 2
1
vote
1 answer

How to insert a List to a table with Dao Room in Android when there is a primarykey

I have a data class contains id and code. And I have a List contains codes only. how to insert the codes into table without making up ids? Actually I do not need the id column at all, but it seems Room requires a primary key, and codes cannot be…
caibirdcnb
  • 275
  • 5
  • 18
1
vote
5 answers

SQL - Can I base a table primary key on more than one field, requiring the uniqueness of first OR second value?

Let us say we have a product catalog. Table has following fields: CompanyID and ProductID . I want the table primary key to be based on those two fields, so as a record is unique when its pair of CompanyID and ProductID fields have unique…
Mariusz
  • 481
  • 2
  • 4
  • 11
1
vote
2 answers

has_many :through with :primary_key on join table not working

In my Rails 3 project, I have a user model with a self referential join, through the follow model. I want to use this join table to find activity related to the followed user. I have almost everything set up correctly, except that the query…
Justin
  • 1,203
  • 8
  • 15
1
vote
0 answers

Can I generate database id-s both serverside and client side with JPA?

I have the following Id description of my entity in JPA: @Id @SequenceGenerator(name = "my_sequence", sequenceName = "my_seq") @GeneratedValue(generator = "my_sequence", strategy = GenerationType.SEQUENCE) private long id; and it works from Java.…
Dims
  • 47,675
  • 117
  • 331
  • 600
1
vote
1 answer

How to read Snowflake primary keys into python

This question is a bit related to another question: Get List of Primary Key Columns in Snowflake. Since INFORMATION_SCHEMA.COLUMNS does not provide the required information regarding the primary keys. And the method proposed by Snowflake itself,…
1
vote
1 answer

Is there a way to start from max in a primary key in PostgreSQL if I import data from another db?

I have an old MSAccess DB and I want to convert it to PostgreSQL. I found DBeaver very useful. Some operations may be done by hand. This is the case of Primary Keys. You must manually set the primary keys. I didn't found another way to do it. So in…
1
vote
1 answer

Combination of UUID and integer value as primary key in one database in MySQL

Could you give me any advice, if will be good to mix UUID as primary key and auto increments integer value for different tables in that same database? We want to rebuild database which will be bigger in time and will works in distributed…
1
vote
2 answers

How to find range in Cassandra Primary key?

Use case: Find maximum counter value in a specific id range I want to create a table with these columns: time_epoch int, t_counter counter The frequent query is: select time_epoch, MAX t_counter where time_epoch >= ... and time_epoch < ... This is…
1
vote
1 answer

SQLITE: Merge tables from different databases with Primary Key Constraint

I want to concatenate/append tables from 2 different databases (database X and Y). ATTACH "all\tab_X.db" AS db1; ATTACH "all\tab_Y.db" AS db2; INSERT INTO db1.PAYMENT SELECT * FROM db2.PAYMENT However this is giving me this error: UNIQUE…
Pavitran
  • 47
  • 1
  • 7
1
vote
2 answers

Primary and foreign keys

Simple question, can't seem to find an answer on Google. I'm normalizing a database and I'm converting one of the tables to 2nd Normal Form. Question is: can the primary key of a table also be a foreign key in that same table? If it makes it…
MGZero
  • 5,812
  • 5
  • 29
  • 46
1
vote
1 answer

Why up to index_granularity * 2 extra rows will be read for ClickHouse primary key?

According to ClickHouse documentation, when reading a single range of the primary key, up to index_granularity * 2 extra rows in each data block can be read. Why index_granularity * 2 extra rows? I can't figure this out. Take the (CounterID, Date)…
Lacey
  • 11
  • 1
1
vote
2 answers

How to deal with null Primary Key in Mysql?

I know sql doesn't allow null values in pk. but assume we have some nulls in pk, what we can do about it in practice? my thoughts check other tables to see if we can recover nulls in pk ignore null values, as they are not needed to be…
Momoco
  • 41
  • 5
1
vote
1 answer

NHibernate on a table with two "primary" keys

I'm learning NHibernate in order to layer it over a rather peculiar legacy database. Other applications use the same live database, so I can't make changes that will affect them. I've run into a problem because one table, which represents hardware…
JohnCC
  • 615
  • 7
  • 20
1
vote
3 answers

SQL Composite Key

I am a SQL newbie and here is a question that I cannot find a solution yet. Say I have tables User and Partner. I want to have a composite key for Partner, which is a pair of User's. So I can have keys (A,B) and (B,A) where A and B are both users. I…
user2812201
  • 437
  • 3
  • 7
1 2 3
99
100