+ All Categories
Home > Technology > Overview of sql basics part 2 - by software outsourcing company india

Overview of sql basics part 2 - by software outsourcing company india

Date post: 14-Apr-2017
Category:
Upload: jignesh-aakoliya
View: 34 times
Download: 4 times
Share this document with a friend
20
Prepared By: Mr. Dhrumil Patel iFour Consultancy Advance SQL
Transcript
Page 1: Overview of sql basics  part 2 - by software outsourcing company india

Prepared By:Mr. Dhrumil Patel

iFour Consultancy

Advance SQL

Page 2: Overview of sql basics  part 2 - by software outsourcing company india

SQL Commands

Some of The Most Important SQL Commands• SELECT - extracts data from a database• UPDATE - updates data in a database• DELETE - deletes data from a database• INSERT INTO - inserts new data into a database• CREATE DATABASE - creates a new database• ALTER DATABASE - modifies a database• CREATE TABLE - creates a new table• ALTER TABLE - modifies a table• DROP TABLE - deletes a table• CREATE INDEX - creates an index (search key)• DROP INDEX - deletes an index

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 3: Overview of sql basics  part 2 - by software outsourcing company india

SQL CREATE DATABASE Statement

The CREATE DATABASE statement is used to create a database.• CREATE DATABASE dbname;

Database tables can be added with the CREATE TABLE statement.

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 4: Overview of sql basics  part 2 - by software outsourcing company india

The CREATE TABLE statement is used to create a table in a database. Tables are organized into rows and columns; and each table must have a name. SQL CREATE TABLE Syntax

• CREATE TABLE table_name(column_name1 data_type(size),column_name2 data_type(size),column_name3 data_type(size),....);

The column_name parameters specify the names of the columns of the table. The data_type parameter specifies what type of data the column can hold. The size parameter specifies the maximum length of the column of the table.

SQL CREATE TABLE Statement

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 5: Overview of sql basics  part 2 - by software outsourcing company india

The SELECT statement is used to select data from a database. The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set. SQL SELECT Syntax:

SELECT column_name,column_name FROM table_name; and SELECT * FROM table_name;

SELECT Statement

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 6: Overview of sql basics  part 2 - by software outsourcing company india

The INSERT INTO statement is used to insert new records in a table. It is possible to write the INSERT INTO statement in two forms. The first form does not specify the column names where the data will be

inserted, only their values: INSERT INTO table_name

VALUES (value1,value2,value3,...);

The second form 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

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 7: Overview of sql basics  part 2 - by software outsourcing company india

The UPDATE statement is used to update existing records in a table. SQL UPDATE Syntax

UPDATE table_nameSET column1=value1,column2=value2,...WHERE some_column=some_value;

SQL UPDATE Statement

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 8: Overview of sql basics  part 2 - by software outsourcing company india

The DELETE statement is used to delete records in a table. SQL DELETE Syntax:

DELETE FROM table_nameWHERE some_column=some_value;

It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be intact:

DELETE FROM table_name;

or

DELETE * FROM table_name;

SQL DELETE Statement

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 9: Overview of sql basics  part 2 - by software outsourcing company india

The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

SQL GROUP BY Syntax:

SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_name;

SQL Group By Statement

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 10: Overview of sql basics  part 2 - by software outsourcing company india

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

SQL HAVING Syntax:

SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_nameHAVING aggregate_function(column_name) operator value;

SQL having Clause

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 11: Overview of sql basics  part 2 - by software outsourcing company india

SQL joins are used to combine rows from two or more tables. An SQL JOIN clause is used to combine rows from two or more tables, based on a

common field between them. The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN return all rows from multiple tables where the join condition is

met.

SQL Joins

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 12: Overview of sql basics  part 2 - by software outsourcing company india

Different SQL JOINs• INNER JOIN: Returns all rows when there is at least one match in BOTH tables.• Syntax:• SELECT table1.Column_name,table2.Column_name

FROM table1INNER JOIN table2ON table1.column_name=table2.column_name;

• LEFT JOIN: Return all rows from the left table, and the matched rows from the right table.• Syntax:• SELECT table1.Column_name,table2.Column_name

FROM table1LEFT OUTER JOIN table2ON table1.column_name=table2.column_name;

Types Of JOINs

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 13: Overview of sql basics  part 2 - by software outsourcing company india

RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table.

• Syntax:• SELECT table1.Column_name,table2.Column_name

FROM table1RIGHT OUTER JOIN table2ON table1.column_name=table2.column_name;

• FULL JOIN: Return all rows when there is a match in ONE of the tables.• Syntax:• SELECT table1.Column_name,table2.Column_name

FROM table1FULL OUTER JOIN table2ON table1.column_name=table2.column_name;

Types Of JOINs

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 14: Overview of sql basics  part 2 - by software outsourcing company india

A stored procedure is a group of sql statements that has been created and stored in the database. Stored procedure will accept input parameters so that a single procedure can be used over the

network by several clients using different input data. Stored procedure will reduce network traffic and increase the performance. If we modify stored

procedure all the clients will get the updated stored procedure. Syntax:

CREATE PROCEDURE dbo.sp_whoAS SELECT FirstName, LastName FROM Person.Person;GOEXEC sp_who;GO

Stored Procedure

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 15: Overview of sql basics  part 2 - by software outsourcing company india

A function is a special kind stored program that returns a single value. You use functions to encapsulate common formulas or business rules that are

reusable among SQL statements or stored programs. Syntax:

CREATE FUNCTION function_name(param1,param2,…) RETURNS datatype [NOT] DETERMINISTICstatements

Function

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 16: Overview of sql basics  part 2 - by software outsourcing company india

Difference Between Function and Stored Procedure

Function Stored ProcedureFunction must return a value. In Stored Procedure it is optional( Procedure can return zero or n

values).

Functions will allow only input parameters, doesn’t support output parameters.

Whereas Procedures can have input/output parameters.

Functions can be called from Procedure. Whereas Procedures cannot be called from Function.

Function allows only SELECT statement in it. Whereas Procedure allows SELECT as well as DML(INSERT/UPDATE/DELETE) statement in it.

Function will not allow us to use try-catch blocks. Whereas in Procedure, for exception handling we can use try catch blocks.

We can use only table variables, it will not allow using temporary tables.

Can use both table variables as well as temporary table in it.

Functions can be called from a select statement. Procedures can’t be called from Select/Where/Having and so on statements. Execute/Exec statement can be used to call/execute Stored Procedure.

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 17: Overview of sql basics  part 2 - by software outsourcing company india

In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.)

Consider a person who needs to know a customer’s name, loan number and branch name, but has no need to see the loan amount. This person should see a relation described, in SQL, by

(select customer_name, borrower.loan_number, branch_name from borrower, loan where borrower.loan_number = loan.loan_number )

A view provides a mechanism to hide certain data from the view of certain users. Any relation that is not of the conceptual model but is made visible to a user as a “virtual

relation” is called a view.

Views

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 18: Overview of sql basics  part 2 - by software outsourcing company india

A view consisting of branches and their customers:create view all_customer as

(select branch_name, customer_name

from depositor, account where depositor.account_number = account.account_number

) union

(select branch_name, customer_name

from borrower, loan where borrower.loan_number = loan.loan_number

)

Views (Cont.)

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 19: Overview of sql basics  part 2 - by software outsourcing company india

www.w3schools.com www.tutorialspoint.com

References

Open Source Company In Indiahttp://www.ifourtechnolab.com

Page 20: Overview of sql basics  part 2 - by software outsourcing company india

Thank you

Software development company indiahttp://www.ifourtechnolab.com


Recommended