SQL PRIMARY KEY Constraint
The PRIMARY KEY
constraint uniquely identifies each record in a table.
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).
SQL PRIMARY KEY on CREATE TABLE
The following SQL creates a PRIMARY KEY
on the "ID" column when the "Persons" table is created:
MySQL:
CREATE TABLE Persons(
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
SQL Server / Oracle / MS Access:
CREATE TABLE Persons(
ID int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
To allow naming of a PRIMARY KEY
constraint, and for defining a PRIMARY KEY
constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
CREATE TABLE Persons(
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
);
Note: In the example above there is only ONE PRIMARY KEY
(PK_Person). However, the VALUE of the primary key is made up of TWO COLUMNS (ID + LastName).
SQL PRIMARY KEY on ALTER TABLE
To create a PRIMARY KEY
constraint on the "ID" column when the table is already created, use the following SQL:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons
ADD PRIMARY KEY (ID);
To allow naming of a PRIMARY KEY
constraint, and for defining a PRIMARY KEY
constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons
ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName);
Note: If you use ALTER TABLE
to add a primary key, the primary key column(s) musthave been declared to not contain NULL values (when the table was first created).
DROP a PRIMARY KEY Constraint
To drop a PRIMARY KEY
constraint, use the following SQL:
MySQL:
ALTER TABLE Persons
DROP PRIMARY KEY;
SQL Server / Oracle / MS Access:
ALTER TABLE Persons
DROP CONSTRAINT PK_Person;
W3schools Pathfinder
Track your progress - it's free!
FAQs
Both My Learning and W3Schools Public Profile are completely free of charge, however there are available upgrade plans that can enhance your learning experience. Where can I find My Learning? You can access your My Learning profile through the upper right corner on any W3Schools page.
Why do people avoid W3Schools? ›
There is a LOT of working constantly happening in the worlds of HTML, CSS, and JavaScript, and if a resource doesn't keep up, it is a detriment to the programming world, not a benefit. Using W3Schools to learn something is a waste of time, some topics are outdated and it lacks the rigor that you'll find on MDN.
Is W3Schools certification legit? ›
W3Schools is Trusted by Top Companies
Our certificates are recognized and valued by companies looking to employ skilled developers.
Is W3Schools a good website? ›
W3schools is a great resource for looking up how to write a specific piece of code, but it doesn't have the flow or continuity that the other courses do. Basically, as others keep mentioning, W3schools is good for syntax, but it doesn't really teach you to think like a developer.
What is W3Schools used for? ›
W3Schools is a freemium educational website for learning coding online. Initially released in 1998, it derives its name from the World Wide Web but is not affiliated with the W3 Consortium. W3Schools offers courses covering many aspects of web development. W3Schools also publishes free HTML templates.
Can I get a job with W3Schools certificate? ›
Becoming certified by W3Schools is great proof to show to employers and makes your resume stand out among other candidates.
Which is better, Codecademy or W3Schools? ›
Of the two, Codecademy has a simpler structure. You can easily find the topic you want and start learning. Its fixed pricing plans are also easier to understand. In addition, the biggest issue with W3Schools is the ads that keep popping up as you learn.
Does W3Schools cover everything? ›
W3Schools is a school for web developers, covering all the aspects of web development: HTML Tutorial. CSS Tutorial. JavaScript Tutorial.
Is W3Schools a good place to learn SQL? ›
"Perfect e-learning course for beginners. The training is very well structured together with quizes after almost every lesson. Great job!
How does w3school make money? ›
Through ads : through ads alone they will earn alot in w3schools. They would earn nearly 1–2lacks per day in ads alone. Through donation:. Many people who completed their courses in w3schools, after few years , they would donate some money for the growth of the site .
W3Schools is a tech training provider offering a 24-week, part-time, live online, Web Development Bootcamp.
How popular is W3Schools? ›
The Most Popular Browsers
W3Schools has over 60 million monthly visits.
Is W3 free? ›
W3schools is and will always be a completely free developer resource.
Is W3Schools copyright free? ›
Fair use defines our exceptions to the rights protected by copyright laws. Fair use of W3Schools includes using copyrighted material: In research. In news reporting.
Who is the CEO of W3Schools? ›
Thomas Thorsell-Arntsen is the Chief Executive Officer at W3Schools.
Is W3 free to use? ›
W3.CSS is free to use. No license is necessary.
Is W3Schools space free? ›
Build and host your website with W3Schools Spaces. Get started with your free website in a few clicks. Everything you need right in the browser. It is easy to use - try it!