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

UUID with additional string as prefix in primary key Spring Boot

I had an idea to implement UUID as my primary key in the SQL database with spring boot technology. Currently, I have a dilemma on how to implement a custom fixed string with UUID as my primary key. Here is an example: This is a classic…
1
vote
1 answer

Entity Framwork Update many to many

I am new to ef core. I am trying to implement many to many . Here is my DBContext public class MaxCiSDbContext : DbContext { public DbSet Jobs { get; set; } public DbSet Staffs { get; set; } public…
Ehsan Aliverdi
  • 143
  • 1
  • 1
  • 6
1
vote
4 answers

two tables with the same sequence

Is possible to have two tables with the same incrementing sequence? I was trying to do a tree with ID, NAME, ParentID and i have to join two tables. If i have different id the tree scheme of ID - ParentId will not work. Table A …
macwadu
  • 907
  • 4
  • 24
  • 44
1
vote
1 answer

How can I change this SQL table to include null values? (Crystal Reports / SQL)

I need to get aggregate data from multiple tables without excluding null values. When I pull the tables into crystal reports, it eliminates the results that have null values. Example: Table A contains a unique MemberID (which I need to group by) and…
1
vote
1 answer

Django Admin actions don't work after redefining primary key of the model

I am using standard models.Model in Django. I changed my primary key to _id (mongo). After that, actions in Django Admin don't work anymore. For example, when I use the default Django action – Delete selected, no object is found through the query.…
1
vote
1 answer

Are statistics computed for indexes associated to primary keys in Oracle 9?

I'm beginning to work with Oracle and I've learn that in Oracle 9 statistics aren't collected for an index unless you use COMPUTE STATISTICS, but you can't use that option when defining a primary key, so I would want to know if primary keys can use…
Alberto Martinez
  • 2,620
  • 4
  • 25
  • 28
1
vote
1 answer

Adding Primary and Foreign keys to SQL Table on Databricks

I'm new to Databricks and when I want to alter my SQL tables by adding primary or FK, I get an absurd error that can't seem to debug it. %sql ALTER TABLE samples_abp215dsdp ADD CONSTRAINT PRIMARY (SampleID); here is the error message: Error in SQL…
Pishi
  • 15
  • 1
  • 4
1
vote
1 answer

Django - Adding a primary key to an existing model with data

This is the existing model in a djnago app: class Task_Master_Data(models.Model): project_id = models.ForeignKey(Project_Master_Data, on_delete=models.CASCADE) task_created_at = models.DateTimeField(auto_now_add=True) task_updated_at =…
Nitesh Halai
  • 917
  • 8
  • 20
1
vote
1 answer

Relative Primary Key in Flask_SQLAlchemy

Context/Minimal Example: I'm relatively new to Database design and trying to design a small app in Flask/Flask_SQLAlchemy that tracks inventory. I have a User table: class Users(db.Model): user_id = db.Column(db.Integer, primary_key=True) …
1
vote
2 answers

FHIR works on AWS server not allowing to keep customized id as primary key

We are working for FHIR(Fast Healthcare Interoperability Resources). We have followed “FHIR works on AWS” and deployed the CloudFormation template given by AWS in our AWS environment.Following is the template that we have…
1
vote
1 answer

Update on Duplicate key with two columns to check mysql

I am trying to get my head around the 'On Duplicate Key' mysql statement. I have the following table: id (primary key autoincr) / server id (INT) / member id (INT UNIQUE KEY) / basket (VARCHAR) / shop (VARCHAR UNIQUE KEY) In this table each member…
John Kane
  • 41
  • 1
  • 6
1
vote
1 answer

Type of primary key index and what happens on modifying them in MySQL

The highest rated answer here mentions how in MySQL records are stored in the order of primary index. Does that mean the primary index created is a sparse index? And if so, what happens if one modifies the primary key, by changing the column on…
Rinkesh P
  • 588
  • 4
  • 13
1
vote
2 answers

Does a surrogate (INT) key almost always yield better performance than an unique natural (VARCHAR) key (in MySQL)?

I am struggling to understand what datatype to use for an MySQL database table. Let's say we book publishing company and we need to create a database of all our books and authors in MySQL database. We have around 500000 books. A book has an unique…
corgrath
  • 11,673
  • 15
  • 68
  • 99
1
vote
0 answers

Is normalisation done on the primary key or on the candidate keys?

Im not sure about the correct definitions for normalisations, as primary key and candidate key are used interchangeably. I have come across these 2 definitions when trying to find a definition for 2NF and 3NF. Definitions 1: 2NF: "And every…
1
vote
6 answers

How to get a Primary ID before or at the same time it gets created?

What I am doing: (wp_posts has ID as primary key and it auto-increase) mysql_query("INSERT INTO wp_posts (post_content, post_title) VALUES ('$addEP', '$title')"); $rs = mysql_query("SELECT ID FROM wp_posts WHERE post_title='$title'"); $add_row =…
Muazam
  • 379
  • 1
  • 6
  • 15