Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
16 views
35 pages
DBMS Unit-3
Database Management System Notes
Uploaded by
purfun594
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save DBMS unit-3 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
16 views
35 pages
DBMS Unit-3
Database Management System Notes
Uploaded by
purfun594
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save DBMS unit-3 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save DBMS unit-3 For Later
You are on page 1
/ 35
Search
Fullscreen
UNIT-3| Whatis SOL? 1. SQL is Structured Query Language, which is a computer language for storing, manipulating and retrieving data stored in relational database 2. SQLis the standard language for Relation Database System, All relational database management systems like MySQL, MS Access, and Oracle, Sybase, Informix, postgres and SQL Server use SQL as standard database language Why SOL? 3. Allows users to access data in relational database management systems. 4. Allows users to describe the data. 5. Allows users to define the data in database and manipulate that data. 6. Allows embedding within other languages using SQL modules, libraries & pre-compilers. ~ Allows users to create and drop databases and tables. e Allows users to create view, stored procedure, functions in a database, 22 Hilows users to et permissions-on tables, procedures and views a —- VU VV VV Ue. - se z 2 aHistory: 10. 1970 -- Dr. E. F. “Ted” of IBM is known as the father of relational databases. He described a relational model for databases 11, 1974 ~ Structured Query Language appeared > 12. 1978 ~ IBM worked to develop Codd’s ideas and released a product named SystenvR 13. 1986 ~ IBM developed the first prototype of relational database and standardized by ANSI. The first relational database was released by Relational Software and its later becoming Oracle. SOL Process: 14, When you are executing an SQL command for any RDBMS, the system determines the best way to carry out your request and SQL engine figures out how to interpret the task. 15. There are various components included in the process. These components are Query Dispatcher, Optimization Engines, Classic Query Engine and SQL Query Engine, ete. Classic query engine handles all non-SQI. queries, but SQL query engine won't handle logical files SOL Process: = ost & eer] :The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT UPDATE, DELETE and DROP. These commands can be classified into groups based on their nature They are: DDL Commands DML Commands DCL Commands e DRL/DQL Commands TCI.Commands Data Definition Langu: Creates a new table, a view of a table, of other object in| database Modifies an existing database object, such as a table Deletes an entire table, a view of a lable or other object in the database. TRUNCATE Truncates the lable values without delete table structare Data Manipulat wuage (DML) Commands: Creates a record ‘Modifies recordsREVOKE ‘Command Description SELECT Retrieves certain records from one or more tables ‘Save work done Tdentify a point in a transaction to which we can later roll back, Roll backs Restore database to original since the last Commit A query is a question. A query is formulated for a relation/table to retrieve some useful information from the table. Different query languages are used to frame queries. Form of Basic SOL Oucry a eer? The basic form of an SQL query is as. follows: SELECT [DISTINCT] select-list (List of Attributes) FROM from-tist (Table (s) Name (s)) WHERE qualification (Condition) ‘This SELECT command is used to retrieve the data from the database.For retrieving the data every query must have SELECT clause, which specifies what columns to be selected, ‘And FROM clause, which specifies the table's names The WHERE clause, specifies the selection condition. SELECT. The SELECT list is lit of column names of tables named in the FROM list. Column names can be prefixed by a range variable. FROM: The FROM list in the FROM clause is a list of table names. A Table name can be followed by a range variable. A range variable is particularly useful when the same table name ‘appears more than once in the from-list WHERE: The qualification in the WHERE clause is a Boolean combination (i.¢., an expression using the logical connectives AND, OR, and NOT) of conditions of the form expression op expression, where op is one of the comparison operators {<, >>>} An expression is a column name, a constant, or an (arithmetic or string) expression, DISTINCT. The DISTINCT keyword is used to display the unique tuple or eliminated the duplicate tuple This DISTINCT keyword ts Optional, DDL Commands: ‘The following are the DDL commands. They are. © Create © Alter © Truncate “e Drops¥V3 type Syntax: Basic syntax of CREATE TABLE statement is as follows: CREATE TABLE table name (column! datatype (size), column2 datatype (size), column3 datatype (size) .. columnN datatype (size), PRIMARY KEY (one or more columns): Example; SQL> create table customers (id number (10) not null, name varchar? (20) not null, age number (5) not null, address char (25), salary decimal (8, 2), primary key (id)); TABLE command is used to add, delete or modify columns in an existing table ‘The basic syntax of ALTER TABLE to add a new column in an existing table is as follows: € ALTER TABLE table_name ADD column_name datatype; EX: ALTER TABLE CUSTOMERS ADD phno number (12); ii) The basic syntax of ALTER TABLE to DROP COLUMN in an existing table is as follows: ALTER TABLE table_name DROP COLUMN column_name; EX: ALTER TABLE CUSTOMERS DROP column phno; + The basic syntax of ALTER TABLE to change the DATA TYPE of a column in a table is as follows: Ex: ALTER TABLE customer MODIFY COLUMN phno number( 12),+ ‘The basic syntax of ALTER TABLE to add a NOT NULL constraint to a column in a tables as follows, ALTER TABLE table_name MODIFY column_name datatype NOT NUL ALTER TABLE customers MODIFY phno number (12), NOT NULL, + The basic syntax of ALTER TABLE to ADD PRIMARY KEY constramt to a table ts as follows: —<———— ALTER TABLE table_name ADD PRIMARY KEY (columnt, column2..): Ex: ALTER TABLE customer ADD PRIMARY KEY (id.phno), TRUNCATE: + SQLTRUNCATE TABLE command is used to delete complete data from an existing table, ‘The basic syntax of TRUNCATE. TABLE is as follows: TRUNCATE TABLE table name; EX: ‘TRUNCATE TABLE student; SELECT * FROM student; Empty set (0.00 sec).DROP; SQL DROP TABLE statement is used to remove a table definition and all data, indexes. triggers. constraints, and permission specifications for that table ‘Syntax: Basic syntax of DROP TABLE statement is as follows: DROP TABLE table_name: EX: DROP TABLE student; DML Commands: The following are the DML commands. They are * Insert + Update + Delete INSERT: © SQL INSERT INTO Statement is used to add new rows of data to a table in the database © There are two basic syntaxes of INSERT INTO statement as follows: INSERT INTO TABLE_NAME (columat, column2, column3....columaN)| VALUES. (value, value2, value3,...valueN); + Here, column}. column2.,.columaN are the names of the columns in the table into which you | want to insert data ppp pr aan Tore TANITA IIHA AAAinsert into customers (id name,age address, salary) values (1, ‘ramesh’, insert into customers (id,name,age.address,salary) values (2. ‘khilan’, 25, ‘delhi’, 1500.00 ) 2 rows inserted ‘Syntax2:_ INSERT INTO TABLE_NAME VALUES (valuel, yalue2, value3...valueN); Ex: insert into customers values (1, ‘ramesh’, 32, ahmedabad’, 2000.00 ); UPDATE: | ‘SQL UPDATE Query is used to modify the existing records in a table. We can use WHERE clause with UPDATE query to update selected rows, otherwise all the rows would be affected. ‘Syntax: ‘The basic syntax of UPDATE query with WHERE clause is as follows: SET columnl = valuel, column? = value2...., columnN = valueN + UPDATECUSTOMERS SETADDRESS = Pune’ WHERE ID = 6; + UPDATE CUSTOMERS SET ADDRESS = ‘Pune’, SALARY = 1000.00; DELETE: SQL DELETE Query is used to delete the existing records from a table ‘You can use WHERE clause with DELETE query to delete selected rows, otherwise all the records would be deleted. (WW VWASS AAAS Dt‘The basic syntax of DELETE query with WHERE clause is as follows: DELETE FROM table_name WHERE [condition]; ETE FROM CUSTOMERS WHERE ID = 6; If you want to DELETE all the records from CUSTOMERS table, you do.not need to use WHERE clause and DELETE query would be as follows: DELETE FROM CUSTOMERS, DRLDOL Command. The select command is comes under DRL/DQL. SELECT SELECT Statement is used to fetch the data from a database table which returns data in the form of result table. These result tables are called result-sets. Syotaxt: “The Following Syntax is used to retrieve specific attributes from the table is as follows: SELECT columal, column?, columaN FROM table_name; Here, column, column2...are the fields of a table whose values you want to fetch. The Following Syntax is used to retrieve all the attributes from the table is as follows. a ec et SELECT * FROM table_name; Ex: Select * from student, Distinct: ) SQL DISTINCT keyword 1s used in conjunction with SELECT statement to eliminate all the% 2 ALU W Ae ee OY duplicate records and fetching only unique records There may be a situation when you have multiple duplicate records in a table. While fetching such records, it makes more sense to fetch only unique records instead of fetching duplicate records. Syntax: ‘The basic syntax of DISTINCT keyword to eliminate duplicate records is as follows: SELECT DISTINCT column, column2,.....colummN FROM table_name WHERE [condition]: Ex: SELECT DISTINCT SALARY FROM CUSTOMERS ORDER BY SALARY, 10 Operations : none relation (or) Full Ri The following set operations are used to write a query to combine more than one relation, They are: Union Intersect Except UNION: * SQLUNION clause/operator is used to combine the results of two or more SELECT statements without returning any duplicate rows. : © Touse UNION, each SELECT must have the same number of columns selected, the same number of column expressions, the same data type, and have them in the same order, but they do not have to be the same length. Svotaxs_ @ The Basic synlax of UNION is as follows cer Sc, SELECT column! |, column2 | FROM table! |, table? | [WHERE condition} UNION —SELECT columnl |, column | FROM table! |, table? | [WHERE condition] BX: SELECT 1D, NAME, AMOUNT, DATE FROM CUSTOMERS LEFT JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID UNION SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS RIGHT JOIN ORDERS. ON CUSTOMERS ID © ORDERS.CUSTOMER_ID, UMN ALL Clause: * The UNION ALL operator is used to combine the results of two “T statements including duplicate rows. * The same rules that apply to UNION apply to the UNION ALL operator. Sunt: ‘+ The basic syntax of UNION ALL is as foll SELECT columal {, column2 | FROM tablet |, table? | [WHERE condition} UNION ALL. SELECT columnt |, column? | FROM table! |, table2 | [WHERE condition] E SELPCT 1D, NAME, AMOUNT, DATE FROM CUSTOMERS LEFT JOIN ORDERS ON CUSTOMERS ID = ORDERS CUSTOMER_ID UNION ALL SELECT ID, NAME, AMOUNT, DATE PROM CUSTOMERS RIGHT JOIN ORDERS ON CUSTOMERS ID = ORDERS CUSTOMER _ID;eer ORR eee eeeee eee] | ‘The SOL INTERSECT clause/operator is used to combine two SELECT statements. but returns rows only from the first SELECT statement that are identical to a row in the second SELECT statement This means INTERSECT returns only common rows returned by the two SELECT statements Just as with the UNION operator, the same rules apply when using the INTERSECT operator. MySOL does not support INTERSECT operator Syntax: The basic syntax of INTERSECT 1s as follows: SELECT column} [, column? | FROM tablet {, table2 | (WHERE condition] INTERSECT ECT column! {, column2 | FROM table! |, table2 | [WHERE condition}; SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS LEFT JOIN ORDERS ON CUSTOMERS ID = ORDERS.CUSTOMER_ID INTERSECT si 1D, NAME, AMOUNT, DATE FROM CUSTOMERS RIGHT JOIN ORDERS. ON CUSTOMERS ID = ORDERS CUS EXCEPT: The SQL EXCEPT clause/operator is used to combine two SELECT statements and returns rows from: the first SELECT statement that are not retutned by the second SELECT statement. This means EXCEPT returns only rows, which are not available in second SELECT statement Just as with the UNION operator, the same rules apply when using the EXCEPT operator. MySQL does not support EXCEPT operatorThe basic syntax of EXCEPT is as follows: SELECT column! |, column2 | FROM table! |, table2 | | WHERE condition} EXCEPT SELECT columal [, column? | FROM table1 {, table2 | [WHERE condition]; Ex SELBCT {D, NAME, AMOUNT, DATE FROM CUSTOMERS LEFT JOIN ORDERS ON CUSTOMERS ID = ORDERS.CUSTOMER_ID EXCEPT SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS RIGHT JOIN ORDERS ON CUSTOMERS ID = ORDERS.CUSTOMER_ID; SQL Operators ‘What is an Operator in SQL? © An operator is a reserved word or a character used primarily in an SQL. statement’s WHERE clause to perform operation(s), such as comparisons and arithmetic operations, '* Operators are used to specify conditions in an SQL statement and to serve as conjunctions for maltiple conditions in a statement, 1. Arithmetic operators ! 2. Comparison operators 3, Logical operators 4 Operators use to negate conditionsOperator ‘SQL Comparison Operators; Operator Description Description Addition - Adds values on either side of the operator Subtraction - Subtracts right hand operand from left hand operand, Example a+b will give 30 a-bwill give -10 Multiplication - Multiplics values. on either side of the ‘operator Division - Divides lef hand operand by right hand operand Modulus - Divides left hand operand by right hand operand and returns remainder Checks if the values of two operands are equal of not, if yes then condition becomes true. Checks if the values of two operands are equal or not, if ‘values are not equal then condition becomes true. Checks if the values of two operands are equal or not, if values are not equal then condition becomes true Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true ‘Checks ifthe value of left operand is less than the value of right operand, if yes then condition becomes true. Checks ifthe value of left operand is greater than or equal to the value of right operand, if yes then condition becomes a * b will give 200 b/awill give 2 b%awill gve0 Example (ab) is not true. (o!=b)is true (a> bp is true (a> d) is not ime (
=b) is not truera YY a = Checks ifthe value of left operand is less than oF equal (a = b) is to the value of right, operand. if yes then condition true becomes true. le ‘Checks if the value of left operand is not less than the fa !< by is value of right operand, if yes then condition becomes false ‘trues. > ‘Checks if the value of left operand is not greater than ab bs the value of right operand, if yes then condition true. becomes true. *+ The following are example illustrate the relational operators usage on tables: Bx + SELECT * FROM CUSTOMERS WHERE SALARY > $000; + SELECT * FROM CUSTOMERS WHERE SALARY = 2000; * SELECT * FROM CUSTOMERS WHERE SALARY != 2000, + SELECT * FROM CUSTOMERS WHERE SALARY >= 6500, “The OR operator is used to combine multiple conditions in.an SQL statement's WHERE clause. Ths NOT operator reverses the meaning oF the logical operator with which wis _NOT_ EXISTS. NOT BETWEEN, _NOT_IN, ete ‘operatorSQL AND and OR operators are used to combine multiple conditions to narrow data in an SQL statement. These two operators are called conjunctive operators. These operators provide a means to make multiple comparisons with different operators an the same SQI. statement AND Operator: The AND operator allows the existence of multiple conditions in an SQL statement’s WHERE clause. Sunt, ‘The basic syntax of AND operator with WHERE clause ts as follows SELECT column, column2, columaN FROM table_name WHERE {condi AND [condition2|..AND |conditionN}; E SELECT * FROM CUSTOMERS WHERE AGE >» 25 AND SALARY >= 6500. OR Operator: The OR operator is used to combine multiple conditions in an SQL statement’s WHERE clause Syntax: The basic syntax of OR operator with WHERE clause is as follows: SELEC Hohn cohemnd-colemn FROM tabbe—name WHERE feondition}}-OR—— {condition2}...OR |conditionN}s ix SELECT * FROM CUSTOMERS WHERE AGE >= 25 OR SALARY >= 6500; NOT Operator: The NOT operator reverses the meaning of the logical operator with which itis used EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator, NOTSyntax, SELECT columat, column2, ... column FROM table_name WHERENOT [condition]; EX: WHERE AGE IS NOT NULL; Operator Description BETWE EN ‘The BETWEEN operator is used to search for values that are within a set of values, given the minimum value and the maximum value ‘The EXISTS operator is used to search for the presence of a row in a specified table that meets certain criteria. ‘The IN operator is used to compare a value to a list of literal values that have been specified. 1s ‘The LIKE operator is used to compare a value to similar values using NULL —_wildeard operators. UNIQU ~The NULL operator is used to compare a value with a NULL value. E ‘The UNIQUE operator searches every row of a specified table for ‘uniqueness (no duplicates). LIKE Operator: |} SQLILIKE clause is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction with the LIKE operator: 1. The percent sign (%) 2. The underscore (_) ___1) The percent sign repeesents 2210 one. or multiple characters The underscore represents a single number or character.ve. ‘The symbols can be used in combinations Syatax: The basic syntax of % and _ 18 as follows s T FROM tab WHERE column LIKE 'XXXX%' or SELECT FROM WHERE column LIKI or CT FROM table_name or FROM table_name WHERE column LIKE 'XXXX° or SELECT FROM table_name WHERE column LIKE *_XXXX_ po RAE UE. | | | |Ex: ‘Statement WHERE SALARY LI WHERE SALARY LIK! WHERE SALARY LIKE "00%! WHERE SALARY LIKE '2_%_%! WHERE SALARY LIKE "%or' WHERE SALARY LIKE ' 2%3" WHERE SALARY LI Description Finds any values that start with s Finds any values that have sad in any position Finds any values that have 00 in the second and third positions Finds any values that start with 2 and are af least 3 characters in length Finds any values that end with r Finds any values that ha 2 in the second position and end with a3 Finds any values in a five-digit amber that start with 2 and end withoe SU NA ee a erator ‘The BETWEEN operator is used to select values within a range, Syntax: SELECT column_namefs) FROM ‘able_name WHERE column_name BETWEEN value! AND value2; ELECT * FROM Products WHERE Price BETWEEN 10 AND 20, TT BETWEEN Operator: SELECT * FROM Products WHERE Price NOT BETWEEN 10 AND 20; IN Operator: The IN operator allows you to specify multiple values in a WHERE clause Syotax SELECT column_name(s) FROM table_name WHERE column_name IN (valuel value2,...): Ex; SELECT * FROM Customers WHERE salary IN (5000, 10000); SOL Joins: * SQL Joins clause is used to combine records from two or more tables in a database. + AJOIN isa means for combining fields from two tables by using values common to each * Consider the following two tables, CUSTOMERS and ORDERS tables are as follows.|_NAME | AGE. | ADDRESS SALARY 1 Ramesh | 32} 4 | Chaitali_| 25 | Mumbai | 6500.00 | | $ | Hardik | 27 Bhopal! $500,001. 6 | Komal { 22 | MP1 4500.00 SELECT 1D, NAME, AGE, AMOUNT FROM CUSTOMERS, ORDERS. WHERE CUSTOMERS ID - ORDERS.CUSTOMER_ID, This would produce the following result: | ID [| NAME | AGE | AMOUNT | 13 Peon F257 eT ene 3000 | {3 | kaushik 4 23( 18004) 2} Khilan | 25 | 1560 3)NOTE: Join ts performed in the WHE Several operators can be used to join tables, such as <>, <#, >=, =, BETWEEN, LIKE, and NOT; they can all be used to join tables. However, the most common operator is the equal symbol, SOL Join Types: There are different types of joins available in SQL: They are: + INNER JOIN + OUTER JOIN + SELF JOIN + CARTESIAN JOIN RIO! ‘The most frequently used and important of the joins is the INNER JOIN. They are also referred to as an EQUUOIN The INNER JOIN creates a new result table by combining column values of two tables (table! and table2) based upon the join-predicate. ‘The query compares each row of tablel with each row of table? to find all pairs of rows which satisfy the join-predicate When the join-predicate is satisfied, column values for each matched pair of rows of A and B are combined into a result row. ) The basic syntax of INNER JOIN is as follows: SELECT tablel.columnl, table2.column2,.. FROM tablel INNER JOIN table2 ON tablel.common_filed = table2.common_field; Ex; SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS INNER JOIN ORDERS CUSTOMERS.ID = ORDERS CUSTOMER_ID; OUTER JOIN, C1. The Outer join can be classified into 3 types. They are Left Outer Join\ Right Outer Join Full Outer JoinLeft Outer Join: ‘The SQL. LEFT JOIN returns all rows from the left table, even if there are no matches in the right table, ‘This means that a Left join retums all the values from the left table, plus matched values from the right table or NULL in case of no matching join predicate Syntax: ‘The basic syntax of LEFT JOIN is as follows: SELECT tublel.columnt, table2.column2... FROM tablet LEFT JOIN table? ON tablel.common_filed = table2.common_fields EX: SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS LEFT JOIN ORDERS ON CUSTOMERS ID = ORDERS.CUSTOMER_ID} RIGHT JOP ‘The SQL RIGHT JOIN returns all rows from the right table, even if there are no matches in the left table. ‘This means that a right join returns all the values from the right table, plus matched values from the left table or NULL in case of no matching join predicate. Syntax: The basic syntax of RIGHT JOIN is as follows: SELECT tablel.columnt, table2.column2... FROM table! RIGHT JOIN table2 ON tablel.conimon_filed = table2.common_field; Ex: SELECT ID, NAME, AMOUNT, DATE FROM ‘CUSTOMERS, RIGHT JOIN ORDERS ON CUSTOMERS .ID = ORDERS.CUSTOMER_ID, EULLJOIN: The SQL FULL JOIN combines the results of both left and right outer joins. The joined table will contain all records from both tables, and fill in NULLS for missing matches on either side.@ Syntax: + The basic syntax of FULL JOIN is as follows: SELECT fablel.columnl, table2.column2... FROM tablet FULL JOIN table2 ON tablel.common_filed = table2.common_field; Ex; SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS FULL JOIN ORDERS ON CUSTOMERS. ID = ORDERS.CUSTOMER_ID; SELF JO! + The SQL SELF JOIN is used to join a table to it as if the table were two tables, temporarily renaming at least one table in the SQL statement, COOL ‘Syntax: + The basic syntax of SELF JOIN is as follows: SELECT a.column_name, b.column_name...FROM tablel a, tablel b WHERE a.common_filed = b.common_field; Ex: SELECT aID, b.NAME, aSALARY FROM CUSTOMERS a, CUSTOMERS b WHERE a.SALARY < b.SALARY; CARTESIAN JOL + The CARTESIAN JOIN or CROSS JOIN returns the cartesian product of the sets of records from the two or more joined tables. + Thus, it equates to an inner join where the join-condition always evaluates to ‘True or where ‘the join-condition is absent from the statement. wi ‘Syntax: + The basic syntax of CROSS JOIN is as follows: _. SELECT. tableLcolumnt, table2.colamn2.. FROM tabi table3};, Ex; SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS, ORDERS; MIEWS IN SOL: + A view is nothing more than a SQL statement that is stored itv the database with an rh associated name, eee. anette sienna. eidienseen neemA view is actually a composition of a table in the form of a predefined SQL query A view can contain all rows of a table or select rows from a table A view can be created from one or many tables which depends on the written SQL query to create a view, ‘Views, which are kind of virtual tables, allow users to do the following: * Structure data in a way that users or classes of users find natural of intuitive + Restrict access to the data such that a user can see and (sometimes) modify exactly what they need and no more. Summarize data from various tables which can be used to generate reports. + Views provide data security + Different users can view same data from different perspective in different ways at the same time. + Views cal also be used to include extra/additional information Creating Views: © Database views are created using the CREATE VIEW statement. Views can be created from a single table, multiple tables, ot another view. + To create a view, a user must have the appropriate system privilege according to the specific implementation. «The basic CREATE VIEW syntax is as follows: CREATE VIEW view_name AS SELECT columnl, column2..... FROM table_name WHERE |condition]; __Ex: _ CREATE VIEW CUSTOMERS VIEW AS SELECT name, age FROM CUSTOMERS: ‘You can query CUSTOMERS. VIEW in similar way as you query an actual table. Following is the example: SELECT * FROM CUSTOMERS VIEW,Updating a View: A view can be updated under certain conditions: TUTORIALS POINT Simply Rasy Learning : * The SELECT clause may not contain the keyword DISTINCT. * The SELECT clause may not contain summary functions. + The ECT clause may not contain set functions. ae + The SELECT clause may not contain set operators. a +The SELECT clause may not contain an ORDER BY clause , + The FROM clause may not contain multiple tables. + The WHERE clause may not contain sub queries mig + The query may not contain GROUP BY or HAVING. c NOTE: So if a view satisfies all the above mentioned rules then you can update a view. Following is an example to update the age of Ramesh: he UPDATE CUSTOMERS_VIEW SET AGE = 35 WHERE name="Ramesh’; H Deleting Rows into a View: » F a + Rows of data can be deleted from a view. The same rules that apply to the UPDATE and INSERT commands apply to the DELETE command. + Following ts an example to delete a record having AGE= 22 delete from customers_view where age = 22; + Obviously, where you have a view. you need @ way to drop the view if itis no longer needed +The syntax 1s very simple as given below ARRRARARAR DROP VIEW view_names *) i I —,* Following is an example to drop. CUSTOMERS_VIEW from CUSTOMERS table: DROP VIEW CUSTOMERS_VIEW; ~-GROUPBY: ___ © SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups. The GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause. Syatax: The GROUP BY clause must follow the conditions in the WHERE clause and must precede the ORDER BY clause if one is used. SELECT columnl, column FROM table_name WHERE [ conditions | GROUP BY columnl, column2 ORDER BY columnl, column2: select name, sum(salary) from customers group by name; ORDER BY; SQL ORDER BY clause is used to sort the data in ascending or descending order, based on ‘one or more columns. Some database sorts query results in ascending order by default. Syntax: } The basic syntax of ORDER BY clause is as follows) SELECT column-list FROM table_nameap [ORDER BY column1, column2, .. columaN] [ASC | DESC); Ex: 1. select * from customers order by name, salary: 2. select * from customers order by name desc; of HAVING Clause:_ ‘The HAVING clause enables you to specify conditions that filter which group results appear in the final results. ‘The WHERE clause places conditions an the selected columns, whereas the HAVING clause places conditions on groups created by the GROUP BY clause. Syntax: SELECT columnl, column2 FROM tablel, table2 WHERE [conditions | GROUP BY columal, column2 HAVING { conditions | ORDER BY columnt, column2; Ex: select id, name, age, address, salary from customers group by age having count(age) >= 2; AN inctions: SQL aggregate functions return a single value, calculated from values in a column. 1 Useful aggregate functions; © AVGO - Returns the average value ( COUNT( - Returns the number of rows MAX Returns the tangest value - - MIN()- Returns the smallest value SUMO - Returns the sum AVG 0 Funeti The AVG () function returns the average value of a numeric column. ! rAYGO Syntax SELECT AVG (columa_name) FROM table_name; Ex: SELECT AVG (Price) FROM Products; COUNT O Function COUNT aggregate function is used to count the number of rows in a database table. COUNT 0 Syntax: SELECT Ci {colamn_name) FROM table_name; Ex: SELECT COUNT (Price) FROM Products; MAX ( Function ‘The SQL MAX aggregate function allows us to select the highest (maximum) value for a certain column. Assannanaaseryniey ys. MAX OSvntax: SELECT MAX (column_name) FROM table_name; EX: SELECT MAX (SALARY) FROM EMP: ‘SQL MIN Function: SQL MIN function is used to find out the record with minimum value among a record set. MINO Syntax: SELECT MiN—tcohrmmnamey FROM———— ———— table_name; EX eLECT MIN (SALARY) FROM EMP;“/ Nien ad eX SOLSUM Function SOL SUM function is used to find out the sum of a field in various records, UM ntax: SELECT COUNT (column_name) FROM table_name; EX: SELECT COUNT (EID) FROM EMP, PRIMARY Key: A primary key is a field in a table which uniquely identifies each row/record in a database table. Properties Primary kev: °A primary keys must contain: 1) Unique values 2) NOT NULL values, | Atable can have only one primary key, which may consist of single or multiple fields Ifa table has a primary key defined on any field(s), then you cannot have two records having the same value of that field(s). FOREIGN Key: _ A foreign key is a key used to link two tables together. This is sometimes called a referencing key. 1 Foreign Key is a column or a combination of columns whose values match a Primary Key in a differemt table. “The relationship between 2 tables matches the Primary Key in one of the tables with a Foreign Key in the second table.Sub-Oucrics/Nested Queries in SOL; Introduction to Nested Queries :_ ‘One of the most powerful features of SQL is nested queries. A nested query is a query that has another query embedded within it; the embedded query is ‘Tale a Sub query. When writing a query, we sometimes need to express a condition that refers to a table that must itself be computed A subquery typically appears within the WHERE clause of a query. Subqueries can sometimes appear in the FROM clause or the HAVING clause. ECT, INSERT, UPDATE, and DELETE statements IN, BETWEEN etc. Subqueries can be used with the SEL along with the operators lik There are a few rules that subqueries must follow: 1. Subqueries must be enclosed within parentheses. 2. Asubquery can have only one column in the SELECT clause, unless multiple columns are in the main query for the subquery to compare its selected columns. 3. A subquery cannot be immediately enclosed in a set function. Subqueries with the SELECT Statement: Subqueries are most frequently used. with the SELEC statement. The basic syntax is as follows’ SELECT column_name |, column_name | FROM tablet |. table2 | WHERE column_name OPERATOR (SELECT columa_name . Ba [. column_name | FROM tablet |, table? | [WHERE))ne y ay ay Ex: select “from customers where id in (select id from customers where salary 4500), Subqueries with the tT Statement: Sub queries also can be used with INSERT statements. ‘The INSERT statement uses the data returned from the subquery to insert into another table, ‘The selected data in the subquery can be modified with any of the character, date or number functions Syntax SERT INTO table_name [ (columat 1. column? }) | SELECT [*Jcolumnt |, column? | FROM table! |, table2] | WHERE VALUE OPERATOR | Ex insert into customers_bkp select * from customers where id in (select id from customers) , ‘Subqueries with the UPDATE Statement: ‘The subquery can be used in conjunction with the UPDATE statement Either single or multiple columns in a table can be updated when using a subquery With the UPDATE statement, UPDATE table SET columa_name = new value | WHERE OPERATOR | VALUE ] (SELECTCOLUMN_NAME FROM TABLE_NAME) | WHERE) |; UPDATE CUSTOMERS SET SALARY * SALARY * 0.25 WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP WHERE AGE >* 27);(or) A transaction is an execution of a user program and is seen by the DBMS as a series or list of, actions ie., the actions that can be executed by a transaction includes the reading and writing of database. ‘Transaction Operations: Access to the database is accomplished in a transaction by the following two operations, read(X) : Performs the reading operation of data item X from the database. write(X) : Performs the writing operation of data item X to the database. Example: Let TI be a transaction that transfers $50 from account A to account B. This transaction can be illustrated as follows, TI: read(A); A:=A-50, write(A); read(B); B= B+ 50, write(B), Transaction Concept: ‘The concept of transaction is the foundation for concurrent execution of transaction in a DBMS and recovery from system failure in a DBMS. ‘A user writes data access/updates programs in terms of the high-level query language supported by the DBI LSM TT ee eS eee eee eats ee epee ee ae et recovery, it is convenient to regard an execution of a user program or transaction, as a series of reads and writes of database objects. To read a database object. itis first brought in to main memory from disk and then its value is copied into a program. This is done by read operation. To write a database object, in-memory, copy of the object is first modified and then written to disk. This is done by the write operation, Properties of Transaction (ACID): There are four important properties of transaction that a DBMS must ensure to maintain data in concurrent access of database and recovery from system failure in DBMS. ‘The four properties of transactions are,
You might also like
SQL Dumps
PDF
No ratings yet
SQL Dumps
35 pages
Unit Iii SQL
PDF
No ratings yet
Unit Iii SQL
35 pages
SQL (Structured Query Language)
PDF
No ratings yet
SQL (Structured Query Language)
16 pages
SQL, The Structured Query Language
PDF
No ratings yet
SQL, The Structured Query Language
60 pages
SQLServer
PDF
No ratings yet
SQLServer
27 pages
Dbms Unit 3 Part1
PDF
No ratings yet
Dbms Unit 3 Part1
47 pages
3.SQL Queries DDL
PDF
No ratings yet
3.SQL Queries DDL
61 pages
learning-material-UNIT 4
PDF
No ratings yet
learning-material-UNIT 4
12 pages
SQL-DDL and DML
PDF
No ratings yet
SQL-DDL and DML
45 pages
SQL Commands: A Data Type Defines What Kind of Value A Column Can Contain
PDF
No ratings yet
SQL Commands: A Data Type Defines What Kind of Value A Column Can Contain
12 pages
Unit 4 Bcomcardbms 2024
PDF
No ratings yet
Unit 4 Bcomcardbms 2024
34 pages
DBMS Module - 2
PDF
No ratings yet
DBMS Module - 2
155 pages
DBMS Unit-Iii
PDF
No ratings yet
DBMS Unit-Iii
35 pages
DBMS M Iv
PDF
No ratings yet
DBMS M Iv
97 pages
What Is SQL?
PDF
No ratings yet
What Is SQL?
9 pages
SQL Commands: SR - No. Command & Description
PDF
No ratings yet
SQL Commands: SR - No. Command & Description
27 pages
Unit 3 SQL
PDF
No ratings yet
Unit 3 SQL
24 pages
3.SQL Queries DDL
PDF
No ratings yet
3.SQL Queries DDL
49 pages
DBMS L1 - Lab
PDF
No ratings yet
DBMS L1 - Lab
19 pages
Sql-Structured Query Language
PDF
No ratings yet
Sql-Structured Query Language
33 pages
Database Systems Scse
PDF
No ratings yet
Database Systems Scse
80 pages
DBMS File
PDF
No ratings yet
DBMS File
11 pages
SQL Notes
PDF
No ratings yet
SQL Notes
11 pages
FDB Lecture05
PDF
No ratings yet
FDB Lecture05
43 pages
Unit 3
PDF
No ratings yet
Unit 3
18 pages
Chapter+2+ +Database+Query+Languages
PDF
No ratings yet
Chapter+2+ +Database+Query+Languages
64 pages
SQL (Structured Query Language) Is Used To Perform Operations On The Records Stored in The Database
PDF
No ratings yet
SQL (Structured Query Language) Is Used To Perform Operations On The Records Stored in The Database
35 pages
SQLNOTES
PDF
No ratings yet
SQLNOTES
10 pages
Structured Query Language
PDF
No ratings yet
Structured Query Language
21 pages
Dbms Manual II Cse
PDF
No ratings yet
Dbms Manual II Cse
74 pages
SQL Notes-1
PDF
No ratings yet
SQL Notes-1
28 pages
DBMS Module 3
PDF
No ratings yet
DBMS Module 3
53 pages
SQL Full Revision
PDF
No ratings yet
SQL Full Revision
27 pages
Dbmsss Notes
PDF
No ratings yet
Dbmsss Notes
28 pages
Chapter 6 - SQL
PDF
No ratings yet
Chapter 6 - SQL
40 pages
Structured Query Language Tutorial
PDF
No ratings yet
Structured Query Language Tutorial
9 pages
SQL Notes by Apna College
PDF
No ratings yet
SQL Notes by Apna College
29 pages
Chapter 4 Database Management-1
PDF
No ratings yet
Chapter 4 Database Management-1
125 pages
Unit 3
PDF
No ratings yet
Unit 3
27 pages
SQL v1 PDF
PDF
No ratings yet
SQL v1 PDF
42 pages
Structured Query Language
PDF
No ratings yet
Structured Query Language
48 pages
SQL (1) - 241105 - 150950
PDF
No ratings yet
SQL (1) - 241105 - 150950
91 pages
SQL & Query Optimization: Unit - Iii
PDF
No ratings yet
SQL & Query Optimization: Unit - Iii
123 pages
SQL Interview
PDF
No ratings yet
SQL Interview
84 pages
Database: TDB2073 - SPDB
PDF
No ratings yet
Database: TDB2073 - SPDB
52 pages
0.0.1 SQL
PDF
No ratings yet
0.0.1 SQL
73 pages
Unit-3 Notes
PDF
No ratings yet
Unit-3 Notes
30 pages
Structured Query Language Prepared By: Reema Agrawal Asst Professor Meri
PDF
No ratings yet
Structured Query Language Prepared By: Reema Agrawal Asst Professor Meri
22 pages
SQL Lab Manual 1
PDF
No ratings yet
SQL Lab Manual 1
12 pages
SQL Notes by Apna College
PDF
No ratings yet
SQL Notes by Apna College
29 pages
SQL - Structured Query Language
PDF
No ratings yet
SQL - Structured Query Language
24 pages
Structured Query Language
PDF
No ratings yet
Structured Query Language
37 pages
Unit-4 - Introduction To Structured Query Language
PDF
No ratings yet
Unit-4 - Introduction To Structured Query Language
96 pages
Exp1 1
PDF
No ratings yet
Exp1 1
57 pages
Lecture 03 - SQL Commands - DDL
PDF
No ratings yet
Lecture 03 - SQL Commands - DDL
31 pages
FDB Lecture05
PDF
No ratings yet
FDB Lecture05
52 pages
Introduction To SQL
PDF
100% (1)
Introduction To SQL
33 pages
SQL Integrity Constraints 1
PDF
No ratings yet
SQL Integrity Constraints 1
64 pages
DBMS Experimentdbms-4
PDF
No ratings yet
DBMS Experimentdbms-4
6 pages
DBMS Unit-1
PDF
No ratings yet
DBMS Unit-1
34 pages
DBMS Unit-5
PDF
No ratings yet
DBMS Unit-5
27 pages
DBMS Unit-2
PDF
No ratings yet
DBMS Unit-2
16 pages
DBMS Unit-4
PDF
No ratings yet
DBMS Unit-4
9 pages