23
Second session

intro for sql

Embed Size (px)

Citation preview

Page 1: intro for sql

Second session

Page 2: intro for sql

SQL language

Data type

Select statement

Objectives

Page 3: intro for sql

SQL language

DCLDMLDDL

• select• Insert• Delete• update

• create• drop• Alter• truncat

e

• grant• Revoke• deny

Page 4: intro for sql
Page 5: intro for sql

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

Page 6: intro for sql

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.

Page 7: intro for sql

 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.

Page 8: intro for sql

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 .. ??

Page 9: intro for sql

Data Types 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.

Page 10: intro for sql

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.

Page 11: intro for sql

Exact numeric

Page 12: intro for sql

Approximate numerics

Page 13: intro for sql

Character StringsThe 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.

Page 14: intro for sql

Unicode Character Strings

Page 15: intro for sql

Binary Strings

Page 16: intro for sql

CREATE TABLE

• CREATE TABLE 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))

Page 17: intro for sql

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 :

Page 18: intro for sql

OperatorsOperators :

BETWEEN – Range of valueLIKE – To find a name contaning specific characterIN – 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 ProductsWHERE Price BETWEEN 10 AND 20;

Page 19: intro for sql

INSERT

INSERT INTO table_nameVALUES (value1,value2,value3,...);

insert into employees values (1,‘mones','nasr city')

INSERT INTO table_name (column1,column2,column3,...)VALUES (value1,value2,value3,...);

INSERT INTO employees (emp_name) VALUES ( 'moo');

Page 20: intro for sql

Alter

To add a new column to a table :

ALTER TABLE table_name ADD column_name datatype

ALTER TABLE employees ADD DateOfBirth date

To change structure of an existing column :

ALTER TABLE table_name ALTER COLUMN column_name datatype

ALTER TABLE employees ALTER COLUMN DateOfBirth year

Page 21: intro for sql

DropTo remove an existing databaseDROP DATABASE database_name

DROP DATABASE Sales;

To remove an existing table from a databaseDROP TABLE table_name DROP TABLE ProductVendor1 ;

To drop coulmnALTER TABLE table_name DROP COLUMN column_name

ALTER TABLE employees DROP COLUMN Id;

Page 22: intro for sql

Try this select * from employeesupdate 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 employeesWHERE salary> 3000

insert into alltypes values( 100,'Mohammad','ZPR02D','[email protected]','Male','0205468712',28/4/1973,45000,2500.00 )

Page 23: intro for sql

Mahmoud mones [email protected]