You are on page 1of 37

INTRODUCTION

TO SQL
TOPIC OUTLINE

▰ 7-1 Introduction to SQL


▰ 7-2 Data Definition Commands
▰ 7-2b Creating The Database
▰ 7-2d Data Types
▰ 7-2e Creating Table Structures
▰ 7-2f SQL Constraints
▰ 7-2g SQL Indexes
TOPIC OUTLINE

▰ 7-3 Data Manipulation Commands


▰ 7-3a Adding Table Rows
▰ 7-3b Saving Table Changes
▰ 7-3c Listing Table Rows
▰ 7-3d Updating Table Rows
▰ 7-3e Restoring Table Contents
▰ 7-3f Deleting Table Rows
TOPIC OUTLINE

▰ 7-3g Inserting Table Rows with a Select Subquery


1
Introduction to SQL
INTRODUCTION TO SQL

▰ Ideally, a database language allows you to


create database and table structures,
perform basic data management chores
(add, delete, and modify), and perform
complex queries designed to transform
the raw data into useful information.
CATEGORIES OF SQL FUNCTIONS

▰ Data Definition Language (DDL)


▰ Data Manipulation Language (DML)
DATA DEFINITION COMMANDS

COMMAND OR
DESCRIPTION
OPTION
CREATE TABLE Creates a new table in the user’s database schema
NOT NULL Ensures that a column will not have null values
UNIQUE Ensures that a column will not have duplicate values
PRIMARY KEY Defines a primary key for a table
FOREIGN KEY Defines a foreign key for a table
Defines a default value for a column (when no value is
DEFAULT
given)
CHECK Validates data in an attribute
DATA DEFINITION COMMANDS

COMMAND OR
DESCRIPTION
OPTION
CREATE INDEX Creates an index for a table
Creates a dynamic subset of rows and columns from one
CREATE VIEW
or more tables
Modifies a table’s definition (adds, modifies, or deletes
ALTER TABLE
attributes or constraints)
Creates a new table based on a query in the user’s
CREATE TABLE AS
database schema
DROP TABLE Permanently deletes a table (and its data)
DROP INDEX Permanently deletes an index
DROP VIEW Permanently deletes a view
DATA MANIPULATION COMMANDS

COMMAND OR
DESCRIPTION
OPTION
INSERT Inserts row(s) into a table
SELECT Selects attributes from rows in one or more tables or views
Restricts the selection of rows based on a conditional
WHERE
expression
GROUP BY Groups the selected rows based on one or more attributes
Restricts the selection of grouped rows based on a
HAVING
condition
ORDER BY Orders the selected rows based on one or more attributes
UPDATE Modifies an attribute’s values in one or more table’s rows
DELETE Deletes one or more rows from a table
DATA MANIPULATION COMMANDS

COMMAND OR
DESCRIPTION
OPTION
=, <, >, <=, >=,
Used in conditional expressions
<>, !=
AND/OR/NOT Used in conditional expressions
BETWEEN Checks whether an attribute value is within a range
IS NULL Checks whether an attribute value is null
Checks whether an attribute value matches a given
LIKE
string pattern
Checks whether an attribute value matches any value
IN
within a value list
EXISTS Checks whether a subquery returns any rows
DATA MANIPULATION COMMANDS

COMMAND OR
DESCRIPTION
OPTION
Returns the number of rows with non-null values for a
COUNT
given column
Returns the minimum attribute value found in a given
MIN
column
Returns the maximum attribute value found in a given
MAX
column
SUM Returns the sum of all values for a given column
AVG Returns the average of all values for a given column
2
DATA DEFINITION
COMMANDS
CREATING THE DATABASE

▰ XAMPP is the most popular PHP


development environment
▰ XAMPP is a completely free, easy to install
Apache distribution containing MariaDB,
PHP, and Perl. The XAMPP open source
package has been set up to be incredibly
easy to install and to use.
▰ https://www.apachefriends.org/index.html
DATA TYPES

▰ A data type defines what kind of value a


column can hold: integer data, character
data, monetary data, date and time data,
binary strings, and so on.
▰ See
https://www.w3schools.com/sql/sql_dataty
pes.asp
for the list of all data types in sql
SQL COMMANDS

SHOW DATABASES;

*To show the current database


SQL COMMANDS

CREATE DATABASE
[name_of_database];

*To create the database with the database


name
SQL COMMANDS

USE [name_of_database];

*To select the database created


SQL COMMANDS

USE [name_of_database];

*To select the database created


SQL COMMANDS

SHOW TABLES;

*to show the tables in the current database


SQL COMMANDS

CREATE TABLE table_name


(
column_name1 data_type(size)
constraint_name,
column_name2 data_type(size)
constraint_name,
column_name3 data_type(size)
constraint_name,
SQL CONSTRAINTS

▰ SQL constraints are used to specify rules


for the data in a table.
▰ If there is any violation between the
constraint and the data action, the action
is aborted by the constraint.
SQL CONSTRAINTS

▰ Constraints can be specified when the


table is created (inside the CREATE TABLE
statement) or after the table is created
(inside the ALTER TABLE statement).
SQL CONSTRAINTS

▰ NOT NULL - Indicates that a column


cannot store NULL value
▰ UNIQUE - Ensures that each row for a
column must have a unique value
SQL CONSTRAINTS

▰ PRIMARY KEY - A combination of a NOT


NULL and UNIQUE. Ensures that a column
(or combination of two or more columns)
have a unique identity which helps to find
a particular record in a table more easily
and quickly
SQL CONSTRAINTS

▰ FOREIGN KEY - Ensure the referential


integrity of the data in one table to match
values in another table
▰ CHECK - Ensures that the value in a
column meets a specific condition
▰ DEFAULT - Specifies a default value for a
column
SQL COMMANDS

INSERT INTO table_name VALUES


(
‘data1’, ‘data2’,’data3’,
....
);
SQL COMMANDS

INSERT INTO table_name VALUES


(
‘data1’, ‘data2’,’data3’,
....
);
SQL COMMANDS

To display the data inserted

SELECT column_name,column_name
FROM table_name;
and
SELECT * FROM table_name;
SQL COMMANDS

To arrange the data to be displayed

SELECT *
FROM table_name
ORDER BY column_name [ASC|DESC];
SQL COMMANDS

To add a column in a table, use the following


syntax:

ALTER TABLE table_name


ADD column_name datatype;
SQL COMMANDS

To delete a column in a table, use the


following syntax (notice that some database
systems don't allow deleting a column):

ALTER TABLE table_name


DROP COLUMN column_name;
SQL COMMANDS

To change the data type of a column in a


table, use the following syntax:

ALTER TABLE table_name


MODIFY COLUMN column_name
datatype;
SQL COMMANDS

To update the data inserted in the database

UPDATE tablename
SET columnname = expression WHERE
conditionlist;
SQL COMMANDS

To delete a record

DELETE FROM tablename


[WHERE conditionlist];
SQL COMMANDS

To delete the table

DROP TABLE table_name;


SQL COMMANDS

To delete the database

DROP DATABASE database_name;

You might also like