DATABASE CONCEPT and SQL For NCERT CS
DATABASE CONCEPT and SQL For NCERT CS
Information is processed, organized, or structured data that is meaningful and useful for
decision-making or understanding. In other words, information is derived from data by
interpreting it in a context that makes it valuable for specific purposes.
In the context of a School Database, data is stored in the form of tables (relations). Each
table consists of rows (tuples) representing individual records, and columns (attributes)
representing the characteristics of those records.
Example: ‘Students’ Table
1 Angavai 15 10 [email protected]
2 Malimizhi 16 11 [email protected]
3 Kuzhali 14 9 [email protected]
• Relation (Table): A table in a relational database that stores data in rows and columns.
The table Students is a relation in the school database. It stores information about
students, such as their ID, name, age, class, and email.
Keys:
Keys are crucial for identifying unique records in a table and establishing relationships
between tables.
Example: ‘Classes’ Table
1. Candidate Key: A set of attributes that can uniquely identify a tuple in a table.
• In the Students table, we can select StudentID as the primary key because it
uniquely identifies each student. A primary key cannot be null, so each student
must have a unique StudentID.
• In the Classes table, ClassID is chosen as the primary key because it uniquely
identifies each class.
3. Alternate Key: Other candidate keys that were not selected as the primary key.
• In the Students table, both Email and the combination of Name and Email
could be alternate keys, as they can also uniquely identify a student. However,
since StudentID is the primary key, these alternate keys are not used as the
main unique identifier.
4. Foreign Key: An attribute or a set of attributes that refers to the primary key of another
table to establish a relationship.
• Suppose we have another table called Enrollments that records which students
are enrolled in which classes:
1 1 1
2 2 2
3 3 1
• Here, StudentID is a foreign key that refers to the StudentID in the Students
table, establishing a relationship between the Enrollments and Students
tables.
• Similarly, ClassID is a foreign key that refers to the ClassID in the Classes
table, linking Enrollments to Classes.
Putting It Together:
In this school database example:
• The Students table stores the student records.
• The Classes table stores the class information.
• The Enrollments table links students to classes using foreign keys (StudentID and
ClassID).
Keys such as primary keys and foreign keys help ensure the uniqueness of records and
establish relationships between tables, allowing efficient organization and retrieval of data.
• Create Table: Defines a new table with attributes and data types.
CREATE TABLE Students (ID INT PRIMARY KEY, Name VARCHAR(50), Age
INT, Email VARCHAR(100));
• Describe Table: Shows the structure of the table.
DESCRIBE Students;
• Alter Table:
o Add Attribute:
ALTER TABLE Students ADD Phone VARCHAR(15);
o Remove Attribute:
ALTER TABLE Students DROP COLUMN Phone;
• Delete Data:
DELETE FROM Students WHERE ID = 1;
• Select Data:
SELECT * FROM Students;
Operators in SQL:
• Mathematical Operators: +, -, *, /
• Relational Operators: =, !=, <, >, <=, >=
• Logical Operators: AND, OR, NOT
Clauses in SQL:
• Alias: Used to give a temporary name to a table or column.
SELECT Name AS StudentName FROM Students;
Update Command:
• Update Data:
UPDATE Students SET Email = '[email protected]' WHERE ID = 1;
Aggregate Functions:
• Max: Finds the maximum value.
SELECT MAX(Age) FROM Students;
Having Clause:
• Used to filter groups based on conditions.
SELECT Age, COUNT(*) FROM Students GROUP BY Age HAVING COUNT(*) > 1;
Joins:
• Cartesian Product: Produces all combinations of rows.
SELECT * FROM Students, Courses;
FROM Students
- Retrieves all records from Students even if there are no matching records in
Courses.
• Right Join:
SELECT Students.Name, Courses.CourseName
FROM Students
- Retrieves all records from Courses even if there are no matching records in
Students.
• Full Join:
SELECT Students.Name, Courses.CourseName
FROM Students
conn = mysql.connector.connect(
host="localhost",
user="root",
password="mypass",
database="Studentdb"
#Create a cursor
cursor = conn.cursor()
Executing Queries:
• Insert Data:
cursor.execute("INSERT INTO Students (ID, Name, Age) VALUES (%s, %s,
%s)", (1, 'John', 20))
• Update Data:
cursor.execute("UPDATE Students SET Age = 21 WHERE ID = 1")
conn.commit()
• Delete Data:
cursor.execute("DELETE FROM Students WHERE ID = 1")
conn.commit()
Fetching Data:
• Fetch One Record:
cursor.execute("SELECT * FROM Students")
row = cursor.fetchone()
print(row)
print(row)
Row Count:
cursor.execute("SELECT * FROM Students")