Prerequisite – Primary key in MS SQL Server
SQL Server has different keys which serve a different purpose. In this article, the foreign key will be discussed in brief.
The foreign key has a similar purpose as the primary key yet, the foreign key is used for two tables. In a few cases, the foreign key is used for self-referencing a single table.
Foreign key :
A singular column or a set of columns of one table that uniquely identified by a singular column or a set of columns of another table is referred to as a foreign key.
constraint fk_constraint-name foreign key(col1, col2) references parent_table-name(col1, col2) (OR) foreign key(col1, col2) references parent_table-name(col1, col2)
A foreign key has two tables – parent table and child table. If a user wants to insert a column in a child table, the column has to be a part of the parent table otherwise, an error is displayed. In the syntax, the constraint term is not mandatory to use.
When a foreign key is mentioned in the query, the key automatically creates a referential constraint meaning a column can be inserted in the child table only if it is a part of the parent table.
Two tables named student (parent table) and marks (child table) are considered from the university database.
Table – Marks
If a user wants to insert a new column, the query is given as –
foreign key('rollno') references student('rollno') insert into marks ('name', 'rollno', 'marks') values('Naina, '111', '7.5')
An error is displayed as the roll number is already taken by a student. (Foreign key constraint). The foreign key does not allow the value to reoccur. To avoid such errors, the values must neither be repeated nor a different column must be considered.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Difference between Primary Key and Foreign Key
- Primary key in MS SQL Server
- Types of Keys in Relational Model (Candidate, Super, Primary, Alternate and Foreign)
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- Difference between Primary key and Unique key
- Difference between Super Key and Candidate Key
- Difference between Primary key and Super key
- Delete statement in MS SQL Server
- SQL Server Mathematical functions (SQRT, PI, SQUARE, ROUND, CEILING & FLOOR)
- SQL Server Identity
- SQL SERVER | Conditional Statements
- SQL Server | STUFF() Function
- SQL Server | SERVERPROPERTY()
- Mean and Mode in SQL Server
- Allow only alphabets in column in SQL Server
- SQL SERVER | IN Condition
- Difference between MySQL and MS SQL Server
- Comparisons between Oracle vs SQL Server
- Difference between MariaDB and MS SQL Server
- Introduction of MS SQL Server
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.