SQL
SQL
Languages
4 Types of Database Languages
1. Data Definition Language (DDL)
2. Data Manipulation Language (DML)
3. Data Control Language (DCL)
4. Transaction Control Language (TCL)
Data Definition Language (DDL)
• DDL is a type of database language used to define and manage
the structure of a database. It includes commands such as
CREATE, ALTER, and DROP that are used to create, modify,
and delete database objects such as tables, views, indexes,
and constraints.
Data Manipulation Language (DML)
• DML is a type of database language used to manipulate the
data stored in a database. It includes commands such as
SELECT, INSERT, UPDATE, and DELETE that are used to
retrieve, add, modify, and delete data from database tables.
Data Control Language (DCL)
• DCL is a type of database language used to control access to a
database. It includes commands such as GRANT and REVOKE
that are used to grant or revoke privileges to users or roles.
Transaction Control Language (TCL)
• TCL is a type of database language used to manage
transactions within a database. It includes commands such as
COMMIT, ROLLBACK, and SAVEPOINT that are used to
control the atomicity, consistency, isolation, and durability
(ACID) properties of transactions.
SQL
Structured Query Language
What is SQL?
• SQL or Structured Query Language
is a domain-specific language used
in programming and designed for
managing data held in a relational
database management system
(RDBMS). It is particularly useful in
handling structured data.
• SQL became a standard of the
American National Standards
Institute (ANSI) in 1986, and of the
International Organization for
Standardization (ISO) in 1987
HISTORY OF SQL
• Structured Query Language (SQL)
was developed in the 1970s by IBM
scientists Donald Chamberlin and
Raymond Boyce. It was originally
called SEQUEL, which was short for
"structured English query
language".
• SQL is pronounced “sequel” or
sometimes “ess-cue-ell.”
What Can SQL do?
• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and views
Database Tables - A database most often contains one or
more tables. Each table is identified by a name (e.g. "Customers"
or "Orders"). Tables contain records (rows) with data.
3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
cd C:\xampp\mysql\bin
Log in to MySQL:
• Once you're in the MySQL bin directory, you can log in to MySQL by
executing the mysql command with the appropriate options. For
XAMPP, you'll typically use the following command:
mysql -u root -p
Note: If you haven't set a password for the MySQL root user during
XAMPP installation, you can simply omit the -p option when logging in to
MySQL using the command line. Here's how to proceed:
mysql -u root
Showing Databases
• To show the list of databases in MySQL, whether you're using XAMPP
or any other MySQL setup, you can use the SHOW DATABASES;
command.
The CREATE DATABASE statement is used to create a new SQL
database.
Syntax:
CREATE DATABASE databasename;
Database created
The DROP DATABASE statement is used to drop an existing
SQL database.
Syntax:
Note: Be careful before dropping a database. Deleting a database will result in loss of
complete information stored in the database!
Syntax for dropping/deleting Database
notice that the database “sample” is now deleted from the database list
To use a database in MySQL, you can use the USE statement. The basic syntax is as follows:
USE database_name;
This statement will set the context to the ESSUC database, and any subsequent database
operations (e.g., creating tables, inserting data, etc.) will be performed on this database.
Note that you need to have the necessary privileges to use a database, and that you should make sure
that the database you want to use exists and is accessible to your MySQL account.
The CREATE TABLE statement is
used to create a new table in a
database.
Syntax: Example:
CREATE TABLE table_name ( CREATE TABLE Persons (
column1 datatype, PersonID int,
column2 datatype, LastName varchar(255),
column3 datatype, FirstName varchar(255),
.... Address varchar(255),
); City varchar(255)
);
“show tables” is use to display all the tables created in a Database
“SHOW COMLUMNS FROM (name of table)” is use to display a list
of the columns in the ‘PERSONS’ table, including information such
as the column name, data type, default value, and whether or not
the column is NULL or has a UNIQUE constraint, among others.
The DROP TABLE statement is used to drop an existing table
in a database.
Syntax : Example:
• DROP TABLE table_name; • DROP TABLE Persons;
The INSERT INTO statement is used
to insert new records in a table.
Syntax:
• INSERT INTO table_name (column1, column2, column3
, ...) VALUES (value1, value2, value3, ...);
Example:
• INSERT INTO Persons (PersonID, LastName,
FirstName, Address, City)
VALUES ('23-4567', 'Cesista', 'Avie
Abraham', 'Dolores', 'Eastern, Samar');
if you get this message, this means you successfully insterted a data in
the table
The SELECT statement in SQL is used to retrieve data from a database.
• The WHERE keyword in SQL is used to filter records based on a
specified condition. It allows you to retrieve only the rows that meet
the specified criteria. Here's the basic syntax of the SELECT statement
with the WHERE clause:
Syntax:
Example:
• UPDATE Persons
SET LastName = 'Pomarejos', FirstName= 'Aira Mae'
WHERE PersonID = 2;
The DELETE statement is used to delete existing records
in a table.
Syntax:
• DELETE FROM table_name WHERE condition;
Example:
• DELETE FROM Persons WHERE LastName='Osila';
The ALTER TABLE statement is used to add, delete, or modify columns in an existing
table.
The ALTER TABLE statement is also used to add and drop various constraints on an
existing table.
Syntax: Example:
• ALTER TABLE table_name • ALTER TABLE Persons
ADD column_name ADD Email varchar(255);
datatype;
Syntax: Example:
ALTER TABLE table_name • ALTER TABLE person
CHANGE first_name
CHANGE old_column_name
given_name VARCHAR(50)
new_column_name NOT NULL;
column_definition;
NOTICE THAT THE COLUMN ‘ADDRESS’ HAS BEEN CHANGED INTO ‘LOCATION’
SQL CONSTRAINTS
SQL Create Constraints
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures the
accuracy and reliability of the data in the table. If there is any violation between the
constraint and the data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column,
and table level constraints apply to the whole table.
Note: A NULL value is different from a zero value or a field that contains spaces. A field
with a NULL value is one that has been left blank during record creation!
SQL NOT NULL Constraint
By default, a column can hold NULL values.
The NOT NULL constraint enforces a column to NOT accept NULL values.
This enforces a field to always contain a value, which means that you cannot insert a
new record, or update a record without adding a value to this field.
SQL NOT NULL on CREATE TABLE
• The following SQL ensures that the "ID", "LastName",
and "FirstName" columns will NOT accept NULL values
when the "Persons" table is created:
• The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
• A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the
PRIMARY KEY in another table.
The table with the foreign key is called the child table, and the table with the primary
key is called the referenced or parent table.
Look at the following two tables:
Notice that the "PersonID" column in the "Orders" table points to the "PersonID" column in the "Persons" table.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key column, because it has to be one
of the values contained in the parent table.
SQL UNIQUE Constraint
• The UNIQUE constraint ensures that all values in a column are different.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.