0% found this document useful (0 votes)
2 views

SQL

The document outlines the four types of database languages: DDL, DML, DCL, and TCL, explaining their functions in managing database structure, data manipulation, access control, and transaction management. It also provides an overview of SQL, its history, capabilities, and the process of creating and managing databases and tables using SQL commands. Additionally, it discusses SQL constraints, including NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY, as well as the concept of auto-increment fields in MySQL.

Uploaded by

Johndy Hobayan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL

The document outlines the four types of database languages: DDL, DML, DCL, and TCL, explaining their functions in managing database structure, data manipulation, access control, and transaction management. It also provides an overview of SQL, its history, capabilities, and the process of creating and managing databases and tables using SQL commands. Additionally, it discusses SQL constraints, including NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY, as well as the concept of auto-increment fields in MySQL.

Uploaded by

Johndy Hobayan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

Database

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.

CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo Ana Trujillo Avda. de la México D.F. 05021 Mexico


Emparedados y Constitución 2222
helados

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

5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden


SQL Platform
MySQL is an open-source relational database management system
(RDBMS).
XAMPP
• XAMPP is a free and open-source cross-platform web server solution
stack package developed by Apache Friends. The name "XAMPP" stands
for:

• X: Stands for "cross-platform".


• A: Apache HTTP Server, the web server software.
• M: MariaDB (formerly MySQL), a relational database management
system.
• P: PHP, a server-side scripting language.
• P: Perl, a programming language often used for web development.
Creating Database using
cmd and xampp
download xampp on their official website:
https://www.apachefriends.org/download.html
after installing, Start your XAMPP control panel and
ensure that the Apache and MySQL services are
running. If not, click on the "Start" button next to each
service.
use the command line interface (CLI) to create a
database in MySQL using XAMPP, you can follow these
steps:
1. Open Command Prompt:
• Press Win + R to open the "Run" dialog.
• Type cmd and press Enter to open Command Prompt.

2. Navigate to MySQL Bin Directory:


• By default, MySQL binaries are located in the bin directory of your XAMPP
installation. Navigate to this directory using the cd command. For example:

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;

Example: CREATE DATABASE sample;


Syntax for creating a new Database

Database created
The DROP DATABASE statement is used to drop an existing
SQL database.

Syntax:

DROP DATABASE databasename;

Example: DROP DATABASE sample;

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;

Here, database_name is the name of the database you want to use.


For example, if you have a database named ESSUC, you could use the following statement to use
it:
USE mydb;

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:

• SELECT column1, column2, ... FROM table_name WHERE condition;

Syntax:

SELECT column1, column2, ... FROM table_name WHERE condition;


The UPDATE statement is used to
modify the existing records in a table.
Syntax:
• UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

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;

• ALTER TABLE table_name • ALTER TABLE Persons


DROP COLUMN column_nam DROP COLUMN Email;
e;
NEW COLUMN ADDED
NEW COLUMN ADDED
NEW COLUMN ADDED
The UPDATE statement is used in MySQL to modify
existing data in a table. The basic syntax is as
follows:
• UPDATE table_name SET column1 = value1, column2
= value2, ... WHERE some_column = some_value;
COLUMN ‘Email’ HAS
BEEN UPDATED 
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 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.

The following constraints are commonly used in SQL:


•NOT NULL - Ensures that a column cannot have a NULL value
•UNIQUE - Ensures that all values in a column are different
•PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a
table
•FOREIGN KEY - Prevents actions that would destroy links between tables
•CHECK - Ensures that the values in a column satisfies a specific condition
•DEFAULT - Sets a default value for a column if no value is specified
•CREATE INDEX - Used to create and retrieve data from the database very quickly
What is a NULL Value?
• A field with a NULL value is a field with no value.
• If a field in a table is optional, it is possible to insert a
new record or update a record without adding a value to
this field. Then, the field will be saved with a NULL
value.

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:

• CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
To name a UNIQUE constraint, and to define a UNIQUE constraint on
multiple columns, use the following SQL syntax:

• CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT UC_Person UNIQUE (ID,LastName)
);
SQL UNIQUE Constraint on ALTER
TABLE
To create a UNIQUE constraint on the "ID" column when the table is already created,
use the following SQL:

ALTER TABLE Persons


ADD UNIQUE (ID);
SQL PRIMARY KEY on CREATE TABLE
• CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint
on multiple columns, use the following SQL syntax:

• CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (ID,LastNa
me)
);
SQL FOREIGN KEY Constraint

• 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:

PersonID LastName FirstName Age


• Persons Table
1 Hansen Ola 30
2 Svendson Tove 23
3 Pettersen Kari 20

OrderID OrderNumber PersonID


• Orders
1 77895 3
Table 2 44678 3
3 22456 2
4 24562 1

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.

• A PRIMARY KEY constraint automatically has a UNIQUE constraint.


However, you can have many UNIQUE constraints per table, but only
one PRIMARY KEY constraint per table.
• CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID)
);
AUTO INCREMENT Field
• Auto-increment allows a unique number to be
generated automatically when a new record is inserted
into a table.
• Often this is the primary key field that we would like to
be created automatically every time a new record is
inserted.
Syntax for MySQL
The following SQL statement defines the "Personid" column to be
an auto-increment primary key field in the "Persons" table:

• CREATE TABLE Employee (


Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
);

MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature.


By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record.
To let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:
SQL DATA TYPES
• An SQL developer must decide what type of data that will be
stored inside each column when creating a table. The data type is
a guideline for SQL to understand what type of data is expected
inside of each column, and it also identifies how SQL will interact
with the stored data.
• In MySQL there are three main data types: string, numeric, and
date and time.

• Note: Data types might have different names in different database.


And even if the name is the same, the size and other details may
be different! Always check the documentation!

You might also like