Method : create table student(studid int primary key auto_increment,studname varchar(50),mobileno bigint);
Now to apply the UNIQUE key on the column
alter table student add UNIQUE(mobileno);
To remove UNIQUE key use this command first run in mysql and see the exact index name of columm
SHOW CREATE TABLE stud;
Or
SHOW INDEX FROM stud;
after executing this command we get Key_name column name and mysql will tell u the exact indexname for that column.
Query : ALTER TABLE stud DROP INDEX mobileno;
Create table Student(studid int primary key,studentname varchar(50));
Create table UT1(ut1id int, fk_studid int, Maths int);
alter table ut1 add foreign key(fk_studid) references Student(studid);
# Removing Primary Key and Foreign Key – Step by Step
Suppose we have two tables:
### Step 1: Create the Student table
CREATE TABLE Student (
studid INT PRIMARY KEY,
studentname VARCHAR(50)
);
Here, `studid` is the **Primary Key** of the `Student` table.
---
### Step 2: Create the UT1 table
CREATE TABLE UT1 (
ut1id INT,
fk_studid INT,
Maths INT
);
Here, `fk_studid` is initially a normal column.
---
### Step 3: Add a Foreign Key to the UT1 table
ALTER TABLE UT1
ADD FOREIGN KEY (fk_studid)
REFERENCES Student(studid);
Now `fk_studid` in the `UT1` table becomes a **Foreign Key** that references `studid` in the `Student` table.
The relationship is:
Student
studid (Primary Key)
↑
│
│ Foreign Key
│
UT1
fk_studid
---
# Removing Both Keys
If we want to remove **both the Foreign Key and the Primary Key**, we should remove them in the following order:
### Step 4: Find the Foreign Key name
Run:
SHOW CREATE TABLE UT1;
MySQL may show a Foreign Key name such as:
ut1_ibfk_1
The exact name may be different in your database.
---
### Step 5: Remove the Foreign Key from UT1
Use the Foreign Key name obtained in Step 4:
ALTER TABLE UT1
DROP FOREIGN KEY ut1_ibfk_1;
Now the Foreign Key relationship between `UT1` and `Student` has been removed.
---
### Step 6: Remove the Primary Key from Student
Now remove the Primary Key:
ALTER TABLE Student
DROP PRIMARY KEY;
The Primary Key on `studid` is now removed.
---
# Important Rule
When removing both keys, follow this order:
First → Remove Foreign Key from UT1
↓
Second → Remove Primary Key from Student
**Why?**
Because the Foreign Key in `UT1` depends on the Primary Key in `Student`.
Therefore, the **dependent key (Foreign Key) should be removed first**, followed by the **referenced key (Primary Key)**.
Apply Primary key in table
Method 1: create table student(studid int primary key auto_increment,studname varchar(50));
Method 2: create table student(studid int,studname varchar(50));
alter table student add primary key(studid);
Method 3 : To remove Primary key constraint from table
alter table student drop primary key;
Note : If auto_increment is enabled on the column than , drop command on primary key is not working, so try to remove primary key constraint, do not enable auto_increment option.