0% found this document useful (0 votes)
249 views7 pages

Primary Key and Foreign Key

This document explains the connection between primary keys and foreign keys in SQL, highlighting their importance in relational databases for maintaining data integrity and enabling complex queries. It provides a step-by-step guide on creating foreign key constraints to establish relationships between tables, illustrated with examples of Customers and Orders tables. Additionally, it covers string functions in SQL that can be used for data manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
249 views7 pages

Primary Key and Foreign Key

This document explains the connection between primary keys and foreign keys in SQL, highlighting their importance in relational databases for maintaining data integrity and enabling complex queries. It provides a step-by-step guide on creating foreign key constraints to establish relationships between tables, illustrated with examples of Customers and Orders tables. Additionally, it covers string functions in SQL that can be used for data manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

How to connect primary key and foreign key in sql:

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.

Understanding the Role of Primary Keys

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.

Characteristics of Primary Keys

 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.

Deciphering Foreign Keys and Their Importance

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.

Benefits of Using Foreign Keys

Data Integrity: Foreign keys help maintain referential integrity by ensuring that
the relationship between tables is consistent.

Relationship Representation: They represent relationships between different


entities in a database.
Query Power: Foreign keys enable complex queries across multiple tables,
such as joins.

Establishing Relationships: Connecting Primary Keys to Foreign Keys

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.

Step-by-Step Guide to Creating a Foreign Key Constraint

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.

SQL>CREATE TABLE Customers (

CustomerID int NOT NULL,

CustomerName varchar(255) NOT NULL,

PRIMARY KEY (CustomerID)

);

Table Created

SQL>CREATE TABLE Orders (

OrderID int NOT NULL,

OrderNumber varchar(255) NOT NULL,

CustomerID int,

PRIMARY KEY (OrderID),


FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)

);

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.

Examples of other tables:

Details table:

Roll_number Name City Phone_Number Address

Scores table:

Roll Marks Semester

SQL> CREATE TABLE Details(Roll_number INT,Name


VARCHAR(60),City VARCHAR(60),Phone_Number INT,Address
VARCHAR(100),PRIMARY KEY(Roll_number));

Table created.

SQL> CREATE TABLE Scores(Roll INT,Marks INT,Semester INT,FOREIGN


KEY (Roll) REFERENCES Details (Roll_number));

Table created.

DELETE A FOREIGN KEY


The syntax for deleting the foreign key is –
ALTER TABLE table_name DROP FOREIGN KEY column_name
B) String Functions:

1. Concatenation: CONCAT is used to add two attribute values such as string.


SQL> select concat (eno, loc) from emp; CONCAT(ENO,LOC) ;

2. lpad: LPAD() function is used to padding the left side of a string with a
specific set of characters.

SQL> select lpad(ename,10,'*') from emp;

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.

SQL> select rpad(ename,10,'*') from emp;

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

SQL> select ltrim('******hi********','*') from dual;

5. rtrim: RTRIM() function is used to remove all specified characters from the
left end side of a string

SQL> select rtrim('******hi********','*') from dual;

6. lower: lower() function is used to convert the attribute value in to lower case.
SQL> select lower(ename) from emp;

7. upper: upper() function is used to convert the attribute values in to upper


case.

SQL> select upper(ename) from emp;

8. initcap: initcap() is used to convert the attribute values first character in


capital letter.

SQL> select initcap (ename) from emp;

9. length: length() function is used to calculate the length of the given attribute.

SQL> select ename,length(ename) from emp;

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.

SQL> select ename, substr(ename,4) from emp;

11. instr: instr() function return the location of starting passion of the sub string
in the existing value.

SQL> select instr('welcome to CRRCOE','to') from dual;

(or)

SQL> select instr('welcome to GVPCEW','to') from dual;


INSTR('WELCOMETOGVPCEW','TO')

-----------------------------

SQL> select instr('welcome to gvp','to') from dual;

INSTR('WELCOMETOGVP','TO')

--------------------------

You might also like