Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
-Structured query language is query language for storing modifying and retrieving data from
database.
DDL
create
Alter
Drop
DML
Select
Insert
Update
Delete
DCL
Grant
Revoke
Create
Eg:- Create table tab1 (name varchar(20),id int, age int, dob date);
The new table will be filled with data from existing table have the same column definition
Eg:- create table tab2 as select name,id from tab1 where id<5;
Drop
Truncate is used to delete data inside table not entire table definition.
Alter
In Mssql
In oracle 10g
Insert
Select into
Eg:-
Delete
Update
Select
Select database
Use dbname
Distinct
Operators to use in where clause are =, <, >, <=, >=, BETWEEN, LIKE, IN
Orderby
Limit (mysql)
Rownum (oracle)
Max/Min
Eg: -
Wildcards % ,_ , [],!/^
%a-ends with a
_r%a_i any char followed by r followed by any chars ends with a followed by any char ends with i.
IN / not in
Aliases
Mysql
Constraints
Rules enforced on data of table to limit type of data ensures accuracy and reliability of data in
database. Either column or table level.
Create table customer (name varchar, id int NOT NULL, address varchar(100));
create
Alter constraint
Drop
create
Drop
Create table order (order_id int primary key , person_id int foreign key references
person(person_id),
Create table order (order_id int, person_id int, primary key (order_id) foreign key (person _id)
references person (person-id);
Create table order (order_id int, person_id int , constraint con2 foreign key (person_id) references
person (person_id);
Alter
Alter table order add constraint ufkey foreign key(pid) references person (person_id);
Create table order (name varchar,id int , address varchar default “Chennai”);
Alter
Alter table order alter column address set default ‘chennai’; (sql server)
Alter table order alter address set default ‘chennai’; (mysql)
Alter table order modify address default ‘chennai’;(oracle)
Drop
Alter table order alter column address drop default;
Alter table order alter address drop default; (mysql);
create table person (id int, name varchar, age int check(age>=18);
Create table person (id int, name varchar, age int, constraint chk_age check (age<=18));
Create table person (id int, name varchar, age int, check(age>=18));
Alter
Drop
Drop
AutoIncrement
Create table tab1 (name varchar, id int identity (1,1); sql server
Minvalue 1
start with 1
increment by 1
cache 10;
Group by
Having
Having clus is used along with groupby used to restrict dta send to groupby clause.
Where is filters the data before grouping whereas having filters after grouping.
Eg:-
Select name ,id ,age, department from person group by department have age>50;
Join
Join is used to combine rows from two or more tables based on related columns between them.
Inner join
Left join
Right join
Full join
Cross join
Inner join or equi join or natural join
Select person .name ,person. Address, order .orderno from person inner join order on
person.id=order.id order by person.id.
When to useinner join
Use an inner join when you want to match values from both tables.
Use inner joins to obtain information from two separate tables and combine that information in one result set.
When you use inner joins, consider the following facts and guidelines:
1. Inner joins are the SQL Server default. You can abbreviate the INNER JOIN clause to JOIN.
2. Specify the columns that you want to display in your result set by including the qualified column names in the
select list.
3. Include a WHERE clause to restrict the rows that are returned in the result set.
4. Do not use a null value as a join condition because null values do not evaluate equally with one another.
5. SQL Server does not guarantee an order in the result set unless one is specified with an ORDER BY clause.
[Source: Microsoft SQL Server 7.0 data warehousing training]
Left
Returns all records from left table and matched records from right table .
Use left or right outer joins when you require a complete list of data that is
stored in one of the joined tables in addition to the information that matches the join condition.
Use an outer join when you want to find unmatched rows. (because either you want to find missing values from
one of the tables, like in this case, or you may want to include all values from your tables even if they are
missing values in the other table).
Self join
To dispay emp and their manager name where as namager is not in that table
Select E1.name as employee E2.name as manager from emp e1 emp e2 where E1.mgr_id==E2.id;
While self-joins rarely are used on a normalized database, you can use them to reduce
the number of queries that you execute when you compare values of different columns
of the same table.
You must specify table aliases to reference two copies of the table. Remember that table
aliases are different from column aliases. Table aliases are designated as the table name
followed by the alias.
When you create self-joins, each row matches itself and pairs are repeated, resulting in
duplicate rows. Use a WHERE clause to eliminate these duplicate rows [Microsoft SQL
Server 7.0 data warehousing training
Every row of one table is matched with every row of another table.
Each Select statement must have same no of columns with same datatype in same order
EG:-
Union
Views
Create eg:-
SUBQUERY
Eg:-
Select product name ,price from productdetails where product_id in (select id from orderdetails );