SQL NOTES
SQL NOTES
Definitions:
DATABASE: A database is an organized collection of data, typically stored and accessed electronically. It serves
as a repository for information, allowing efficient retrieval, insertion, and manipulation of data.
DBMS: A Database Management System (DBMS) is software that facilitates the creation, maintenance, and
utilization of databases. It provides users and applications with an interface to interact with the data, ensuring
data integrity, security, and efficient management of resources.
SQL: SQL (Structured Query Language) is a specialized language used for managing and manipulating
relational databases. It allows users to perform tasks such as querying, updating, inserting, and deleting data in
a database.
MySQL: MySQL is a popular open-source relational database management system (RDBMS) that uses SQL as
its query language. It is widely used for web development, powering many dynamic websites and applications
due to its scalability, reliability, and ease of use.
RDBMS: A Relational Database Management System (RDBMS) is a software system that manages relational
databases. It organizes data into tables consisting of rows and columns, with each row representing a record
and each column representing a specific attribute or field. RDBMS enforces relationships between tables using
keys, ensuring data integrity and facilitating efficient data retrieval through structured query language (SQL).
It provides mechanisms for defining, updating, querying, and maintaining databases, offering features such as
transaction management, concurrency control, and data security. RDBMS is widely used in various
applications, including enterprise systems, web development, and data analytics, due to its flexibility and
scalability.
Characteristics of MySQL
1. It is free and Open Source software which does not require to pay for its usage.
2. It is easy to use, quick and reliable.
3. It is platform independent software which works on many operating systems like Windows, UNIX,
LINUX etc.
4. It is compatible with many programming languages including JAVA, Python, C++, PHP, PERL, etc.
5. It can handle large amount of data very efficiently and accurately.
Classification of SQL Statements
1. Data Definition Language (DDL)
2. Data Manipulation Language (DML)
3. Transaction Control Language (TCL)
1. Data Definition Language (DDL): DDL is a subset of SQL used to define and modify the structure of
database objects such as tables, indexes, and constraints. It includes commands like CREATE, ALTER, and
DROP. CREATE is used to define new database objects like tables, ALTER is used to modify the structure of
existing objects, and DROP is used to delete objects from the database. DDL commands are essential for
database administrators and developers to design and manage the schema of a database effectively. They allow
for the creation, modification, and deletion of database elements, enabling the organization and optimization of
data storage.
2. Data Manipulation Language (DML): DML is used to manipulate data stored in the database. It includes
commands like SELECT, INSERT, UPDATE, and DELETE. SELECT retrieves data from one or more tables
based on specified criteria, INSERT adds new records into a table, UPDATE modifies existing records, and
DELETE removes records from a table. DML commands are essential for performing day-to-day operations on
the data within a database. They enable users and applications to retrieve, modify, and remove data, facilitating
efficient data management and analysis.
3. Transaction Control Language (TCL): TCL consists of commands used to manage transactions within a
database. It includes commands like COMMIT, ROLLBACK, and SAVEPOINT. COMMIT is used to save the
changes made during a transaction, making them permanent in the database. ROLLBACK is used to undo the
changes made during a transaction, reverting the database to its previous state. SAVEPOINT allows for
creating a point within a transaction to which you can later roll back. TCL commands are crucial for ensuring
the integrity and consistency of data within a database. They enable users to control the outcome of
transactions, ensuring that changes are accurately reflected or reverted as needed, maintaining data reliability.
Some of the SQL Queries:-
1. SHOW DATABASE; : The "SHOW databases;" query is a command used in SQL to display a list of all
databases available on a particular database server. When executed, this query returns a result set
consisting of the names of all databases stored on the server. It provides users with a convenient way to
view the database environment and identify the databases they have access to.
2. CREATE DATABASE database_name; : The "CREATE DATABASE database_name;" query is a SQL
command used to create a new database within a database management system (DBMS). When
executed, this command creates a new, empty database with the specified name.
The syntax consists of the "CREATE DATABASE" keywords followed by the name you wish to assign
to the new database. This name must adhere to the naming rules and restrictions of the specific DBMS
being used.
3. USE database_name; : The "USE database_name;" query is a SQL command used to select a specific
database for subsequent operations within a database management system (DBMS). When executed,
this command instructs the DBMS to set the specified database as the current active database for the
current session.
4. SHOW TABLES; : The "SHOW TABLES;" query is a SQL command used to display a list of all tables
within the currently selected database in a database management system (DBMS). When executed, this
query returns a result set containing the names of all tables stored in the active database.
5. CREATE TABLE; : The "CREATE TABLE" query is a fundamental SQL command used to create a
new table within a database management system (DBMS). When executed, this command defines the
structure of a table by specifying its columns, data types, constraints, and other properties.
The syntax for the "CREATE TABLE" query typically includes the following components:
1. Table Name: The name of the table to be created.
2. Column Definitions: Each column in the table is defined with a name and a data type. Additionally,
you can specify other properties such as NULL/NOT NULL constraints, default values, and auto-
incrementing settings.
3. Constraints: Constraints define rules or conditions that data in the table must follow. Common
constraints include PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, and DEFAULT.
Example:
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100) UNIQUE,
hire_date DATE
);
This example creates a table named "employees" with columns for employee ID, first name, last name,
email, and hire date. The "id" column is defined as the primary key with auto-incrementing values,
ensuring each record has a unique identifier. The "email" column is defined as UNIQUE, ensuring that
each email address is unique within the table.
Relation: A relation is a fundamental concept that refers to a table that consists of rows and columns. It is a way
to represent data in a structured form. Relations are at the heart of the relational database model, which is one
of the most commonly used models for organizing and managing data.
Attribute: An attribute is a key concept that refers to a column in a relation (table). Attributes represent
specific properties or characteristics of the data entries in a table. Each attribute in a table has a name and a
data type, and it is associated with a set of values for each row (tuple) in the table.
Tuple: A tuple is a row in a relation (table). It represents a single record or data entry in the table, A tuple
consists of a set of values, one for each attribute (column) in the relation, Each tuple in a relation is unique if
the relation has a primary key that ensures distinct records.
Domain: A domain is the set of possible values that an attribute can take. It is defined by the attribute's data
type, For example, the domain of an integer attribute may be the range of integers from a specific minimum to
a maximum value, while the domain of a string attribute may include all possible strings up to a certain length.
Degree: The degree of a relation is the number of attributes (columns) it contains, For example, a relation with
five attributes has a degree of five.
Cardinality: The cardinality of a relation is the number of tuples (rows) it contains. For example, a relation
with 100 tuples has a cardinality of 100.
Keys: In a relational database, keys are important concepts used to uniquely identify records, establish
relationships between tables, and enforce data integrity.
1. Candidate Key: A candidate key is a set of one or more attributes that can uniquely identify a tuple (record)
in a relation (table). A relation can have multiple candidate keys, but each key must uniquely identify a record
and must not contain redundant attributes, The attributes in a candidate key must be unique and not null.
2. Primary Key: A primary key is a candidate key chosen to uniquely identify each tuple in a relation, A
relation can have only one primary key, The primary key must be unique for each record and cannot be null.
3. Alternate Key: An alternate key is a candidate key that is not chosen as the primary key. Alternate keys can
still uniquely identify records, but they serve as backup or secondary keys. They can be used to enforce data
integrity and may also be indexed for faster data access.
4. Foreign Key: A foreign key is an attribute (or set of attributes) in one relation that refers to the primary key
of another relation, Foreign keys establish relationships between tables and enforce referential integrity, For a
foreign key to be valid, the values in the foreign key attribute must either match a primary key value in the
related table or be null (if the foreign key allows null values), Foreign keys enable linking and navigating
between related tables in the database.
Constraints: Constraints are rules in a Database Management System (DBMS) that enforce data integrity and
consistency within tables. They define restrictions on the data values that can be stored in attributes (columns)
of a relation (table).
1. **NOT NULL**: The `NOT NULL` constraint ensures that an attribute (column) cannot hold null values,
this means that every tuple (row) in the relation must have a value for the attribute; the attribute cannot be left
empty, `NOT NULL` is often used when an attribute is essential for identifying or defining the record, such as
an email address or a username.
2. **UNIQUE**: The `UNIQUE` constraint ensures that all values in an attribute (or a combination of
attributes) are distinct across all tuples in the relation, No two tuples in the relation can have the same value for
the attribute, `UNIQUE` constraints are often used for attributes that should have unique values within a table,
such as email addresses or usernames.
3. **PRIMARY KEY**: A `PRIMARY KEY` constraint is a combination of the `NOT NULL` and `UNIQUE`
constraints applied to an attribute or a set of attributes, The primary key uniquely identifies each tuple in the
relation, so it must be unique across all tuples and cannot be null, Each relation can have only one primary key,
and it is used to ensure data integrity and enable efficient indexing and retrieval.