Primary Key and Foreign Key
Primary Key and Foreign Key
Relational databases are the backbone of modern data management. They allow
us to store, retrieve, and manipulate data in a structured and efficient manner. At
the heart of these databases lie two critical concepts: primary keys and foreign
keys. These keys are the building blocks of database relationships, ensuring data
integrity and enabling complex queries that can drive insights and business
decisions. In this article, we’ll dive deep into how to connect primary keys and
foreign keys in SQL, providing you with the knowledge to harness the full
potential of relational databases.
Before we can connect primary keys and foreign keys, it’s essential to
understand what they are and why they’re important. A primary key is a unique
identifier for each record in a database table. It ensures that each row can be
uniquely identified, which is crucial for maintaining data integrity and for
performing operations like updates and deletes.
Uniqueness: No two rows can have the same primary key value.
Non-nullability: A primary key cannot be NULL, as it must always have
a value to identify the record.
Consistency: Once assigned, the primary key value should not change.
A foreign key, on the other hand, is a field (or collection of fields) in one table
that uniquely identifies a row of another table. The foreign key is defined in a
second table, but it refers to the primary key or a unique key in the first table.
This creates a relationship between the two tables, allowing us to link data
across them.
Data Integrity: Foreign keys help maintain referential integrity by ensuring that
the relationship between tables is consistent.
Now that we’ve covered the basics, let’s explore how to connect primary keys
and foreign keys in SQL. The process involves creating a foreign key constraint
that references a primary key in another table. This constraint enforces the
relationship between the two tables and ensures referential integrity.
1) Identify the primary key in the parent table that will be referenced.
2) Ensure that the foreign key column(s) in the child table have the same
data type as the primary key.
3) Use the ALTER TABLE statement to add a foreign key constraint to the
child table.
Let’s consider an example where we have two tables: Customers and Orders.
The Customers table has a primary key called CustomerID, and we want to
link the Orders table to it by using a foreign key.
);
Table Created
CustomerID int,
);
Table Created
In the example above, the Orders table has a column CustomerID that
references the CustomerID in the Customers table. This establishes a link
between the two tables, where each order is associated with a customer.
Details table:
Scores table:
Table created.
Table created.
2. lpad: LPAD() function is used to padding the left side of a string with a
specific set of characters.
LPAD(ENAME,10,'*')
----------------------------------------
******KING
*****BLAKE
*****CLARK
*****JONES
*****SCOTT
******FORD
*****SMITH
*****ALLEN
******WARD
****MARTIN
****TURNER
LPAD(ENAME,10,'*')
----------------------------------------
*****ADAMS
*****JAMES
****MILLER
3. rpad: RPAD() function is used to padding the right side of a string with a
specific set of characters.
RPAD(ENAME,10,'*')
----------------------------------------
KING******
BLAKE*****
CLARK*****
JONES*****
SCOTT*****
FORD******
SMITH*****
ALLEN*****
WARD******
MARTIN****
TURNER****
RPAD(ENAME,10,'*')
----------------------------------------
ADAMS*****
JAMES*****
MILLER****
14 rows selected.
4. ltrim: LTRIM() function is used to remove all specified characters from the
left end side of a string
5. rtrim: RTRIM() function is used to remove all specified characters from the
left end side of a string
6. lower: lower() function is used to convert the attribute value in to lower case.
SQL> select lower(ename) from emp;
9. length: length() function is used to calculate the length of the given attribute.
10. substr:substr() function is used to find the substring of the given attribute
value. It retuns size-1 of the given string/ attribute as a sub string.
11. instr: instr() function return the location of starting passion of the sub string
in the existing value.
(or)
-----------------------------
INSTR('WELCOMETOGVP','TO')
--------------------------