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

UUID and auto increment INT as primay key for diffrent tables in one db

I have a database with about 100 tables, I want to add REST API requests connected with some tables. I don't want to expose id from the database directly to the request so I want to change PK (primary key) from auto-increment int to UUID for…
David
  • 67
  • 9
1
vote
2 answers

how to make an aotuGenerated field in a mutableList of parcelabel object in a feild of entity class Room kotlin

I have an entity class (PipeLine) that has a Mutable list of a parcelabel class (DamagePoint ) the main class PipeLine has a field val id:Int=0, primary key set to autoGerat=true it's working fine the subclass DamagePoint also has a primary key val…
1
vote
0 answers

Inserting with pyodbc in SQL Server table that has two columns as primary key

I am using pyodbc to insert a custom pricelist in SQL Server. My other queries work on the server, but I have difficulty with the following. When I use SQL Server Management Studio the values are nicely inserted: INSERT INTO…
gygoole
  • 21
  • 3
1
vote
2 answers

SQL Trigger Wont Generate Primary Key

I am trying to create a table that logs all inserts in the author table. Here is the author table, and the Audit_log Table: CREATE TABLE Author(AuthorID INT PRIMARY KEY NOT NULL, last_name CHAR(20), first_name…
Yeefer
  • 11
  • 2
1
vote
2 answers

if I have a complex primary key, is it possible to create foreign key only for one column of it?

I have this: ID1 INTEGER PRI ID2 INTEGER PRI NAME VARCHAR now I need to: ID1_REF INTEGER REFERENCE TO ID1 DESCRIPTION VARCHAR so, one column refers to one column of primary key, not to 2 column. Is it even possible in relational databases?
user893856
  • 1,039
  • 3
  • 15
  • 21
1
vote
3 answers

How to find out what table's primary keys are using a select query

Is it possible to run SELECT PIRMARY_KEY FROM SomeTable, where PRIMARY_KEY is a keyword that will automatically translate to SomeTable's primary key columns I am using Oracle database
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
1
vote
1 answer

Cannot create primary key using already created index

I have a table ideas with columns idea_id, element_id and element_value. Initially, I had created a composite primary key(ideas_pkey) using all three columns but I started facing size limit issues with the index associated with the primary key as…
Aakash Verma
  • 3,705
  • 5
  • 29
  • 66
1
vote
1 answer

How to query for many composite primary keys in mysql?

Let's say I have a table with a column A, column B, and column C that make up a composite primary key. Now let's say I have like 10,000 values for those primary keys. ex. (1, 2, 3), (4, 5, 6), (7, 8, 9) ... etc for (A, B, C) I want each row…
hicscimi
  • 21
  • 1
1
vote
1 answer

Kafka/KsqlDb : Why is PRIMARY KEY appending chars?

I intend to create a TABLE called WEB_TICKETS where the PRIMARY KEY is equal to the key->ID value. For some reason, when I run the CREATE TABLE instruction the PRIMARY KEY value is appended with the chars 'JO' - why is this happening? KsqlDb…
malexanders
  • 3,203
  • 5
  • 26
  • 46
1
vote
0 answers

Android Room Java - Right way to insert data when the rowId / Primary Key of the new row is required

In my app (MVVM architecture) I am inserting a new row to a table. In the next step I want to use the newly generated Primary Key as identifier for a notification. My initial approach (based on my limited experience) was to insert the row…
Zudy
  • 49
  • 7
1
vote
1 answer

ID got null in json (id=null) RESTapi Python flask sqlite. What to do if id is equal to null

Using flask framework I created table in SQLite with 3 rows (id, author, title). While i run program id is equal to null (id=null) in JSON format. Please help out to this. I created 2 files in python app.py to run my code in flask framework and…
Tejas Mankar
  • 108
  • 1
  • 7
1
vote
1 answer

SQLDataAdapter filling datatable with primary key produces error and exits sub

Ok so this is going to take some explaining. The process I am trying to do is grab data from a table function in SQL and then fill a dataset with the returned values. I then have to run this query twice more to query an alternative number table.…
Malcolm Baker
  • 58
  • 1
  • 6
1
vote
1 answer

Extension of an ID based off the ID of another table

I have two tables. Order and Pallet. Each order can have several pallets. I am trying to figure out if I can make the PalletID an extension of the OrderID. For example there is an OrderID of 8000. When a pallet is added to the order the palletID…
1
vote
0 answers

2 id columns in postgres

I am doing a personal project and am using Postgres as the database service. I am accessing that database through google colab and after uploading the data to the database via PGAdmin, I notice my table has 2 ID columns, even though I dropped the id…
1
vote
0 answers

How to generate custom primary key with timestamp in mariadb

In MariDB I want to create a table for invoice with an auto generated id that consist of 'YYYY' concatenated with 'select count('id') from where company_id = 'id'. The id is of type varchar. Normally I use a simple id like id integer…
AndaluZ
  • 1,410
  • 3
  • 15
  • 33