Second session
SQL language
Data type
Select statement
Objectives
SQL language
DCLDML
DDL
• select
• Insert
• Delete
• update
• create
• drop
• Alter
• truncate
• grant
• Revoke
• deny
DML
• data manipulation language (DML) In a database management system
(DBMS), a language that is used to insert data in, update, delete and query a
database.
• DML is used to manipulate the data of a database.
•Ex: Select, Update, insert, delete
Data Manipulation Language (DML)
• is the language element that allows you to use the
• core statements SELECT ,INSERT, UPDATE, DELETE to manipulate data in any
SQL Server tables. Core DML statements include the following:
• SELECT: Retrieves rows from the database and enables the selection of one
or many rows or columns from one or many tables in SQL Server.
• INSERT: Adds one or more new rows to a table or a view in SQL Server.
• UPDATE: Changes existing data in one or more columns in a table or view.
• DELETE: Removes rows from a table or view.
Data Definition Language
• DDL statements are used to build and modify the structure of your tables
and other objects in the database. When you execute a DDL statement, it
takes effect immediately
Create -To make a new database, table, index, or stored procedure.
Drop -To destroy an existing database, table, index, or view.
Alter -To modify an existing database object.
Data Control Language
• DCL is abbreviation of Data Control Language. It is used to create roles,
permissions
as well it is used to control access to database by securing it.
• Examples: GRANT, REVOKE statements
GRANT command is used by administrators to add new permissions to a
database user.
REVOKE command is used to remove database access from
a user previously
After we know SQL statement
what is Transaction Control Language .. ??
DataTypes
Data types specify the type of data that you work with in a program.
The data type defines the size of memory needed to store the data
and the kinds of operations that can be performed on the data.
Why data types are important
• Using the wrong data type can cause large performance degradation
in the database and can lead to data corruption.
• Using the wrong data type can cause the database to use significantly
more storage than needed, which is wasteful.
Exact numeric
Approximate numerics
Character Strings
The DateTimeOffset data type is similar to the DateTime data type, but it also
keeps track of time zones.
• Char(size)—Holds a fixed-length string.The fixed size is specified in
parentheses. Holds a maximum of 8,000 characters.
• Varchar(size)—Holds a variable-length string.The maximum size is
specified in parentheses. Holds a maximum of 8,000 characters.
• Text—Holds a variable-length character string. Maximum size is 2 GB
of text data.
Unicode Character Strings
Binary Strings
CREATETABLE
• CREATETABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name2 data_type,
...
)
create table employees
(
id int,
emp_name varchar(50),
address varchar(50)
)
Recap of SQL Queries
• A query in SQL can consist of up to six clauses, but only the first two, SELECT and
FROM, are mandatory.The clauses are specified in the following order:
• Allows users to retrieve specific information from the database.
SELECT <attribute list>
FROM <table list>
[WHERE <condition>]
[GROUP BY <grouping attribute(s)>]
[HAVING <group condition>]
[ORDER BY <attribute list>]
• There are three SQL commands to modify the database: INSERT, DELETE, and
UPDATE
Select statement syntax :
Operators
Operators :
BETWEEN – Range of value
LIKE –To find a name contaning specific
character
IN – Set of values
Like :
select * from employees where emp_name like 'a%'
select * from employees where emp_name like '%d'
select * from employees where emp_name like 'nabil'
select * from employees where emp_name like '_abil'
in :
SELECT * FROM employees WHERE emp_Name IN
('ahmed','ali')
between :
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
INSERT
INSERT INTO table_name
VALUES (value1,value2,value3,...);
insert into employees values
(1,‘mones','nasr city')
INSERT
INTO table_name (column1,column2,col
umn3,...)
VALUES (value1,value2,value3,...);
INSERT INTO employees (emp_name)
VALUES ( 'moo');
Alter
To add a new column to a table :
ALTERTABLE table_name
ADD column_name datatype
ALTERTABLE employees
ADD DateOfBirth date
To change structure of an existing column :
ALTERTABLE table_name
ALTER COLUMN column_name datatype
ALTERTABLE employees
ALTER COLUMN DateOfBirth year
Drop
To remove an existing database
DROP DATABASE database_name DROP DATABASE Sales;
To remove an existing table from a
database
DROPTABLE table_name DROP TABLE ProductVendor1 ;
To drop coulmn
ALTERTABLE table_name
DROP COLUMN column_name
ALTERTABLE employees
DROPCOLUMN Id;
Try this
select * from employees
update employees
set salary= 700
where emp_name like 'a%‘
create table alltypes
( id int,
firstname varchar(50),
postcode varchar(50),
email varchar(50),
gender varchar(50),
tel numeric(18,0) )
We need to select all employees who take salary more
than $3,000 from the employees table.
SELECT *
FROM employees
WHERE salary> 3000
insert into alltypes values(
100,'Mohammad','ZPR02D','test@gmail.com','Male','0205468712',28/4/1973,45000,2500
.00 )
Mahmoud mones
01115478856
Mahmoud.mones@Hotmail.com

intro for sql

  • 1.
  • 2.
    SQL language Data type Selectstatement Objectives
  • 3.
    SQL language DCLDML DDL • select •Insert • Delete • update • create • drop • Alter • truncate • grant • Revoke • deny
  • 5.
    DML • data manipulationlanguage (DML) In a database management system (DBMS), a language that is used to insert data in, update, delete and query a database. • DML is used to manipulate the data of a database. •Ex: Select, Update, insert, delete
  • 6.
    Data Manipulation Language(DML) • is the language element that allows you to use the • core statements SELECT ,INSERT, UPDATE, DELETE to manipulate data in any SQL Server tables. Core DML statements include the following: • SELECT: Retrieves rows from the database and enables the selection of one or many rows or columns from one or many tables in SQL Server. • INSERT: Adds one or more new rows to a table or a view in SQL Server. • UPDATE: Changes existing data in one or more columns in a table or view. • DELETE: Removes rows from a table or view.
  • 7.
    Data Definition Language •DDL statements are used to build and modify the structure of your tables and other objects in the database. When you execute a DDL statement, it takes effect immediately Create -To make a new database, table, index, or stored procedure. Drop -To destroy an existing database, table, index, or view. Alter -To modify an existing database object.
  • 8.
    Data Control Language •DCL is abbreviation of Data Control Language. It is used to create roles, permissions as well it is used to control access to database by securing it. • Examples: GRANT, REVOKE statements GRANT command is used by administrators to add new permissions to a database user. REVOKE command is used to remove database access from a user previously After we know SQL statement what is Transaction Control Language .. ??
  • 9.
    DataTypes Data types specifythe type of data that you work with in a program. The data type defines the size of memory needed to store the data and the kinds of operations that can be performed on the data.
  • 10.
    Why data typesare important • Using the wrong data type can cause large performance degradation in the database and can lead to data corruption. • Using the wrong data type can cause the database to use significantly more storage than needed, which is wasteful.
  • 11.
  • 12.
  • 13.
    Character Strings The DateTimeOffsetdata type is similar to the DateTime data type, but it also keeps track of time zones. • Char(size)—Holds a fixed-length string.The fixed size is specified in parentheses. Holds a maximum of 8,000 characters. • Varchar(size)—Holds a variable-length string.The maximum size is specified in parentheses. Holds a maximum of 8,000 characters. • Text—Holds a variable-length character string. Maximum size is 2 GB of text data.
  • 14.
  • 15.
  • 16.
    CREATETABLE • CREATETABLE table_name ( column_name1data_type, column_name2 data_type, column_name2 data_type, ... ) create table employees ( id int, emp_name varchar(50), address varchar(50) )
  • 17.
    Recap of SQLQueries • A query in SQL can consist of up to six clauses, but only the first two, SELECT and FROM, are mandatory.The clauses are specified in the following order: • Allows users to retrieve specific information from the database. SELECT <attribute list> FROM <table list> [WHERE <condition>] [GROUP BY <grouping attribute(s)>] [HAVING <group condition>] [ORDER BY <attribute list>] • There are three SQL commands to modify the database: INSERT, DELETE, and UPDATE Select statement syntax :
  • 18.
    Operators Operators : BETWEEN –Range of value LIKE –To find a name contaning specific character IN – Set of values Like : select * from employees where emp_name like 'a%' select * from employees where emp_name like '%d' select * from employees where emp_name like 'nabil' select * from employees where emp_name like '_abil' in : SELECT * FROM employees WHERE emp_Name IN ('ahmed','ali') between : SELECT * FROM Products WHERE Price BETWEEN 10 AND 20;
  • 19.
    INSERT INSERT INTO table_name VALUES(value1,value2,value3,...); insert into employees values (1,‘mones','nasr city') INSERT INTO table_name (column1,column2,col umn3,...) VALUES (value1,value2,value3,...); INSERT INTO employees (emp_name) VALUES ( 'moo');
  • 20.
    Alter To add anew column to a table : ALTERTABLE table_name ADD column_name datatype ALTERTABLE employees ADD DateOfBirth date To change structure of an existing column : ALTERTABLE table_name ALTER COLUMN column_name datatype ALTERTABLE employees ALTER COLUMN DateOfBirth year
  • 21.
    Drop To remove anexisting database DROP DATABASE database_name DROP DATABASE Sales; To remove an existing table from a database DROPTABLE table_name DROP TABLE ProductVendor1 ; To drop coulmn ALTERTABLE table_name DROP COLUMN column_name ALTERTABLE employees DROPCOLUMN Id;
  • 22.
    Try this select *from employees update employees set salary= 700 where emp_name like 'a%‘ create table alltypes ( id int, firstname varchar(50), postcode varchar(50), email varchar(50), gender varchar(50), tel numeric(18,0) ) We need to select all employees who take salary more than $3,000 from the employees table. SELECT * FROM employees WHERE salary> 3000 insert into alltypes values( 100,'Mohammad','ZPR02D','test@gmail.com','Male','0205468712',28/4/1973,45000,2500 .00 )
  • 23.