Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.

Similar presentations


Presentation on theme: "Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL."— Presentation transcript:

1 Topic 1: Introduction to SQL

2 SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL is the set of commands that is recognized by nearly all RDBMS.

3 SQL commands can be divided into following categories. Data Definition Language (DDL) Data Manipulation Language (DML) Transaction Control Language (TCL)

4 DDL (Data Definition Language) Commands: These commands are used for creation; alteration and removal of database objects e.g.: CREATE TABLE, ALTER TABLE, and DROP TABLE.

5 DML (Data Manipulation Language) Commands: These commands are used to add, modify or remove the data contained in the database tables. e.g.: INSERT, DELETE, UPDATE, SELECT etc. are DML commands.

6 TCL (Transaction Control Language) Commands These commands are used to start or end transactions. e.g.: COMMIT, ROLLBACK, ROLL BACK TO etc. are TCL commands.

7 Topic 2: DDL commands Note: All commands given below are based on the table Student: Table: Student CREATE is used to create a table. The syntax of this command is: create table tablename(column1name datatype [constraint],column2name datatype [constraint], column3name datatype [constraint]);

8 Keywords specifying Items that may be created table Data Types char(size) - Fixed length string of characters of the set size. The size of the string is limited to 255 characters. date number(maxsize) - Number with a maximum number of digits specified by "maxsize". number(maxdigits,maxright) - A decimal number with a manimum number of "maxdigits" with "a maximum number of digits to the right of the decimal, "maxright". varchar(maxsize) - A character string with variable lingth limited to "maxsize". Constraints Constraints are rules for the column.. Possible values include: not null - The column values must have a value and cannot be null. primary key - Each record is uniquely identified by this column. unique - No two values may be the same in the column

9 Example create table citylist(name varchar(20),state varchar(20),population number(8),zipcode number(5) unique);

10 STUDENT NameRollClassAgeMarks Raghav101C121591 Ajay102M121492 Gagan103C121580 Shika104E121485

11 Delete table: Drop table name Example drop student;

12 Topic 3:DML command INSERT INTO: A row or tuple can be inserted in an existing table with the help of an INSERT command.It is DML command Syntax: Insert into tablename [column list] values (val1, val2,…) Note: Values should be inserted in the same order as table creation.

13 Example: Write a command to insert a row in the table student. Ans: Insert into student values (‘ahmed’, 105, C12, 16, 98);

14 UPDATE STATEMENT: Update command is used to make changes to the existing values.

15 Example 1: Write a command to change the marks from 40 to 86 for rollno 2 Ans: Update Student Set marks =86 where rollno = 2;

16 Example 2: Write a query to change the marks as 95 and grade a A+ for rollno 31. Ans: Update Student set marks=95 and grade=’A+’ where rollno = 31;

17 Example 3: Write a command to update all the marks by adding 5 marks for all students. Ans: Update Student set marks = marks +5;

18 Example 4: Write a command to change the grade as A for all students whose marks is greater than 90. Ans: Update Student set grade=’A’ where marks > 90;

19 Next class

20 DELETE COMMAND: This command is used to delete one row or more than one row. Example: Write a command to delete the details of students who have scored less than 33 marks. Ans: Delete from Student where marks < 33;

21 SELECT: The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result-set). Syntax: SELECT [ALL | DISTINCT] columnname1 [,columnname2]FROM tablename1 [,tablename2][WHERE condition] [ and|or condition...][GROUP BY column-list][HAVING "conditions][ORDER BY "column-list" [ASC | DESC] ] Note: SQL statements are not case sensitive. SELECT is the same as select.

22 Example 1: Select * from student; Example 2: Select all from student; The above two commands display all the details from the table Student. Example 3: Select name, marks from student; This command will display only the name & marks columns from the table student. Example 4: Select Distinct class from student;

23 DISTINCT can be used to select column names and values deleting duplicates. Output: Class C12 M12 E12

24 WHERE clause: The WHERE clause is used to specify a selection criterion. To conditionally select data from a table, a WHERE clause can be added to the SELECT statement.

25 Syntax SELECT column FROM table WHERE column operator value

26 With the WHERE clause, the following operators can be used: OperatorDescription =Equal <>Not equal >Greater than <Less than >=Greater than or equal

27 <= Less than or equal BETWEEN Between an inclusive range LIKE Search for a pattern IN If you know the exact value you want to return for at least one of the columns

28 Example 1: Select name, marks from student where age > 14; NameMarks Raghav90 Gagan90

29 Example 2: Write a query to display all details about the students whose age is 15. Ans: Select * from Student where age=15;

30 Example 3: Write a query to display name, roll and class of all students whose marks is above 90. Ans: Select name, roll, class from Student where marks > 90;

31 Example 4: Write a query to display the name and age of students who belong to class C12. Ans: Select name, age from student where class = ‘C12’;


Download ppt "Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL."

Similar presentations


Ads by Google