SQL
MYSQL - ALTER, DROP, RENAME, MODIFY
WHAT IS THE ALTER COMMAND? As the saying goes Change is the only constant With time business...
Primary Key is a field or a combination of fields that identify a record uniquely. The Primary key is a column or set of columns that are unique. In other words, every value is unique for Primary Key.
Below diagram summarizes all the above point for the SQL Primary Key.
In this tutorial, you will learn
We can Create a Primary Key in 2 ways:
1. SQL Server Management Studio
2. T-SQL: Create Primary while creating a New Table
Step 1) Right Click on the Table name. Click on Design.
Step 2) Right-click on the Column name. Click on 'Set Primary Key'
Result: Course_Id is now a Primary Key.
Below is the syntax to create table with Primary Key from T-SQL
Syntax:
CREATE TABLE <Table_Name> ( Column1 datatype, Column2 datatype,CONSTRAINT <Name> PRIMARY KEY (Column name) . );
Let's create a Table with One Column as a SQL Primary Key.
Query:
CREATE TABLE COURSE_TSQL_PK (Course_ID Int not Null, Course_name Varchar(20) CONSTRAINT PK PRIMARY KEY (Course_ID) )
Step 1) Run the Query by clicking on 'Execute.'
Result: Course_Id is now a Primary Key.
Example: Let's see if it allows entering Multiple Records with Same Course ID.
Step 1) Insert 4 rows with different Course_ID
Insert into COURSE_TSQL_PK values (1,'SQL'); Insert into COURSE_TSQL_PK values (2,'Python'); Insert into COURSE_TSQL_PK values (3,'SQL'); Insert into COURSE_TSQL_PK values (4,'C');
Step 2) Verify all Data Inserted successfully by running the Select query.
Note: We can insert duplicate values in the Non-Primary key.
Step 3) Now let's try Inserting New records with an existing Course_ID which is Primary Key.
Insert into COURSE_TSQL_PK values (4,'JAVA');
Result: System does not allow inserting new value as 4 is there Course_ID column which is a Primary Key.
Now we will see how to add Primary Key to existing table in SQL:
You can use the ALTER statement to create a primary key. However, the primary key can only be created on columns that are defined as NOT NULL. You cannot create a primary key on a column that allows NULLs. If you need to do, you have to drop and recreate the table.
Here is the syntax:
ALTER TABLE tableName ADD CONSTRAINT constraintName PRIMARY KEY (column_1, column_2, ... column_n);
For example:
ALTER TABLE students ADD CONSTRAINT students_pk PRIMARY KEY (admission);
We have added a primary key constraint to an already existing table. The constraint has been added on the admission column and assigned the name students_pk.
WHAT IS THE ALTER COMMAND? As the saying goes Change is the only constant With time business...
What are functions? MySQL can do much more than just store and retrieve data . We can also perform...
What is a union? Unions combine the results from multiple SELECT queries into a consolidated...
{loadposition top-ads-automation-testing-tools} There are many SQL management tools available in...
What is While Loop? WHILE loop statement works similar to the Basic loop statement except the EXIT...
What are JOINS? Joins help retrieving data from two or more database tables. The tables are...