sql

34
SQL Structured Query Language

Upload: ftz-420

Post on 20-Jan-2015

1.695 views

Category:

Education


1 download

DESCRIPTION

Basic Command Using in SQL

TRANSCRIPT

Page 1: Sql

SQL

Structured Query Language

Page 2: Sql

SQL-Create table

• Create table table_name(column_name1 datatype, column_name2 datatype……)

Eg:• create table example (id int ,name

varchar(10),address varchar(10))Msg:• Command(s) completed successfully.

Page 3: Sql

SQL-Insert Values

• Insert into table_name(column1,column2,…..)values(values1,values2,…)

Eg:• insert into example (id,name,address)

values(123,'xxxx','yyyyy')Msg:(1 row(s) affected)

Page 4: Sql

SQL-Select Command

• select *from exampleId name address123 xxxx yyyyy456 jjjj rrrr456 iiii nnnn567 eeee ffff

Page 5: Sql

SQL-Alter Command• Alter table table_name add or drop

column_name datatypeEg: alter table example add mobileno int Msg: Command(s) completed successfully.Id name address mobileno123 xxxx yyyyy NULL456 jjjj rrrr NULL888 iiii nnnn NULL567 eeee ffff NULL

Page 6: Sql

SQL-Update Command• Update table_name set column_name=new

value where column_name=any valueEg: Update example set id=888 where name='iiii‘Msg: (1 row(s) affected)Id name address123 xxxx yyyyy456 jjjj rrrr888 iiii nnnn567 eeee ffff

Page 7: Sql

SQL-Delete Command

• Delete table_name where conditionEg: delete example where id=888Msg: (1 row(s) affected)Id name address mobileno123 xxxx yyyyy NULL456 jjjj rrrr NULL567 eeee ffff NULL

Page 8: Sql

SQL-Drop Command

• Drop table table_nameEg: drop table exampleMsg:Command(s) completed successfully.

Page 9: Sql

SQL-Primary Key & Foreign Key CREATE TABLE table_name (column1 datatype null/not null,

column2 datatype null/not null,...CONSTRAINT constraint_name PRIMARY KEY (column1, column2, . column_n));

CREATE TABLE table_name (column1 datatype, column2 datatype, column3 datatype, Primary Key (column_any), Foreign Key (Column_any) references Table_any(datatype));

Page 10: Sql

SQL-Primary Key & foreign Key• create table example (id int primary key,name

varchar(10),address varchar(10))A primary key is used to unique and Not Null

identify each row in the table.

• create table example2 (salary int,expamount int, id int references example(id))

A foreign key is a referential constraint between two tables.

Page 11: Sql

SQL-Primary Key & Foreign Key Id name address 123 rrrr tttt369 klkl iooo456 iiii hhhh7889 wsws weww

Salary expamount id10000 4235 788912369 8526 45612369 865 45665894 12589 123

Example (primary Key tale)

Example2 (Foreign Key tale)

Page 12: Sql

SQL-DISTINCT

• select distinct address from exaddressioioklkkyuyu

Page 13: Sql

SQL-Primary Key & Foreign Key

• select name,address,salary from example e,example2 e2 where e.id=e2.id

O/PName address salarywsws weww 10000iiii hhhh 12369iiii hhhh 12369rrrr tttt 65894

Page 14: Sql

SQL-BETWEEN & COUNT• SELECT "column_name“ FROM "table_name"

WHERE "column_name" BETWEEN 'value1' AND 'value2‘

• select id from ex where id between 100 and 500ID123456• SELECT COUNT("column_name") FROM "table_name“• select count(id)'No Of Records' from exNo Of Records4

Page 15: Sql

SQL-Connection String• string strcon = "Data Source=172.16.0.1;Initial

Catalog=BatchThree;User ID=magnum;Password=Magnum";

• SqlConnection sqlcon = new SqlConnection(strcon);• sqlcon.Open();• string strsql = "insert into datab values

('" + txtmarks1.Text + "','" + txtmarks2.Text + "','" + txtmarks3.Text + "','" + txtname.Text + "')";

• SqlCommand cmd = new SqlCommand(strsql, sqlcon); cmd.CommandType = CommandType.Text;• cmd.ExecuteNonQuery(); sqlcon.Close();

Page 16: Sql

SQL-Connection String

Page 17: Sql

SQL-Connection String

Page 18: Sql

SQL-Connection String

Page 19: Sql

SQL-Stored Procedure

• create procedure proexample (@id int,@name varchar(10),@address varchar(10)) as insert into example(id,name,address) values (@id,@name,@address)

• Command(s) completed successfully.

• exec proexample 666,'hghg','yuyu’• (1 row(s) affected)

Page 20: Sql

SQL-Stored Procedure

• select *from exampleId name address123 rrrr tttt369 klkl iooo456 iiii hhhh666 hghg yuyu7889 wsws weww

Page 21: Sql

SQL-JOINS

• SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables

• Inner Join• Left Join• Right Join• Full Join

Page 22: Sql

SQL-INNER JOIN

• The INNER JOIN keyword return rows when there is at least one match in both tables.

• SELECT column_name(s)FROM table_name1INNER JOIN table_name2ON table_name1.column_name=table_name2.column_name

Page 23: Sql

SQL-INNER JOIN

• select name,address,salary from example inner join example2 on example.id=example2.id order by name

Name address salaryiiii hhhh 12369iiii hhhh 12369rrrr tttt 65894Wswsweww 10000

Page 24: Sql

SQL-LEFT JOIN• The LEFT JOIN keyword returns all rows from the

left table (table_name1), even if there are no matches in the right table (table_name2)

• SELECT column_name(s)FROM table_name1LEFT JOIN table_name2ON table_name1.column_name=table_name2.column_name

Page 25: Sql

SQL_LEFT JOIN

• select name,address,salary from example left join example2 on example.id=example2.id order by name

Name address salaryhghg yuyu NULLiiii hhhh 12369iiii hhhh 12369klkl iooo NULLrrrr tttt 65894wsws weww 10000

Page 26: Sql

SQL-RIGHT JOIN

• The RIGHT JOIN keyword returns all the rows from the right table (table_name2), even if there are no matches in the left table (table_name1)

• SELECT column_name(s)FROM table_name1RIGHT JOIN table_name2ON table_name1.column_name=table_name2.column_name

Page 27: Sql

SQL_RIGHT JOIN

• select name,address,salary from example right join example2 on example.id=example2.id order by name

Name address salaryiiii hhhh 12369iiii hhhh 12369rrrr tttt 65894wsws weww 10000

Page 28: Sql

SQL-FULL JOIN

• The FULL JOIN keyword return rows when there is a match in one of the tables.

• SELECT column_name(s)FROM table_name1FULL JOIN table_name2ON table_name1.column_name=table_name2.column_name

Page 29: Sql

SQL-FULL JOIN

• select name,address,salary from example full join example2 on example.id=example2.id order by name

Name address salaryhghg yuyu NULLiiii hhhh 12369iiii hhhh 12369klkl iooo NULLrrrr tttt 65894wsws weww 10000

Page 30: Sql

SQL-VIEW

• views can be considered as virtual tables and it physically stores the data. A view also has a set of definitions,and it does not physically store the data.

The syntax for creating a view is as follows:• CREATE VIEW "VIEW_NAME" AS "SQL

Statement"

Page 31: Sql

SQL-VIEW

• create view vex as select *from ex• select *from vex id name address123 pop yuyu456 huh ioio856 exa rere• drop view vex

Page 32: Sql

SQL-DISTINCT• The SELECT UNIQUE term is an Oracle-only SQL statement. It is

equivalent to SELECT DISTINCT. SyntaX:• SELECT DISTINCT "column_name"

FROM "table_name“

Eg: select *from ex

Id name address123 pop yuyu456 huh ioio856 exa klkk856 exa klkk

Page 33: Sql

SQL-IN & LIKE• SELECT "column_name“ FROM "table_name"

WHERE "column_name" IN ('value1', 'value2', ...)• select name,address from ex where id in(456)name addresshuh ioio• SELECT "column_name“ FROM "table_name"

WHERE "column_name" LIKE {PATTERN}• select name,address from ex where id like(456)name addressHuh ioio