Recipes/Menus

3. SQL. Changing the state of a DB with SQL. Learn to make SQL queries. Access data from a DB using SQL Structure of an SQL query

Description
3. SQL Learn to make SQL queries Access data from a DB using SQL Structure of an SQL query Changing the state of a DB with SQL insert data change and remove data Luento 4 - sivu 1 SQL Query Language SQL
Categories
Published
of 23
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
3. SQL Learn to make SQL queries Access data from a DB using SQL Structure of an SQL query Changing the state of a DB with SQL insert data change and remove data Luento 4 - sivu 1 SQL Query Language SQL (Structured Query Language) Most popular query and management language for RDBMS Various dialects ANSI (American National Standads Institute) SQL, eli SQL1 SQL2 vuoden 1992 standardi, laajennettu SQL1:stä SQL 99 (SQL3) viimeisin SQL 2003 laajennettu SQL2:sta (mm. rekursiolla, laukaisimilla ja olioilla) Käytännössä olio-relationaalisen mallin mukainen Ohjelmistotuottajien SQL-murteet tyypillisesti SQL2:n laajennuksia omilla ja SQL3:n piirteillä Luento 4 - sivu 2 3.2 SQL Queries Practically, each SQL queries contains keywords SELECT, FROM ja WHERE in the form: SELECT list of attributes FROM list of relations WHERE condition FROM part lists the relation(s) for applying the query WHERE part contins the conditions which the chosen tuples must satisfy. SELECT part defines the schema for the resulting relation meaning, which attributes are included (* denotes all) Luento 4 - sivu 3 Example DB The following queries are based on the DB below Movie(title, year, length, incolor, studioname, producerc#) StarsIn(movieTitle, movieyear, starname) MovieStar(name, address, gender, birthdate) MovieExec(name, address, cer#, networth) Studio(name, address, presc#) Luento 4 - sivu 4 Example Target table Movie(title, year, length, incolor, studioname, producerc#) query the movies produced by the studio Disney at SELECT * FROM Movie WHERE studioname = Disney and year = 1990; The reulting relation could be: title year length incolor studioname producerc# Pretty Woman true Disney 999 Luento 4 - sivu 5 SELECT Resulting relation can have different names for the attributes than the originating ones. Let us change the previous query to the following: SELECT title AS name, length AS duration FROM Movie WHERE studioname = Disney and year = 1990; jolloin kyselyn tulosrelaatio: name duration Pretty Woman 119 Luento 4 - sivu 6 Example SELECT part may contain mathematical formulas. Let us change the previous query to express the length in hours instead of minutes: SELECT title AS name, length * AS lengthinhours; The resulting relation is: name lengthinhours Pretty Woman Luento 4 - sivu 7 Duplicates In SQL the resulting relations are not sets but they are bags. This means that the same tuple may occur several times. If the duplicates need to be removed from the results the SELECT part of a query must contain keyword DISTINCT. For example the relation: Movie (title, year, length, incolor, studioname, producerc#) query: SELECT title FROM Movie; may produce doubles, since title is not the primary key. The following query removes the duplicates:. SELECT DISTINCT title FROM Movie; Luento 4 - sivu 8 FROM A query in SQL may refer to several relations by giving the names of the refered relations in the FROM part. The other parts of the query can then refer to the attributes of these relations. Example: We want to retrieve the producer of Star Wars. Now, the query has to be targeted to relations Movie( title, year, length, incolor, studioname, producerc#) and MovieExec(name, address, cert#, networth), where the attributes producerc# ja cer# are the certification numbers identifying the producer. Luento 4 - sivu 9 Queries that are Targered to Several Relations The query could be executed as two queries 1. The certificate number (producerc#) of the producer from relation Movie. 2. Retrieving the corresponding person from the MovieExec relation. These two steps can be given in a single query: SELECT name FROM Movie, MovieExec WHERE title= Star Wars AND producerc# =cert#; Luento 4 - sivu 10 WHERE WHERE part may contain the following relational operators-osa voi sisältää seuraavia vertailuoperaattoreita: =, , , , =, ja =. The values being compared can be constants or attributes of relations defined in the FROM part. Arithmetical operators can be used: ( +, -, *, /). Example: (year ) * (year ) 100 is true for years Luento 4 - sivu 11 Example The names of the movies made after 1970 or are shorter that 90 minutes made by MGM. SELECT title FROM Movie WHERE (year 1970 OR length 90) AND studioname = MGM ; Luento 4 - sivu 12 String Comparison SQL makes it possible compare certain similarities ofstrings with the operator LIKE LIKE expression is of the form: s LIKE p where s on a string and p is a pattern. The pattern can contain special characters : % and _ % denotes that a substring of s (of any length) can contain any characters. _ denotes a one-character substring of any character. The expression s LIKE p is true, if p matches to s Luento 4 - sivu 13 String Comparison Example SELECT title FROM Movie WHERE title LIKE Star ; retrieves the movies whose name consists of nine characters so, that the first four are Star following a space and the last four can be any characters. Example. SELECT title FROM Movie WHERE title LIKE % `s % ; retrieves all movies which are in the genetic foem. Luento 4 - sivu 14 Sorting the Resukting Relation The tuples of the resulting relation can be defined with one or more attributes: The order is declared by adding order qualifier to the SELECT part ORDER BY list of attributes The order is ascending (ASC) by default Luento 4 - sivu 15 Example Query SELECT * FROM Movie WHERE studioname = Disney AND year = 1990 ORDER BY length, title; results in a relation of movies ordered from the shortes to the longest one and if the movies are equally length they appear in the alphabetical order. Luento 4 - sivu 16 Example 2 SELECT title, year, length, incolor, studioname, producerc# FROM Movie WHERE studioname = Disney AND year = 1990 ORDER BY 3, 1 DESC; Luento 4 - sivu 17 Aggregation Operators Calculate a scalar from a column of a relation The operators are: 1. SUM 2. AVG 3. MIN 4. MAX 5. COUNT the number of values, or the number of different values, if DISTINCT is used Esim. The following query prints the average salary of the producers: SELECT AVG(netWorth) FROM MovieExec; Luento 4 - sivu 18 Aggregation Operators If * is used as a parameter to COUNT operator, then the query results in the number of rows in the relation: Query SELECT COUNT(*) FROM MovieExec; SELECT COUNT(DISTINCT name) FROM MovieExec; outputs the number of different names the relation Luento 4 - sivu 19 Grouping By using GROUP BY part, the aggregate operators can be targeted to groups of values of a column. Now, the query outputs a single row for each group. Example query SELECT studioname, SUM(length) FROM Movie GROUP BY studioname; outputs the overall length of all movies for each studio. The resulting relation has as many rows as there are studios with different name in the studioname relation. Luento 4 - sivu 20 Example Grouping by producers Movie( title, year, length, incolor, studioname, producerc#) MovieExec(name, address, cert#, networth) SELECT name, SUM(length) FROM MovieExec, Movie WHERE producerc# = cer# GROUP BY name ORDER BY 2 DESC; Luento 4 - sivu 21 Insert, Delete and Update Modification operators change the stste of a database. The operators are: 1. INSERT adding one or more rows to a relation 2. DELETE removing one or more rows from a relation 3. UPDATE updating one or more rows in a relation Luento 4 - sivu 22 Defining Schema with SQL Schema is defined with CREATE TABLE Example. CREATE TABLE MovieStar ( name CHAR(30), address VARCHAR(255), gender CHAR(1), birthdate DATE ); Removing a table: Command DROP TABLE removes relation R Luento 4 - sivu 23
Search
Similar documents
View more...
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks