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

Insert tuple under foreign key constraint

first question posted... The database is in italian, only thing you should be aware of is that articolo = article, dettagli = details, codice = code/id. I apologize for the inconvenience I have two tables and each one references to the other one…
1
vote
0 answers

Aurora MySQL 5.7 ignores primary key index

I have a particular query that uses IN() in the where condition which pass set of IDs. Table Name - A Primary Key - id When 2000 ids passed to the IN() - eg: select col1,col2 from A where id IN (1,2,3,4,5,*******) , query uses the primary…
senrick
  • 65
  • 5
1
vote
2 answers

SQLite: unable to use PRIMARY KEY (sqlite3.OperationalError)

I'm trying to create a PRIMARY KEY with sqlite3 but I get an error. Code: import sqlite3 class DataBaseManager: def __init__(self, database): self.database = database self.tablevideos = "videos" …
1
vote
2 answers

MariaDB table with UUID primary key as BINARY(16) NOT NULL

I want to use BINARY UUIDs as my primary key in my tables, but using my own custom functions that generates optimised UUIDs loosely based on this article: https://mariadb.com/kb/en/guiduuid-performance/ The table structure and two main functions of…
1
vote
1 answer

Dropping a primary key in mysql

Sounds simple but I seem to have a tough time in dropping a primary key on a table in mysql - Have a table CompanyList with CompID as the primary key. It is being used as a foreign key in a bunch couple of other tables in the database. I want to…
user275157
  • 1,332
  • 4
  • 23
  • 45
1
vote
6 answers

Database Primary Key's -> An identity field AND a name field?

all my tables have an Id field of some type (UserId, PostId, FooId, etc). I usually make this a Primary Key. A table I have is called Countries. It has CountryId SMALLINT Name VARCHAR(100) -- Yes, english country names only, in this…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
1
vote
1 answer

Loading collection in base class from joined-subclass with NHibernate

I have a class with some properties and a collection of names like so: public class A : BaseObject { private Int32 zindex; private Int32 atNmNr; private IList names = new List(); } Then I have a class B that…
1
vote
3 answers

General SQL question about Primary Keys

I know this is pretty elementary but here it goes. I would like to know how you know what columns are a primary key in a table that does not have a primary key? Is there a technique or something that I should read? Thank you in advance
Maximus
  • 15
  • 2
1
vote
1 answer

Can the number of clustering columns in the primary key affect the Compression Ratio?

I have a table t1 which gives me a Compression Ratio of 0.25: CREATE TABLE t1( id varchar, c2 text, c3 float, c4 float, c5 float, c6 text, c7 text, c8 text, PRIMARY KEY ((id),c2, c3, c4, c5, c6, c7, c8) ); and an almost identical table t2 which…
Des0lat0r
  • 482
  • 3
  • 18
1
vote
1 answer

Optimizing Cloud Spanner for Read Latency

I designed a table schema for my Spanner database that takes advantage of interleaved tables to store associated records. I did this to optimize read times. My schema looks something like: CREATE TABLE all_ids ( userid INT64 NOT NULL, ) PRIMARY…
1
vote
1 answer

How to retrieve all columns buts the primary key and|or foreign key in SQLalchemy

Is it possible to ask query object to retrieve all columns but the Foreign and Primary key? session_object.query(Table_Class).all(exclude={pk:True,fk=True}) Currently I am manually removing them after I get them back.
Max
  • 4,152
  • 4
  • 36
  • 52
1
vote
3 answers

Can declare all primary keys bigint make much difference in MySQL?

I have some small tables that don't need the bigint primary key, they won't get that big, but, all tables have bigint primary key as standard. Can this affect my performance or mysql is smart on that? I wouldn't like to change the PKs to int on…
Jonathan
  • 4,724
  • 7
  • 45
  • 65
1
vote
0 answers

EFCore.BulkExtensions failed on the duplicate key value

I'm using EFCore.BulkExtensions for importing some data from Elasticsearch to MsSql. Importing table contains following fields. TABLE [dbo].[oem_catalog]( [Event] [varchar](100) NULL, [IsInternalUser] [bit] NULL, [IsMobile] [bit] NULL, …
user6408649
  • 1,227
  • 3
  • 16
  • 40
1
vote
0 answers

How to make a deterministic java function/class to create primary keys for a table based on upper and lower range?

I want to make a utility function or class which provides incremeneted primary key integers provided an upper and lower range I came up with this:- public class PrimaryGen { private static int upper_range; private static int lower_range; …
1
vote
2 answers

When to use GUID in SQL Server

Possible Duplicate: GUID vs INT IDENTITY Every now and then I see a system using GUIDs and I ask myself how much this will impact the performance. I once worked on a system that used GUIDs everywhere, it was written by a 'senior developer' and he…
Rob
  • 2,466
  • 3
  • 22
  • 40