Database (Lab02)
Database (Lab02)
Lab 02
SQL
• SQL stands for structure query language
• SQL is used to insert, delete and update the
data .
• Developed by E.F. codd and IBM in 1970
Types of SQL
• DDL(Data definition language)
Example:
CREATE, ALTER,DRROP
• DML(Data Manipulation language)
• Example:
• SELECT,UPDATE,INSERT,DELETE
• DCL(DATA CONTROL LANGUAGE)
• Example:
• GRANT, REVOKE
CREATE STATEMENT
• The CREATE DATABASE statement is used to
create a new SQL database.
• Syntax
• CREATE DATABASE databasename;
• EXAMPLE:
• CREATE DATABASE university;
DROP STATEMENT
• The DROP DATABASE statement is used to
drop an existing SQL database.
• Syntax
• DROP DATABASE databasename;
• EXAMPLE:
• DROP DATABASE university;
BACKUP STATEMENT
• The SQL BACKUP DATABASE Statement
• The BACKUP DATABASE statement is used in
SQL Server to create a full back up of an
existing SQL database.
• Syntax
• BACKUP DATABASE databasename
TO DISK = 'filepath';
• EXAMPLE:
CREATE TABLE STATEMENT
• CREATA TABLE statement is used to create table in
database
• Syntax:
• CREATE TABLE table_name
• (
• Column1 Datatype Size,
• Column2 DataType Size,
• Column3 DataType size,
• …
• );
CREATE TABLE STATEMENT
• Table_name= Name of the table where data is
stored.
• column1,column2=Name of the columns of a
table.
• data type=Char, varchar, integer, decimal, date
and more
• Size=Maximum length of the column of a
table.
CREATE TABLE STATEMENT
• Example
• CREATE TABLE student (
• Sid INT (50), Sname varchar (60), Phone
no(30)
)
DROP TABLE STATEMENT
• The DROP TABLE statement is used to drop an
existing table in a database.
• Syntax
• DROP TABLE table_name;
• Example:
• DROP TABLE Student ;
ALTER TABLE STATEMENT
• The ALTER TABLE statement is used to add,
delete, or modify columns in an existing table.
• Syntax:
• ALTER TABLE table_name
ADD column_name datatype;
• Example:
• ALTER TABLE Student
ADD Email varchar(255);
INSERT INTO STATEMENT
• The INSERT INTO statement is used to insert new
records in a table.
• Syntax
• It is possible to write the INSERT INTO statement in
two ways.
• The first way specifies both the column names and the
values to be inserted:
• INSERT INTO table_name (column1, column2, column3,
...)
VALUES (value1, value2, value3, ...);
INSERT INTO STATEMENT
• If you are adding values for all the columns of
the table, you do not need to specify the
column names in the SQL query. However,
make sure the order of the values is in the
same order as the columns in the table. The
INSERT INTO syntax would be as follows:
• INSERT INTO table_name
VALUES (value1, value2, value3, ...);
EXAMPLE
• Below is a sample database
INSERT INTO Example
• CREATE TABLE Persons (
ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
update
• UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Delete statement
• DELETE FROM table_name WHERE condition;
•
• Example:
• DELETE FROM Customers WHERE CustomerNa
me='Alfreds Futterkiste';
SELECT STATEMENT
• SELECT column1, column2, ...
FROM table_name;
• If you want to select all the fields available in
the table, use the following syntax:
• SELECT * FROM table_name;