Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Sql

893 views

Published on

sql basics and fundamentals with database connections

Published in: Entertainment & Humor
  • Be the first to comment

  • Be the first to like this

Sql

  1. 1. SQL<br />Structured Query Language<br />
  2. 2. SQL-Create table<br />Create table table_name(column_name1 datatype, column_name2 datatype……)<br />Eg:<br />create table example (id int ,name varchar(10),address varchar(10))<br />Msg:<br />Command(s) completed successfully.<br />
  3. 3. SQL-Insert Values<br />Insert into table_name(column1,column2,…..)values(values1,values2,…)<br />Eg:<br />insert into example (id,name,address) values(123,'xxxx','yyyyy')<br />Msg:<br />(1 row(s) affected)<br />
  4. 4. SQL-Select Command<br />select *from example<br />Id name address<br />123 xxxxyyyyy<br />456 jjjjrrrr<br />456 iiiinnnn<br />567 eeeeffff<br />
  5. 5. SQL-Alter Command<br />Alter table table_name add or drop column_namedatatype<br />Eg: alter table example add mobilenoint<br />Msg: Command(s) completed successfully.<br />Id name address mobileno<br />123 xxxxyyyyy NULL<br />456 jjjjrrrr NULL<br />888 iiiinnnn NULL<br />567 eeeeffff NULL<br />
  6. 6. SQL-Update Command<br />Update table_name set column_name=new value where column_name=any value<br />Eg: Update example set id=888 where name='iiii‘<br />Msg: (1 row(s) affected)<br />Id name address<br />123 xxxxyyyyy<br />456 jjjjrrrr<br />888 iiiinnnn<br />567 eeeeffff<br />
  7. 7. SQL-Delete Command<br />Delete table_name where coition<br />Eg: delete example where id=888<br />Msg: (1 row(s) affected)<br />Id name address mobileno<br />123 xxxxyyyyy NULL<br />456 jjjjrrrr NULL<br />567 eeeeffff NULL<br />
  8. 8. SQL-Drop Command<br />Drop table table_name<br />Eg: drop table example<br />Msg:Command(s) completed successfully.<br />
  9. 9. SQL-Primary Key & Foreign Key <br />CREATE TABLE table_name (column1 datatype null/not null, column2 datatype null/not null,...CONSTRAINT constraint_name PRIMARY KEY (column1, column2, . column_n));<br />CREATE TABLE table_name (column1 datatype,  column2 datatype, column3 datatype,  Primary Key (column_any), Foreign Key (Column_any) references Table_any(datatype));<br />
  10. 10. SQL-Primary Key & foreign Key<br />create table example (id intprimary key,namevarchar(10),address varchar(10))<br />A primary key is used to unique and Not Null identify each row in the table.<br />create table example2 (salary int,expamountint, id int references example(id))<br />A foreign key is a referential constraint between two tables. <br />
  11. 11. SQL-Primary Key & Foreign Key <br />Id name address <br />123 rrrrtttt<br />369 klkliooo<br />456 iiiihhhh<br />7889 wswsweww<br />Salary expamount id<br />10000 4235 7889<br />12369 8526 456<br />12369 865 456<br />65894 12589 123<br />Example (primary Key tale)<br />Example2 (Foreign Key tale)<br />
  12. 12. SQL-Primary Key & Foreign Key <br />select name,address,salary from example e,example2 e2 where e.id=e2.id<br />O/P<br />Name address salary<br />wswsweww 10000<br />iiiihhhh 12369<br />iiiihhhh 12369<br />rrrrtttt 65894<br />
  13. 13. SQL-Connection String<br />string strcon = "Data Source=172.16.0.1;Initial Catalog=BatchThree;User ID=magnum;Password=Magnum";<br />SqlConnectionsqlcon = new SqlConnection(strcon);<br />sqlcon.Open();<br />stringstrsql = "insert into datab values<br /> ('" + txtmarks1.Text + "','" + txtmarks2.Text + "','" + txtmarks3.Text + "','" + txtname.Text + "')";<br />SqlCommandcmd = new SqlCommand(strsql, sqlcon);<br />cmd.CommandType = CommandType.Text;<br />cmd.ExecuteNonQuery();<br />sqlcon.Close();<br />
  14. 14. SQL-Connection String<br />
  15. 15. SQL-Connection String<br />
  16. 16. SQL-Connection String<br />
  17. 17. SQL-Stored Procedure<br />create procedure proexample(@id int,@namevarchar(10),@address varchar(10)) as insert into example(id,name,address) values (@id,@name,@address)<br />Command(s) completed successfully.<br />exec proexample 666,'hghg','yuyu‘<br />(1 row(s) affected)<br />
  18. 18. SQL-Stored Procedure<br />select *from example<br />Id name address<br />123 rrrrtttt<br />369 klkliooo<br />456 iiiihhhh<br />666 hghgyuyu<br />7889 wswsweww<br />
  19. 19. SQL-JOINS<br />SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables<br />Inner Join<br />Left Join<br />Right Join<br />Full Join<br />
  20. 20. SQL-INNER JOIN<br />The INNER JOIN keyword return rows when there is at least one match in both tables.<br />SELECT column_name(s)FROM table_name1INNER JOIN table_name2ON table_name1.column_name=table_name2.column_name<br />
  21. 21. SQL-INNER JOIN<br />select name,address,salary from example inner join example2 on example.id=example2.id order by name<br />Name address salary<br />iiiihhhh 12369<br />iiiihhhh 12369<br />rrrrtttt 65894<br />Wswsweww 10000<br />
  22. 22. SQL-LEFT JOIN<br />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)<br />SELECT column_name(s)FROM table_name1LEFT JOIN table_name2ON table_name1.column_name=table_name2.column_name<br />
  23. 23. SQL_LEFT JOIN<br />select name,address,salary from example left join example2 on example.id=example2.id order by name<br />Name address salary<br />hghgyuyu NULL<br />iiiihhhh 12369<br />iiiihhhh 12369<br />klkliooo NULL<br />rrrrtttt 65894<br />wswsweww 10000<br />
  24. 24. SQL-RIGHT JOIN<br />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)<br />SELECT column_name(s)FROM table_name1RIGHT JOIN table_name2ON table_name1.column_name=table_name2.column_name<br />
  25. 25. SQL_RIGHT JOIN<br />select name,address,salary from example right join example2 on example.id=example2.id order by name<br />Name address salary<br />iiiihhhh 12369<br />iiiihhhh 12369<br />rrrrtttt 65894<br />wswsweww 10000<br />
  26. 26. SQL-FULL JOIN<br />The FULL JOIN keyword return rows when there is a match in one of the tables.<br />SELECT column_name(s)FROM table_name1FULL JOIN table_name2ON table_name1.column_name=table_name2.column_name<br />
  27. 27. SQL-FULL JOIN<br />select name,address,salary from example full join example2 on example.id=example2.id order by name<br />Name address salary<br />hghgyuyu NULL<br />iiiihhhh 12369<br />iiiihhhh 12369<br />klkliooo NULL<br />rrrrtttt 65894<br />wswsweww 10000<br />
  28. 28. End)<br />Prabhu.ftz@gmail.com<br />

×