0% found this document useful (0 votes)
14 views5 pages

CS Academic Notes Class 12

Uploaded by

sohamsurana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

CS Academic Notes Class 12

Uploaded by

sohamsurana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to database concepts and its need:

A collection of data, commonly called a database, contains information about a particular


enterprise. It maintains any information that may be necessary to the decision-making process
involved in the management of that organization

Relational Data Model (Class 12 CBSE)


Here's a breakdown of key concepts in the relational data model:
1. Relation (Table):
•A structured collection of data points (tuples) organized in rows and columns.
•Each row represents a record, and each column represents an attribute.
2. Attribute (Column):
•A specific characteristic of an entity (represented by a row) in a relation.
•Examples: Name, Age, City (in a Student relation).
3. Tuple (Row):
•A single record in a relation, containing values for each attribute.
•Represents a specific instance of the entity.
4. Domain:
•The set of valid values an attribute can hold.
•Example: Domain of Age might be positive integers.
5. Degree:
•The number of attributes in a relation.
•Example: A Student relation with Name, Age, City has a degree of 3.
6. Cardinality:
•The number of tuples in a relation (total number of records).
7. Keys:
•A set of attributes that uniquely identifies a tuple (record) within a relation.
•Candidate Key: Any set of attributes that can uniquely identify tuples.
•A relation can have multiple candidate keys.
•Example: In Student relation, {Name, Age} or {Student ID} could be candidate keys.
•Primary Key: Chosen from the candidate keys as the main identifier for the relation.
•There can only be one primary key per relation.
•Usually chosen to be the most efficient and stable identifier.
•Alternate Key: Any other candidate key that is not chosen as the primary key.
•Useful for creating additional indexes for faster retrieval.
•Foreign Key: An attribute (or set of attributes) in one relation that references the primary key
of another relation.
•Creates a link between related tables.
•Ensures data integrity by referencing existing data.
Structured Query Language (SQL): A standardized language for interacting with relational
databases. Used to create, manipulate, and retrieve data.
1.Data Definition Language (DDL): A subset of SQL used to define the structure of a
database. Deals with creating, modifying, and deleting database objects like tables and views.
2.Data Manipulation Language (DML): A subset of SQL used to manage data within a
database. Allows for inserting new records, updating existing data, deleting records, and
retrieving specific data using queries.

In MySQL, data types define the kind of data a column in a table can hold. Here's a quick
overview of some common types:
•char(n): Fixed-length character string. Stores a specific number of characters (n), even
padding with spaces if necessary. (e.g., char(10) for a 10-character name)
•varchar(n): Variable-length character string. Stores up to n characters, using only the space
needed for the actual data. (e.g., varchar(50) for a name, can store shorter names efficiently)
•int: Integer data type. Stores whole numbers, typically within a range (e.g., -2147483648 to
2147483647 for INT).
•float: Stores approximate numeric values with decimals. Less precise than decimal data types
but uses less storage space.
•date: Stores date values in YYYY-MM-DD format.

MySQL Database Management (CBSE Class 12)


Creating & Using Databases:
•CREATE DATABASE db_name: Creates a new database named "db_name".
•USE db_name: Selects the specified database for future operations.
•SHOW DATABASES: Displays a list of existing databases.
•DROP DATABASE db_name: Deletes the database "db_name" (谨慎使用 - Use with
caution!).
Tables & Structure:
•SHOW TABLES: Lists all tables within the current database.
•CREATE TABLE table_name (column1 data_type [, column2 data_type...]) Defines a
new table with columns and their data types.
Constraints:
•NOT NULL: Ensures a column cannot have empty values.
•UNIQUE: Prevents duplicate values within a column.
•PRIMARY KEY: Uniquely identifies each row in a table (must be NOT NULL and
UNIQUE).
Managing Tables:
•DESCRIBE table_name: Shows the structure of a table, including column names, data types,
and constraints.
•ALTER TABLE table_name: Modifies an existing table.
•ADD COLUMN new_column data_type: Adds a new column.
•DROP COLUMN column_name: Removes a column.
•ADD PRIMARY KEY (column_name): Sets a primary key.
•DROP PRIMARY KEY: Removes the primary key constraint.
•DROP TABLE table_name: Deletes the specified table

