1. What is a primary key?
Answer: Primary key is a constraint basically that can be created or specified on a field which will be help to recognize the rows uniquely. This will not allow any NULL values and DUPLICATE values.
A table must have only one primary key that includes single or multiple fields. When multiple fields are used to define a primary key, then it can be called as composite key.
2. What is a unique key?
Answer: Unique key is a constraint that can be defined on a field so that it will allow only unique values in that respective column. Unique Key is generally used to maintain the unique values throughout the column for all the records.
You can also define many unique constraints on a table, but you can have only one Primary key constraint defined per table. Unique Key will allow even Null values but only once to show its uniqueness.
A Primary key constraint will have the automatic unique constraint which is by default. Vice Versa is not true.
3. What is a foreign key?
Answer: Foreign key is a constraint which would be created in a table that has a reference with the primary key on another table. Relationship needs to be created, while creating the constraint.
4. What do you mean by joins in SQL?
Answer: Joins is a keyword that is used in the SQL query to display the data which is based on the relationship between the fields of multiple tables. Primary Keys and Foreign Keys play a vital role if you want to implement joins in a query. Also, it is not always mandatory to have similar kind of columns in multiple tables to implement the joins.
5. Name any 4 types of Joins and Explain ?
Answer: There are various types of Joins which can be used to retrieve data from multiple tables which do have relationships between them.
1. Inner Join.
Inner join will return the rows when there is at least one row that matches between the tables.
2. Right Outer Join.
Right join will return the rows which are in common between the tables and all rows of Right hand side table. So, it will give the preference to right hand side table to display all rows, even though there are no matching rows in the left hand side table.
3. Left Outer Join.
Left join will return the rows which are in common between the tables and all rows of Left hand side table. So, it will give the preference to left hand side table to display all rows, even though there are no matching rows in the Right hand side table.
4. Full Outer Join.
Full join will return the rows when there are matching rows in any one of the tables. So, it will give the preference to left hand side table and right hand side table also. So, you can see many null values in the displayed results for most of the columns that are not in common.