DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
MySQL

Primary Key, Unique Key, Key, Foreign Key

Share

A primary key is a column or multiple columns that uniquely identify each row in a table.
Basic key of primary key:
– This must contain unique values
– For multiple columns, the combination of values must be unique
– Primary key is not null
– Only one primary key will be present in a table

As MySQL works faster with integer, the primary key data type shoule be INT or similar like this i.e. BIGINT, TINYINT etc. For a unique value you can use AUTO_INCREMENT in this field.

Here is an example:

CREATE TABLE tbl(
 column_id INT AUTO_INCREMENT PRIMARY KEY,
 column_name VARCHAR(100),
 column_type VARCHAR(20)
 );

For multiple column:

CREATE TABLE tbl(
 column_id INT NOT NULL,
 column_uid NOT NULL,
 column_name VARCHAR(100),
 column_type VARCHAR(20),
 PRIMARY KEY(column_id,column_uid)
 );

A UNIQUE key creates a constraint for a column whose value must be unique. UNIQUE key allows NULL values.
For example, in a user registration table userName and userEmail must be unique.

KEY is alias for INDEX.

Foreign key is a field which matches the other field of another table.
Suppose on user table there is a useID. Also there is an another table called userOrder where each user’s order is inserted. So in this table there is also a column user_id which store the value of userID.

So the user_id field in the userOrder table relates to the userID primary key field in user table.

Namaste UI (Author)

Namaste UI collaborates closely with clients to develop tailored guest posting strategies that align with their unique goals and target audiences. Their commitment to delivering high-quality, niche-specific content ensures that each guest post not only meets but exceeds the expectations of both clients and the hosting platforms. Connect with us on social media for the latest updates on guest posting trends, outreach strategies, and digital marketing tips. For any types of guest posting services, contact us on info[at]namasteui.com.

Recent Posts

How Did The Restaurant Industry change In Post Pandemic Era?

The coronavirus outbreak has drastically changed the way we live our lives. Yes, that's absolutely…

1 week ago

A Guide To CPQ and How It Fuels Marketing and Sales

Sales and marketing teams help attract, convert, and retain customers to ensure an organization’s long-term…

2 weeks ago

Top Questions Asked About Invoice Financing Applications

Are you an owner of a small business who’s trying to come up with ways…

3 weeks ago

Complete Medical Guide to Blood Sugar Testing and Glucose Control

Introduction When patients bring me their lab reports, the confusion is almost always the same.…

4 weeks ago

6 Tips to Strategically Remodel Your Home

Are you excited about remodeling your house after a long time? Perhaps if you're planning…

4 weeks ago

Driving Finding Balance on and off the Mat: How Calm Awareness Supports Everyday

The practice of yoga teaches us to be present, patient and mindful of our decisions.…

4 weeks ago