Inserting Data (INSERT):


•INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...) Adds a
new record with specified values for each column.
Updating Data (UPDATE):
•UPDATE table_name SET column1 = new_value, column2 = new_value2... WHERE
condition Modifies existing data based on a specific condition (e.g., WHERE ID = 10).
Deleting Data (DELETE):
•DELETE FROM table_name WHERE condition Removes records that meet the specified
condition (e.g., DELETE FROM Students WHERE Age < 18).

MySQL SELECT Statement (Short Version)


SELECT: Retrieves data from tables.
•SELECT * FROM table_name: Selects all columns from a table.
•SELECT column1, column2, ... FROM table_name: Selects specific columns.
Operators:
•Mathematical: +, -, *, /, % (for calculations)
•Relational: =, <>, <, >, <=, >= (for comparisons)
•Logical: AND, OR, NOT (for combining conditions)
Aliasing:
•AS alias_name: Assigns a temporary name (alias) to a column or expression for better
readability.
Filtering Data:
•WHERE condition: Filters results based on a specific condition (e.g., WHERE Age > 20).
•IN (value1, value2, ...): Checks if a column value matches any of the listed values.
•BETWEEN value1 AND value2: Filters for values within a specific range.
Ordering Results:
•ORDER BY column_name ASC/DESC: Sorts results by a column in ascending (ASC) or
descending (DESC) order.
NULL Values:
•NULL: Represents missing or unknown data.
•IS NULL: Checks if a column value is NULL.
•IS NOT NULL: Checks if a column value is NOT NULL.
LIKE Operator:
•LIKE 'pattern%': Performs pattern matching using wildcards (% for any characters, - for a
single character).

1. COUNT(*)
•Counts the number of rows in a table or group.
•Includes rows with NULL values.
2. SUM(column_name)
•Calculates the sum of all values in a numeric column.
•Ignores NULL values.
3. AVG(column_name)
•Calculates the average value of a numeric column.
•Ignores NULL values.
4. MAX(column_name)
•Returns the maximum value in a column.
•Considers all data types (numeric and string).
5. MIN(column_name)
•Returns the minimum value in a column.
•Considers all data types (numeric and string).

GROUP BY:
•Groups data rows based on shared values in one or more columns.
•Useful for summarizing data by categories.

HAVING Clause:
•Used with GROUP BY to filter groups based on a condition applied to the aggregate results.
•Filters happen after the grouping is complete.

1. Cartesian Product (CROSS JOIN):


•Combines all rows from one table with all rows from another table, regardless of any
relationship.
•Results in a large dataset and is rarely used intentionally.
2. Equi-Join:
•Combines rows from two tables based on an equality condition between columns in both
tables.
•Specifies the columns for comparison using the ON clause.
3. Natural Join:
•Similar to equi-join, but automatically joins tables based on columns with the same name and
data type in both tables.
•Avoids the need to explicitly specify the join condition in the ON clause.

Connecting Python & SQL:


1.Install a connector library: Specific to your database (e.g., mysql-connector-
python for MySQL).
2.Import the library: import mysql.connector
3.Establish a connection: Use connect() with database details (host, user, password).
Cursor:
•A "pointer" to navigate through data retrieved from the database.
•Created using the cursor() method of the connection object.
Performing Queries (using cursor):
1.Execute the query: Use cursor.execute(query_string) with your SQL statement.
2.(Optional) Insert data: Use cursor.execute(query_string,
data_tuple) with values for placeholders.
Displaying Data:
•fetchone(): Retrieves the first row as a tuple.
•fetchall(): Retrieves all remaining rows as a list of tupl
rowcount:
•Property of the cursor object that reflects the number of rows affected by the last executed
query.
•Useful for checking insert/update/delete operation success (number of rows modified).

You might also like