Method: 1. Use the "alter table table name add constraint primary key name primary key (primary key)" statement to add a named primary key; 2. Use the "alter table table name add primary key (primary key field)" statement to add No named primary key.
The operating environment of this tutorial: Windows 10 system, Oracle 11g version, Dell G3 computer.
How to add a primary key in oracle
Primary key explanation:
The unique keyword of a table, such as a student table, the student number cannot be repeated and is unique, the student number is the keyword, that is Primary key.
Difference from foreign keys:
Foreign keys are fields that are connected to other tables. Or, for example, there is a student table and a course selection table. At this time, the student number in the student table needs to be modified. The corresponding ones in the course selection table must also change, so you need to add the student number as a foreign key constraint to the course selection table, so that when you modify the student number, all foreign key associations will be changed
Primary key addition, deletion and other operations
1. Named primary key
1) Adding named primary key
①Add when creating a table Primary key (yy is the primary key name of the primary key "ID")
CREATE TABLE table_test( id INT NOT NULL, --注意:主键必须非空 name VARCHAR(20) NOT NULL, address VARCHAR(20), constraint yy PRIMARY KEY(id) );
②Add the primary key after creating the table
alter table table_test add constraint yy primary key(id);
Formula: alter table table name add constraint yy primary key (primary key 1, primary key 2) ;
2) Deletion with named primary key
ALTER TABLE table_test DROP CONSTRAINT yy;
Formula: ALTER TABLE table name DROP CONSTRAINT primary key name;
3) Modification with named primary key
Need to delete the primary key first and then add it
2. Unnamed primary key
1) Creation of unnamed primary key
①Add the primary key (primary key "ID" when creating the table The primary key name needs to be queried, there are methods below)
CREATE TABLE table_test( id INT NOT NULL, --注意:主键必须非空 name VARCHAR(20) NOT NULL, address VARCHAR(20), PRIMARY KEY(id) );
②Add the primary key after creating the table
alter table table_test add primary key (id);
Formula: alter table table name add primary key(primary key field 1, primary key field 2.. .);
2) Deletion of unnamed primary key
①First find out the primary key name (constraint_name), the user_cons_columns table will give an explanation at the end of the article
SELECT t.* from user_cons_columns t where t.table_name = 'TABLE_TEST' and t.position is not null;
Formula: SELECT t.* from user_cons_columns t where t.table_name = 'table name' and t.position is not null; --The table name must be in uppercase letters, such as: TABLE_TEST
②Execute the deleted SQL
ALTER TABLE table_test DROP CONSTRAINT SYS_C0056038;
Formula: ALTER TABLE table name DROP CONSTRAINT primary key name;
3) Modification of unnamed primary key
Need to delete the primary key first, and then add it
Recommended tutorial: " Oracle Video Tutorial》
The above is the detailed content of How to add primary key in oracle. For more information, please follow other related articles on the PHP Chinese website!

Oracle software can improve performance in a variety of ways. 1) Optimize SQL queries and reduce data transmission; 2) Appropriately manage indexes to balance query speed and maintenance costs; 3) Reasonably configure memory, optimize SGA and PGA; 4) Reduce I/O operations and use appropriate storage devices.

Oracle is so important in the enterprise software and cloud computing sectors because of its comprehensive solutions and strong technical support. 1) Oracle provides a wide range of product lines from database management to ERP, 2) its cloud computing services such as OracleCloudPlatform and Infrastructure help enterprises achieve digital transformation, 3) Oracle database stability and performance and seamless integration of cloud services improve enterprise efficiency.

MySQL and Oracle have their own advantages and disadvantages, and comprehensive considerations should be taken into account when choosing: 1. MySQL is suitable for lightweight and easy-to-use needs, suitable for web applications and small and medium-sized enterprises; 2. Oracle is suitable for powerful functions and high reliability needs, suitable for large enterprises and complex business systems.

MySQL uses GPL and commercial licenses for small and open source projects; Oracle uses commercial licenses for enterprises that require high performance. MySQL's GPL license is free, and commercial licenses require payment; Oracle license fees are calculated based on processors or users, and the cost is relatively high.

Oracle's evolution from database to cloud services demonstrates its strong technical strength and market insight. 1. Oracle originated in the 1970s and is famous for its relational database management system, and has launched innovative functions such as PL/SQL. 2. The core of Oracle database is relational model and SQL optimization, which supports multi-tenant architecture. 3. Oracle cloud services provide IaaS, PaaS and SaaS through OCI, and AutonomousDatabase performs well. 4. When using Oracle, you need to pay attention to the complex licensing model, performance optimization and data security issues in cloud migration.

Oracle is suitable for enterprise-level applications that require high performance and complex queries, and MySQL is suitable for web applications that are rapidly developed and deployed. 1. Oracle supports complex transaction processing and high availability, suitable for financial and large ERP systems. 2.MySQL emphasizes ease of use and open source support, and is widely used in small and medium-sized enterprises and Internet projects.

The differences in user experience between MySQL and Oracle are mainly reflected in: 1. MySQL is simple and easy to use, suitable for quick access and high flexibility scenarios; 2. Oracle has powerful functions, suitable for scenarios that require enterprise-level support. MySQL's open source and free features attract startups and individual developers, while Oracle's complex features and tools meet the needs of large enterprises.

The difference between MySQL and Oracle in performance and scalability is: 1. MySQL performs better on small to medium-sized data sets, suitable for fast scaling and efficient reading and writing; 2. Oracle has more advantages in handling large data sets and complex queries, suitable for high availability and complex business logic. MySQL extends through master-slave replication and sharding technologies, while Oracle achieves high availability and scalability through RAC.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
