Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

SQL Lesson1 tutorial

Sql tutorial powerpoint

Uploaded by

sisayassefaw123
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

SQL Lesson1 tutorial

Sql tutorial powerpoint

Uploaded by

sisayassefaw123
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

STRUCTURED QUERY LANGUAGE

(SQL)

Lab Session
Lesson 1

Tizazu B. Department of Information Technology


WHAT IS SQL?
 SQL is the standard language used to communicate with a
relational database.
 SQL is the language you use to express your needs to the
database.
 you can request specific information from the database in
the form of a query using SQL.
 SQL is an ANSI standard.
 ANSI, stands for American National Standards Institute, is
an organization that is responsible for devising standards for
various products and concepts.

Tizazu B. Department of Information Technology


WHAT CAN SQL DO?
 SQL can execute queries against a database
 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database
 SQL can create stored procedures in a database
 SQL can create views in a database
 SQL can set permissions on tables, procedures, and views
Tizazu B. Department of Information Technology
TYPES OF SQL
COMMANDS
 The basic categories of commands used in a SQL to perform
various functions are:
1. DDL (Data Definition Language)
2. DML(Data Manipulation Language)
3. DQL (Data Query Language)
4. DCL (Data Control Language)
5. DAC (Data administration commands)
6. TCC (Transactional control commands)

Tizazu B. Department of Information Technology


1. Data Definition Language (DDL)
 DDL, is the part of SQL that allows a database user to create
and restructure database objects, such as the creation or
the deletion of a table.
 i.e.
Defining Database Structures
 The main DDL commands include:
 CREATE
 ALTER

 DROP

 The principal logical data definition statements are:


 CREATE TABLE, CREATE VIEW, CREATE INDEX, ALTER
TABLE, DROP TABLE, DROP VIEW, and DROP INDEX

Tizazu B. Department of Information Technology


4. Data Control Language (DCL):
 DCL in SQL allow you to control access to data within the
database.
 DCL is mainly related to the security issues,
 i.e. who has access to the database objects and what operations
they can perform on them.
 The task of DCL is to prevent unauthorized access to data.
 These DCL commands are normally used to create objects
related to user access and also control the distribution of
privileges among users.
 Some data control commands are as follows:
 ALTER PASSWORD
 GRANT
REVOKE
 CREATE SYNONYM

Tizazu B. Department of Information Technology


2. Data Manipulation Language (DML):
 DML, is the part of SQL used to manipulate data within objects of
a relational database.
 There are three basic DML commands:
 INSERT

 UPDATE

 DELETE

3. Data Query Language (DQL):


 DQL is the most commonly used SQL statements for a relational
database user.
 This SQL statement enables the users to query one or more tables
to get the information they want.
 SQL has only one data query statement:
 SELECT

Tizazu B. Department of Information Technology


5. Data Administration Commands (DAC):
 DAC allows the user to perform audits and perform analyses on
operations within the database.
 They can also be used to help analyze system performance.
 Two general data administration commands are:
 START AUDIT
 STOP AUDIT
 Do not get Data Administration confused with Database
Administration.
 Database Administration is the overall administration of a
database, which envelops the use of all levels of commands.
6. Transactional Control Commands (TCC):
 TCC allows the user to manage database transactions.
 COMMIT Used to save database transactions
 ROLLBACK Used to undo database transactions
 SAVEPOINT Creates points within groups of transactions in which
to ROLLBACK
 SET TRANSACTION Places a name on a transaction
LETS START SQL QUERY
 Procedures to open Microsoft SQL server
 Start-> All program -> Microsoft SQL Server 2008 -> click on
SQL server management studio -> Select Server Type to
“Database Engine” -> Select Server name -> Click Connect.
 We will start our SQL query with the common SQL
commands:
 CREATE
 INSERT
 SELECT
 DELETE
 UPDATE
Creating and Dropping Database
 The Create statement:
 Syntax:
 CREATE <object type> <object name>
 Object type- can be database or table
 Object name- can be database name or table name
 CREATING DATABASE
 The most basic syntax for the CREATE DATABASE statement looks
like :
 CREATE DATABASE <database name>

Example:
Create database myFirstDB;
 The Drop statement:
 Used to remove the databases.
 Syntax:
 Drop <object type> <object name>
 Object type- can be database or table
 Object name- can be database name or table name
 Dropping Database
 The most basic syntax for the DROP DATABASE statement looks like :
 DROP DATABASE <database name>

Example:
Drop database myFirstDB;
Creating Tables
The ‘CREATE TABLE’ Statement
used to create a table in a database.
Syntax:
Use Database_Name
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
);
The data type specifies what type of data the column can
hold.
Example:
use myFirstDB
create table Student
(
stud_id varchar(10),
stud_Fname varchar(50),
stud_Lname varchar(50),
stud_age integer,
stud_sex varchar(50)
);
Adding Attributes
 Syntax:
ALTER TABLE Table_Name
ADD column_name data_type;
 The ALTER TABLE command allows the user to
change the structure of a table.
 Example:
ALTER Table student
Add Stud_grade varchar(1);
Add Birthplace attributes on yours existing database
table ???
Deleting Attributes
 Syntax:
ALTER TABLE Table_Name
DROP COLUMN column_name;
Example:
ALTER Table student
DROP COLUMN CGPA;

Deleting The Table


 Syntax:
DROP TABLE table_name;
 Example:
Drop table student;
Inserting Values to the Table
 Syntax:
INSERT INTO table_Name (column 1, column 2, …)
Values(value 1, value2, …);

Example:
insert into student (stud_id, stud_Fname,
stud_Lname, stud_sex,)
values (‘201’,’Mr.X’,’Mr.Y’,’M’);
Or
insert into student
values (‘201’,’Mr.X’,’Mr.Y’,’M’);
Ex: Insert at least 5 rows in your existing table???
Tizazu B. Department of Information Technology 17
Displaying Data
 Syntax :

Select * from Table_name;

 Example:

Select * from student;

Tizazu B. Department of Information Technology


Exercise
1. Create a database called
InformationTechnology.
2. Create a table StudentExcercise
Field name
SID,firstname,Lastname,Sex,Age,Region,Zone,To
wn,PhoneNumber, Birthdate Program, batch and
birthplace
3. Insert values from table at least 10 students
4. Update 2 rows of region into amhara to Afar
5. Delete 2 rows from table where the sex value
is female.
6. View the table values where program value is
only summer student
END OF THE LESSON

You might also like