Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use primary keys in SQL databases

Shulou Source: shulou.com Published: 2022-05-31 21:42:30 09月27日 Update

This article will explain in detail how to use the primary key in the SQL database. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Each piece of data in the primary key (Primary Key) in the SQL database is a value in the table. In other words, it is used to confirm each row of data in a table. The primary key can be a field in the original data, or an artificial field (a field that has nothing to do with the original data). The primary key can contain one or more fields. When a primary key contains multiple fields, it is called Composite Key. The primary key can be set when creating a new form (using the CREATE TABLE statement), or by changing the existing table schema (using ALTER TABLE).

Here are a few ways to set the primary key when building a new form:

MySQL:

CREATE TABLE Customer (SID integer,Last_Name varchar (30), First_Name varchar (30), PRIMARY KEY (SID)); Oracle:CREATE TABLE Customer (SID integer PRIMARY KEY,Last_Name varchar (30), First_Name varchar (30)); SQL Server:CREATE TABLE Customer (SID integer PRIMARY KEY,Last_Name varchar (30), First_Name varchar (30))

The following is the way to set the primary key by changing the existing table schema:

MySQL:

ALTER TABLE Customer ADD PRIMARY KEY (SID)

Oracle:

ALTER TABLE Customer ADD PRIMARY KEY (SID)

SQL Server:

ALTER TABLE Customer ADD PRIMARY KEY (SID); on how to use the primary key in the SQL database to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Table data data database method content original multiple article more architecture knowledge article good one line statement quality reference help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple Docker vpn macOS