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
2 answers

Mysql Primary key Deletion and re-insertion

Are there any known ramifications to manually inserting a record with a primary key that once existed but was deleted. Said primary key existed only in one table, and is not referenced in any other tables.
John
  • 33
  • 1
  • 4
1
vote
1 answer

Hibernate find by Primary Key In List Direct to L2 Cache

I have an Entity with a simple long primary key. I do a Query like: Select from table where primary_key IN (....); Hibernate seems to want to do a query to get the Ids (that I just specified!) and then go to the L2 cache. Is there any way to skip…
user671435
  • 79
  • 1
  • 8
1
vote
4 answers

Entity Framework 4.1 - Database First approach - How to find column names for primary key fields?

I'm using EF4.1 for DAL for an application, and I use DbContext template generator with POCP entities. The model is created from the database, so all the fields / PK's / FK's / relations are already defined in database. I need to find out in code…
bzamfir
  • 4,698
  • 10
  • 54
  • 89
1
vote
1 answer

define PK before or after LOAD DATA INFILE

I neet to load >1 billion rows into empty MyISAM table. I'm sure all entries in the file are unique. Is it better to load the data into table with PK defined, or add PK later? I have got 8GB RAM and SAS disc workstation with 5GB RAM limit to store…
Leszek
  • 1,290
  • 2
  • 11
  • 21
1
vote
1 answer

Error Violation of PRIMARY KEY constraint - C#

I have a problem with inserting data into a list. The error tells me that the Editor class cannot contain 2 identical Ids. I understand the error, but I'm not touching that class, just adding an object to the Books list. Here are my relations…
1
vote
1 answer

Blocked insert even with Pk constraint with Initially Deferred

I have created a test table : TABLE1(ID -> pk, INFO -> varchar2(100)). I opened the SQL Developer and i changed Pk Deferrable State from "Not Deferrable" to "Initially Deferred". The index of this PK automatically changed from "Unique" to…
Blocked
  • 340
  • 2
  • 5
  • 20
1
vote
2 answers

How to change composite key column type from varchar to nvarchar?

I need to change all the varchar columns to the nvarchar type. I've generated a script with the help from this thread SQL Server - script to update database columns from varchar to nvarchar if not already nvarchar. However this script cannot change…
kor_
  • 1,510
  • 1
  • 16
  • 36
1
vote
2 answers

Postgres INSERT INTO autoincrement table with single column primary key

I am trying to auto increment a table with a single column which is a primary key, eg: CREATE TABLE my_table (id PRIMARY KEY NOT NULL); I would usually just not include the primary key column in the insert statment in order to autoincrement it, but…
Max888
  • 3,089
  • 24
  • 55
1
vote
2 answers

JPA: @JoinTable - Both columns are Primary Keys.. How do I stop that?

This is my annotation I use to generate my Join Table. @OneToMany(cascade = CascadeType.ALL) @JoinTable(name = "service_operations", joinColumns = { @JoinColumn(name = "serviceId") }, inverseJoinColumns = { @JoinColumn(name =…
user639826
1
vote
1 answer

How to create a Custom Unique Primary Key using prisma?

In my application i want to create a default primary key where the key must start with BL- and another rest 9 digits character must be random. Schema model myModel { // try 1 id String @id @default(dbgenerated("BL-")) // try 2 id  String @id…
1
vote
1 answer

Django declaring a read-only field on serializer

I have an endpoint that needs to expose the fields to the user, but under a different name. The user should -not- be able to set the PK (called orderId here) when creating the object, since that should be done automatically with a randomly set value…
Brian C
  • 1,333
  • 3
  • 19
  • 36
1
vote
0 answers

Databricks, How to enforce delta table column to have unique values?

I have defined a delta table in Databricks with a primary key: %sql CREATE TABLE IF NOT EXISTS test_table_pk ( table_name STRING NOT NULL, label STRING NOT NULL, table_location STRING NOT NULL, CONSTRAINT test_table_pk_col PRIMARY…
Mohammad
  • 775
  • 1
  • 14
  • 37
1
vote
0 answers

merging to find only unique values

I have a simple requirement to insert data into the mongo db only if the data is not present based on a primary key(My primary key is a combination of 3 attributes item_id, process_name and actor) using lambda script. Explanation on what I am doing…
Abcd Efgh
  • 57
  • 5
1
vote
2 answers

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PRIMERY KEY

I used the following code to create a table on MySQL: CREATE TABLE IF NOT EXISTS auth ( id INT AUTO_INCREMENT, email VARCHAR(50) UNIQUE NOT NULL PRIMERY KEY, password VARCHAR(250) NOT NULL, username VARCHAR(50) UNIQUE, admin BOOLEAN NOT…
best_of_man
  • 643
  • 2
  • 15
1
vote
0 answers

How can I make ID field in MySQL immutable

Is there are way to make ID fiels in MySql immutable? It's possible in postgreSQL using PGAdmin for example. ID field sets as primary key and auto incr but can be changed manually.
Vandal
  • 21
  • 2