+ All Categories
Home > Documents > Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer...

Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer...

Date post: 04-Jan-2016
Category:
Upload: catherine-walker
View: 217 times
Download: 0 times
Share this document with a friend
31
Topic 1: Introduction to SQL
Transcript
Page 1: Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.

Topic 1: Introduction to SQL

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

• 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.

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

SQL commands can be divided into following categories.

• Data Definition Language (DDL)• Data Manipulation Language (DML) • Transaction Control Language (TCL)

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

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.

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

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.

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

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.

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

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]);

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

• 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

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

Example

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

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

STUDENT

Name Roll Class Age Marks

Raghav 101 C12 15 91

Ajay 102 M12 14 92

Gagan 103 C12 15 80

Shika 104 E12 14 85

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

Delete table:

• Drop table name• Example drop student;

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

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.

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

• Example: Write a command to insert a row in the table student.

• Ans: Insert into student values (‘ahmed’, 105, C12, 16, 98);

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

• • UPDATE STATEMENT: Update command is

used to make changes to the existing values.

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

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;

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

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;

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

Example 3: Write a command to update all the marks by adding 5

marks for all students.

• Ans: Update Student set marks = marks +5;

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

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;

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

• Next class

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

• 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;

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

• 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.

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

• 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;

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

• DISTINCT can be used to select column names and values deleting duplicates.

• Output: • Class• C12• M12• E12

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

• 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.

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

Syntax

SELECT column FROM tableWHERE column operator value

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

With the WHERE clause, the following operators can be used:

Operator Description= Equal<> Not equal> Greater than< Less than>= Greater than or equal

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

<= Less than or equalBETWEEN Between an inclusive rangeLIKE Search for a patternIN If you know the exact value you

want to return for at least one of the columns

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

Example 1: Select name, marks from student where age > 14;

Name Marks

Raghav 90

Gagan 90

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

Example 2: Write a query to display all details about the students whose age

is 15.• Ans: Select * from Student where age=15;

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

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;

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

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’;


Recommended