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

How can I add new item with auto-increment key in DynamoDB with Spring Boot

I want to make the primary key in DynamoDB an AutoIncrement Key like SQL. And after generating this, if there are missing items in sequence,(after deleting some items) +--------+---------------+ | id | name …
1
vote
3 answers

MySQL: size of VARCHAR as PK similarly as fast as an INT?

What would be a good/safe maximum length for a VARCHAR column as primary key not being much/any slower than an INTEGER ID using MySQL 5 + InnoDB on a 64 bit system? Note, that this PK should be assumed to be referenced by other tables, so it will…
Kawu
  • 13,647
  • 34
  • 123
  • 195
1
vote
1 answer

Duplicate Primary Key while doing latteral flattening in SNOWFLAKE

I am kind a new with working with arrays in SNOWFLAKE database. I am trying to load data into dimension tables in SNOWFLAKE database using merge statement where the primary keys of those dimension tables are generated in the staging table itself…
SMR
  • 401
  • 4
  • 15
1
vote
1 answer

SNOWFLAKE Sequence Creation Issue

In SNOWFLAKE database, I was trying to create 2 sequences under same name but with case sensitive and for one sequence I was trying to increment by 1 and the other sequence I was trying to increment based on the value from staging table. To explain…
1
vote
1 answer

postgres getting data where two column references same parent id

There are 3 table in postgres database CREATE TABLE tab_name ( name_id integer NOT NULL, cust_name character varying NOT NULL, -- contains names like david,jones,athur CONSTRAINT tab_name_pkey PRIMARY KEY (name_id) ) CREATE TABLE tab_rel ( …
kiranking
  • 306
  • 11
  • 29
1
vote
1 answer

How to create an ISA Hierarchy where only one subtype is allowed to exist in SQL?

Say for example I have a table for Person. This table looks as such: create table Person (id int not null primary key, FirstName varchar(20) not null, LastName varchar(20) not null ) The ISA Hierarchy has 2 extended types, Staff and Student,…
KrabbyPatty
  • 312
  • 1
  • 2
  • 9
1
vote
1 answer

Merge statement in SNOWFLAKE database on primary key error

I am kinda confused with the way merge statement has to be written in my scenario to load data from a table into dimension and fact tables. Below is the merge statement where I am trying to load data into my DIM product table from the JSON table but…
1
vote
0 answers

Rails - setting own primary key and deleting rails-set id causes problem

For my rails app, I used the scaffolding method to create the tables and the views and wanted to modify the employees model with setting an own primary key (with 'set_primary_key') in my model. I wanted to use the column "personel_number" as my…
Kirinriki
  • 855
  • 4
  • 12
  • 18
1
vote
2 answers

Is it insecure to reveal a row's primary key to the user?

Why do many applications replace the primary key of a database with a seemingly random alternative id when revealing the record to the user? My guess is that it prevents users from guessing other rows in the table. If so, isn't that just false sense…
1
vote
3 answers

How can i generate an entity id only if it is null with JpaRepository?

I have this class with UUID as primary key: @Entity @Table(name = "JOURNAL") @Data @Builder @NoArgsConstructor @AllArgsConstructor public class JournalEntity extends AbstractEntity { @Id @GeneratedValue(generator = "UUID") …
Francesco Rogo
  • 343
  • 3
  • 13
1
vote
2 answers

Comments table with multiple types of parents

My site (ASP.NET/C#/MS-SQL 2005) has multiple sections that allow for comments (User profiles, image pages, videos, etc). I want to store everything in a single comments table. My issue is linking the comment back to its parent. The user profiles'…
Arthur Chaparyan
  • 2,015
  • 6
  • 29
  • 35
1
vote
3 answers

Is it possible to create two tables with disjoint identifiers?

By "disjoint" I mean mutually exclusive sets of ID values. No overlap between both tables. For example, the sequence generator for the id column on both tables should work in conjunction to make sure they are always disjoint. I am not sure if this…
Vivek
  • 344
  • 1
  • 9
1
vote
1 answer

Can I avoid redundant primary key columns in a MySQL table for django (python)?

I am aware of the fact, the if I use the ORM of Django every table has to have a primary key column. Somehow if you have a many_to_many table which links to tables (let's call them authors and books) you would get something like: id author_id …
Aufwind
  • 25,310
  • 38
  • 109
  • 154
1
vote
1 answer

Do I need to create table for Hibernate TableGenerator on my own?

I am trying to figure out the different types of generating primary keys for classes that inherit from a superclass, I am using the embedded H2 database, the data is stored in a file. Everything worked well, the entities that I had previously…
vetal22331122
  • 65
  • 1
  • 1
  • 8
1
vote
1 answer

In PostgreSQL, add a column with unique values according to specific column ordering / sorting

Background I've got a PostgreSQL table, db1, that's got several columns in it but no primary key. I need to add one. Postgres is version 13-point-something, running on a local server on a 64bit Windows PC. Here's a toy version of the…
logjammin
  • 1,121
  • 6
  • 21