0% found this document useful (0 votes)
94 views37 pages

6infoman Lab 1

The document provides an overview of SQL commands for defining and manipulating data in a database. It discusses data definition language commands for creating databases, tables, and defining data types and constraints. It also covers data manipulation commands for inserting, selecting, updating, and deleting data from tables. The document provides syntax examples for key SQL commands.

Uploaded by

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

6infoman Lab 1

The document provides an overview of SQL commands for defining and manipulating data in a database. It discusses data definition language commands for creating databases, tables, and defining data types and constraints. It also covers data manipulation commands for inserting, selecting, updating, and deleting data from tables. The document provides syntax examples for key SQL commands.

Uploaded by

Rhon Antonio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

INTRODUCTI

ON 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
DEFAULT Defines a default value for a column (when no value is 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 or more
CREATE VIEW
tables
Modifies a table’s definition (adds, modifies, or deletes attributes or
ALTER TABLE
constraints)
Creates a new table based on a query in the user’s database
CREATE TABLE AS
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
WHERE Restricts the selection of rows based on a conditional 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 string
LIKE
pattern
Checks whether an attribute value matches any value within a
IN
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 given
COUNT
column
MIN Returns the minimum attribute value found in a given column
MAX Returns the maximum attribute value found in a given 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_datatypes.a
sp 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