Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between key and constraint in sql?

Tags:

sql

I have referred many documents but couldn't find a supporting/compromising answer for this. Can anyone briefly describe the difference between key and constraints?

like image 961
Lakshmi Avatar asked Oct 17 '22 16:10

Lakshmi


1 Answers

A key is a single or combination of multiple fields in a table. Its is used to fetch or retrieve records/data-rows from data table according to the condition/requirement. Keys are also used to create relationship among different database tables or views.

SQL constraints are used to specify rules for the data in a table. If there is any violation between the constraint and the data action, the action is aborted. Constraints can be specified when the table is created (inside the CREATE TABLE statement) or after the table is created (inside the ALTER TABLE statement).

you can check more documentation in the links below

Keys

Constraint

like image 197
KillerPollo Avatar answered Oct 21 '22 05:10

KillerPollo