Introduction SQL
Introduction SQL
Chapter 3
Introduction to SQL
• SQL : Structural Query Language
• It is used for storing, manipulation, and retrieving data from the
database.
• SQL is an ANSI (American National Standards Institute) standard
language, but there are many different versions of the SQL language.
• SQL history
• Installation of MySQL workbench
SQL Commands
• These commands can be classified into the following groups based on
their nature:
1. DDL: Data definition language
• CREATE, ALTER, DROP
2. DML - Data Manipulation Language
• SELECT, INSERT, UPDATE, DELETE
3. DCL - Data Control Language
• GRANT, REVOKE
4. TCL-Transaction Control Language
• COMMIT, ROLLBACK, SAVEPOINT
Section 1
String
Units
Currency (Char) Title (Varchar)
(“millions,”Billion”)
Date time data types
Create Database
• It creates new database schema
• Syntax: CREATE DATABASE database_name;
• To check whether a database exists or not
• Syntax: show databases
• Drop database
• Syntax: DROP DATABASE database_name
Create table
• Syntax:
• CREATE TABLE table_name( column1 datatype, column2 datatype, column3
datatype, ..... columnN datatype,
PRIMARY KEY( one or more columns ) );
• Example: CREATE TABLE cutomer(id integer, first_name varchar(10),
last_name varchar(10), city varchar(10), country varchar(15), phone
varchar(15));
• To check schema of a table:
• DESC tablename;
Insert into
• Insert into:
• Syntax: INSERT INTO table_name( column1, column2....columnN) VALUES
( value1, value2....valueN);
• Example:INSERT INTO customer(id , first_name,
last_name ,city ,country,phone)VALUES (2, ‘Ana’, ‘Trujillo’, ‘Mexico’, ‘Mexico’,
(5) 555-4729);
• If users are adding values for all the columns of the table, you don’t
need to specify the particular column names in the SQL query.
• However, ensure the order of the values is in the same order as the
columns in the table.
Alter and update table
• Alter table:
• Syntax: ALTER TABLE table_name {ADD|DROP|MODIFY} column_name
{data_ype};
• ALTER TABLE table_name RENAME TO new_table_name;
• Update table:
• UPDATE table_name SET column1 = value1, column2 =
value2....columnN=valueN [ WHERE CONDITION ];
• Rename column and table
• ALTER TABLE members RENAME COLUMN join_date TO jd;
• Alter table customer_details rename to cust_details;
Delete, Drop, Truncate
• The DROP TABLE statement in SQL is used to drop an existing table in
a database.(drop structure also)
• Syntax: DROP TABLE table name;
• Delete: (delete only row)
• DELETE FROM table_name WHERE {CONDITION};
• Truncate: (delete only data
• TRUNCATE TABLE table_name;
SQL constraints
• The Constraints in SQL can be specified when the table is created with
the CREATE TABLE statement, or after the table is altered with the ALTER
TABLE statement.
• Syntax: CREATE TABLE table_name ( column1 datatype constraint,
column2 datatype constraint, column3 datatype constraint, .... );
• SQL constraints are used to specify any rules for the records in a table.
• Constraints can be used to limit the type of data that can go into a table.
• if there is any violation between the constraint and the record action, the
action is aborted.
• Constraints can be column level or table level.
Constraints using alter table
• Add unique constraint:
• ALTER TABLE persons ADD UNIQUE (first_name);
• ALTER TABLE person ADD CONSTRAINT UC_person UNIQUE (age, last_name);
• Drop constraint
• ALTER TABLE person DROP CONSTRAINT UC_Person;
Section 2
Data retrieval from single table
SQL Query
• A database most often contains tables. Some name identifies each table. The table
includes records(rows) with Data. To access those records, we need SQL Syntax. Most of
the action you need to perform Database by using the SQL Statement.
• Note: SQL keywords are not case-sensitive (e.g., select as SELECT)
• Keywords include SELECT, UPDATE, WHERE, ORDER BY ETC. Four fundamental
operations that can apply to any databases are:
• Read the Data -- SELECT
• Insert the new Data -- INSERT
• Update existing Data -- UPDATE
• Remove Data –DELETE
• These operations are referred to as the CRUD (Create, Read, Update, Delete).
Movie dataset
• For all the queries in upcoming slides we will use Movie dataset
• Import dataset in MySQL workbench
• First try to understand dataset using excel file of the dataset
• In excel file for one table there is one sheet
SELECT Statement
• The SELECT statement permits you to read data from one or more
tables.
• Syntax:
SELECT column1, column2, ,columnN
FROM table_name;
• Examples:
• Simply print all the movies
• Get movie title and industry for all the movies
SELECT DISTINCT
• ASSIGNING VARIABLE
• Once we declare the variable, now it is ready to use. To assign a value to the
variable use SET
• statement:
• Variable scope is for limited time period. It is defined inside the stored procedure
within BEGIN and it will be out of scope once the END statement reaches.
TRIGGERS
• Trigger is a stored program that invoked automatically in response to an
event such as insert, delete or update that occurs in the table. Suppose,
you defined a trigger and you insert a row inside the table, then it will
automatically invoked before or after the insertion of row.
• There are two types of the TRIGGERS:
• Row-level-Triggers: it is activated for each row that is inserted, deleted or updated.
• Statement-level-Triggers: it is executed for each transaction.
• Advantage of Triggers
• It provides a way to check the integrity in data.
• It can be useful for auditing the data changes in tables
• It handles the errors from the database layer.
CREATING TRIGGERS