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

Primary key collision in scope of one trasaction

I have a postgresql database, which heavily relies on events from the outside, e.g. administrator changing / adding some fields or records might trigger a change in overall fields structure in other tables. There lies the problem, however, as…
winwin
  • 958
  • 7
  • 25
1
vote
1 answer

How to fix "UNIQUE constraint failed" from VACUUM (also INTEGRITY_CHECK fails)

I use an app which creates this SQLite DB with this table: CREATE TABLE expense_report (_id INTEGER PRIMARY KEY, ...) And for some reason that _id (which is the ROWID) became invalid in that DB. When I scan the table I see that the last rows got an…
Mariano Desanze
  • 7,847
  • 7
  • 46
  • 67
1
vote
0 answers

on duplicate key update on id. Correct way to pass the id

I have a table with a primary key id_form that is an autoincrement field. I have a form that will insert into that table. I want to reuse the same form to edit the records. So my form has an hidden input that has value='' when I am inserting a new…
Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
1
vote
1 answer

primary key used in one to many relationship in postgresql for project

I am creating a final year project-online shopping system, but I am having doubt regarding the primary key to be used in tables as it has to get auto inserted in other tables to extract data. Example if I am having two tables shop and products here…
1
vote
1 answer

Sorting Cassandra Query Output Data

I am sure this is the most common problem with Cassandra. Nevertheless: I have this example table: CREATE TABLE test.test1 ( a text, b text, c timestamp, id uuid, d timestamp, e decimal, PRIMARY KEY ((a),c, b, id)) WITH CLUSTERING ORDER BY (b ASC,…
jlb333333
  • 371
  • 2
  • 13
1
vote
2 answers

Am I missing something if I use my entity class without @Id in Spring Data JDBC?

I am new to spring. I just tried successfully using an entity class without @Id in Spring Data JDBC Custom query was added in my repository for retrieving data from 2 mysql tables and returning an entity having the joined table data. If I plan to…
Ammamon
  • 467
  • 1
  • 10
  • 18
1
vote
1 answer

Define Position based on lowest ID and Foreign Key in MySQL

I am currently facing the following issue: I have 1 Table with my Broker Trading Data similar to this: TickerId Id Ticker Shares OrderType ... ... ... ... ... 01.01.20 ABC 5 ABC 500 Buy 01.01.20 ABC 6 …
user11390995
1
vote
6 answers

Database PK-FK design for future-effective-date entries?

Ultimately I'm going to convert this into a Hibernate/JPA design. But I wanted to start out from purely a database perspective. We have various tables containing data that is future-effective-dated. Take an employee table with the following…
1
vote
3 answers

ms sql - find relational tables in database

Is there a way to find the relationship between tables in a database via a query or GUI in SQL Express 2005? Other then manually looking for matching primary/foreign keys in tables is there any easier way to do it?
tdjfdjdj
  • 2,391
  • 13
  • 44
  • 71
1
vote
3 answers

Should I use a surrogate key (id= 1) or natural primary key (tag='sqlalchemy') for my sqlalchemy model?

On the database side, I gather that a natural primary key is preferable as long as it's not prohibitively long, which can cause indexing performance problems. But as I'm reading through projects that use sqlalchemy via google code search, I almost…
Profane
  • 1,128
  • 8
  • 13
1
vote
1 answer

Unique composite index with primary key

I am using postgresql. In each table, uuid is set as the primary key in the "id" column. At this time, in some conditional clause When using "id" and "name" as conditions, Is a composite index of "id" and "name" ["unique"] meaningful? Since the "id"…
hyundeock
  • 445
  • 1
  • 6
  • 15
1
vote
3 answers

Get List of Primary Key Columns in Snowflake

I have a composite primary key. In Information_Schema.Table_Constraints I can see 1 record for this key but it does not have information on columns which make this primary key. where can i find list of columns for constraints?
1
vote
2 answers

Converting primary key into unique key in mysql

I've a table like this : +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | num | int(11) | NO | PRI | NULL | | | t | int(11) |…
user14151025
1
vote
3 answers

MySQL. Primary key in a relational table. Unique id or multiple unique key?

Primary key in relational tables. Composite primary key or unique primary key in those pure relational tables? Which design would you recommend to use in MySQL for high performance? See diagram Technical advantages and disadvantages! Thanks…
1
vote
0 answers

Can I auto-increment the Foreign Key column in my db table so that it matches the main table's auto-incrementing Primary Key column in SQL Server?

Create statement for my first table: 'users': CREATE TABLE users ( uid INT NOT NULL IDENTITY PRIMARY KEY, firstName VARCHAR(50) NOT NULL, lastName VARCHAR(50) NOT NULL, middle VARCHAR(50) NOT NULL, ssn VARCHAR(12) NOT NULL, …
user8050845
1 2 3
99
100