0% found this document useful (0 votes)
59 views27 pages

Database (Lab02)

The document discusses various SQL statements used for database management. It explains statements for creating, altering, dropping, and backing up databases and tables. It also covers statements for inserting, updating, deleting and selecting data from tables, as well as creating constraints for data validation.

Uploaded by

Ujala Jamil
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)
59 views27 pages

Database (Lab02)

The document discusses various SQL statements used for database management. It explains statements for creating, altering, dropping, and backing up databases and tables. It also covers statements for inserting, updating, deleting and selecting data from tables, as well as creating constraints for data validation.

Uploaded by

Ujala Jamil
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/ 27

Database

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

• INSERT INTO Customers (CustomerID, CustomerName,


ContactName, Address, City, PostalCode, Country)
VALUES (92,'Cardinal', 'Tom B. Erichsen', 'Skagen
21', 'Stavanger', '4006', 'Norway');
Insert Data Only in Specified Columns

• It is also possible to only insert data in specific columns.


• The following SQL statement will insert a new record,
but only insert data in the "CustomerName", "City", and
"Country" columns (CustomerID will be updated
automatically):
• Example
• INSERT INTO Customers
• (CustomerName, City, Country)
VALUES 
• ('Cardinal', 'Stavanger', 'Norway');
Insert Data Only in Specified Columns
SQL CONSTARINT
• SQL constraints are used to specify rules for data in a table.
• SQL Create Constraints
• Constraints can be specified when the table is created with the
CREATE TABLE statement, or after the table is created with the
ALTER TABLE statement.
• Syntax
• CREATE TABLE table_name (
    column1 datatype constraint,
    column2 datatype constraint,
    column3 datatype constraint,
    ....
);
CONSTRAINTS
• 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 - Uniquely identifies a row/record in another table
• CHECK - Ensures that all values in a column satisfies a specific
condition
• DEFAULT - Sets a default value for a column when no value is specified
• INDEX - Used to create and retrieve data from the database very
quickly
EXAMPLE
• CREATE TABLE Student(
• Sid INT(8) PRIMARY KEY, Sname
VARCHAR(50),Phoneno VARCHAR(50)
• )
NOT NULL
• 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.
Example
• The following SQL ensures that the "ID", "LastName",
and "FirstName" columns will NOT accept NULL values
when the "Persons" table is created:
• Example
• CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255) NOT NULL,
    Age int
);
UNIQUE CONSTRAINT
• The UNIQUE constraint ensures that all values in a column
are different.
• A PRIMARY KEY constraint automatically has a UNIQUE
constraint.

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

You might also like