datubaze.files.wordpress.com · web viewfeb 01, 2020  · iso/iec 9075-14:2006 defines ways that...

29
1 Datoru vaicājumu valodas Datoru vaicājumu valodas (query languages) lieto lai iegūtu (izgūtu) datus un informāciju no: 1) datu bāzēm (datu bāzes vaicājumu valoda (database query languages)) – datu un faktu iegūšana; 2) informācijas sistēmām (informācijas izguves vaicājumu valodas (information retrieval query languages)) - dokumentu ar vajadzīgo informāciju iegūšana.

Upload: others

Post on 20-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

1

1

21

Datoru vaicājumu valodas

Datoru vaicājumu valodas (query languages) lieto lai iegūtu (izgūtu) datus un informāciju no:

1) datu bāzēm (datu bāzes vaicājumu valoda (database query languages)) – datu un faktu iegūšana;

2) informācijas sistēmām (informācijas izguves vaicājumu valodas (information retrieval query languages)) - dokumentu ar vajadzīgo informāciju iegūšana.

Biežāk lietotās vaicājumu valodas

1. .QL is a proprietary object-oriented query language for querying relational databases; successor of Datalog;

2. Contextual Query Language (CQL) a formal language for representing queries to information retrieval systems such as web indexes or bibliographic catalogues;

3. CQLF (CODASYL Query Language, Flat) is a query language for CODASYL-type databases;

4. Concept-Oriented Query Language (COQL) is used in the concept-oriented model (COM). It is based on a novel data modeling construct, concept, and uses such operations as projection and de-projection for multi-dimensional analysis, analytical operations and inference;

5. D is a query language for truly relational database management systems (TRDBMS);

6. DMX is a query language for Data Mining models;

7. Datalog is a query language for deductive databases;

8. Gellish English is a language that can be used for queries in Gellish English Databases, for dialogues (requests and responses) as well as for information modeling and knowledge modeling;

9. HTSQL is a query language that translates HTTP queries to SQL;

10. ISBL is a query language for PRTV, one of the earliest relational database management systems;

11. LINQ query-expressions is a way to query various data sources from .NET languages;

12. MDX is a query language for OLAP databases;

13. OQL is Object Query Language;

14. OCL (Object Constraint Language). Despite its name, OCL is also an object query language and an OMG standard;

15. OttoQL, intended for querying tables, XML, and databases;

16. Poliqarp Query Language is a special query language designed to analyze annotated text. Used in the Poliqarp search engine;

17. QUEL is a relational database access language, similar in most ways to SQL;

18. RDQL is a RDF query language;

19. SMARTS is the cheminformatics standard for a substructure search;

20. SPARQL is a query language for RDF graphs;

21. SPL is a search language for machine-generated big data, based upon Unix Piping and SQL;

22. SQL is a well known query language and Data Manipulation Language for relational databases;

23. SuprTool is a proprietary query language for SuprTool, a database access program used for accessing data in Image/SQL (formerly TurboIMAGE) and Oracle databases;

24. TMQL Topic Map Query Language is a query language for Topic Maps;

25. UnQL (Unstructured Query Language) is a functional superset of SQL, developed by the authors of SQLite and CouchDB;

26. XQuery is a query language for XML data sources;

27. XPath is a declarative language for navigating XML documents;

28. XSPARQL is an integrated query language combining XQuery with SPARQL to query both XML and RDF data sources at once.

Datu bāzes valoda

(Lietojums jeb lietojuma programma) (Savienojums) (Datu bāzes sistēma)

(Datu bāzes valoda) (Datu bāzes vadības sistēma (DBVS)) (Datu bāze (DB))

1. Datu definēšanas valoda – datu glabāšanas struktūru izveidošana.

2. Datu manipulēšanas valoda – datu ievade, koriģēšana, dzēšana.

3. Datu vaicājumu valoda – datu izgūšana.

4. Datu administrēšanas valoda jeb datu vadības valoda – lietotāju un to tiesību definēšana.

5. Transakciju vadības valoda (commit, savepoint, rollback, set transaction).

Datu bāzes valoda SQL (Structured Query Language)

1970. g. IBM firmas zinātniskais līdzstrādnieks doktors E. F. Kodds publicēja rakstu "Relāciju modelis lielām koplietošanas datu bankām"2, kurā tika definēti matemātiskie pamati relāciju datu bāzēm.

Viņa IBM firmas kolēģi Donald D. Chamberlin un Raymond F. Boyce 70. g. sākumā izstrādāja vaicājumu valodu SQUARE (Specifying Queries As Relational Expressions), kura izmantoja kopu teoriju un predikātu teoriju, lai veiktu datu atlasi no relāciju datu bāzes.

1971. g. E. F. Kods arī pats izstrādāja vaicājumu valodu Alpha relāciju datu bāzēm[footnoteRef:1]. Tās pamatidejas vēlāk tika izmantotas vaicājumu valodā QUEL. [1: Codd, E.F., "Data Base Sublanguage Founded on the Relational Calculus", Proc. 1971 ACM-SIGFIDET Workshop on Data Description, Access, and Control, San Diego.]

1974. gadā D. D. Chamberlin un R. F. Boyce publicēja rakstu “SEQUEL: A Structured English Query Language”, kura detalizēja un bagātināja SQUARE. Vēlāk SEQUEL nosaukums, izmetot patskaņus, tika reducēts uz nosaukumu SQL[footnoteRef:2], jo SEQUEL bija Hawker Siddeley avio kompānijas firmas zīme. [2: D. D. Chamberlin, R. F. Boyce, 1974, SEQUEL: A Structured English Query Language. Retrieved on May 16, 2007 fromhttp://www.almaden.ibm.com/cs/people/chamberlin/sequel-1974.pdf.]

SQL matemātisko pamatu veido relāciju algebra un kortežu relāciju rēķini (izstrādāja E. F. Kods[footnoteRef:3]). SQL valodu izstrādāja izmantošanai relāciju DB, bet vēlāk to izmantoja arī citiem datu bāzes loģiskajiem modeļiem. [3: Codd, Edgar F (June 1970). "A Relational Model of Data for Large Shared Data Banks". Communications of the ACM (Association for Computing Machinery) 13 (6): 377–87. doi:10.1145/362384.362685. Retrieved 2007-06-09.]

70. g. sākumā sākās relāciju datu bāzes vadības sistēmu izstrāde. Lielāko un nozīmīgāko projektu System/R realizēja IBM kompānija Santa Teresa laboratorijā San-Hosē pilsētā Kalifornijas štatā. 1975. gadā tika izveidots sākotnējais relāciju datu bāzes vadības sistēmas (DBVS) prototips. 1978. un 1977. gados otrā System/R realizācija izmantoja arī vaicājumu valoda SEQUEL. Sakarā ar juridiskiem apsvērumiem tika mainīts valodas nosaukums. Tagad tā saucas vaicājumu valoda SQL (Structured Query Language). 1979. gadā projekts System/R tika pabeigts, secinot ka relāciju datu bāžu izmantošana ir komerciāli perspektīva.

Ziņojumi par projekta System/R panākumiem saistīja Kalifornijas štata pilsētas Menlou Park inženieru grupas uzmanību un 1977. gadā tika izveidota kompānija Relational Software, Inc. 1979. gadā jau tika uzsāktas grupas izstrādātās relāciju DBVS Oracle piegādes pasūtītājiem. Oracle kļuva par pirmo komerciālo DBVS skaitļošanas tehnikas programmatūru. Tā par diviem gadiem apsteidza IBM pirmās komerciālās DBVS DB-2 parādīšanos programmatūras tirgū. Turpinājumā kompānija Relational Software, Inc. tika pārsaukta par Oracle, Inc. kompāniju un patreiz tā ir vadošā datu bāzes tehnoloģijas izstrādes un ieviešanas organizācija pasaulē.

Nozīmīgākie gadaskaitļi SQL valodas vēsturē

Datums

Notikums

1970

Kodds izveido relāciju datu bāzu modeli

1974

Kompānija IBM sāk projekta Sustem/R izstrādi

1974

Pirmais publicētais raksts, kurā tiek aprakstīta SEQUEL valoda

1978

Projekta System/R ekspluatācija

1979

Parādās pirmā kompānijas Oracle DBVS realizācija komerciāliem mērķiem

1981

Kompānija Relation Technolpgy izveido DBVS Ingres

1981

IBM izveido DBVS SQL/DS

1982

ANSI izveido valodas SQL standartu komisiju

1983

IBM paziņo par DBVS DB2 izveidi

1986

ANSI pieņem SQL standartu

1986

Firma Sybase izveido relāciju DBVS transakciju apstrādei

1987

ISO pieņem valodas SQL standartu

1988

Firmas Ashton-Tate un Microsoft paziņo par DBVS SQL Server izveidi OS/2 vidē

1988

IBM paziņo par DBVS BD2 otrās versijas izveidošanu

1989

Pirmā datu bāzu servera uztādīšana un SQL pamata operētājsistēmai OS/2

1989

Izveidota SQL Access Group

1991

SQL Access Group publicē savu specifikāciju

1992

Firmas Microsoft publicē protokola ODBC specifikāciju

1992

Pirmā relāciju datu bāzu serveru uzstādīšana tīkla operētājsistēmai Novell Netware

1992

ANSI pieņem standartu SQL2

1993

Pirmās programmas ar protokolu ODBC produktu piegāde

Datu bāzes valodas SQL (Structured Query Language) izmantošana

(1973. g. SEQUEL, IBM) (Datu bāzes sistēmaLietojums, lietojumprogramma, aplikācija(Java, C, Python,SQL)Datu bāzes interfeiss (JDBC, ADO, OLE DB, ODBC)Datu bāzes vadības sistēmaDatu bāzeDati un metadatiGlabājamās procedūras (stored procedures) (PL/SQL, Java, SQL)Datu bāzes administratora grupaSELECT ... FROM ...UPDATE ...INSERT INTO ...ALTER SESSION ...ALTER SYSTEM ...CREATE USER ...GRANT ...)

SQL valodas standarti

Year

Name

Alias

Comments

1986

SQL-86

SQL-87

First formalized by ANSI.

1989

SQL-89

FIPS 127-1

Minor revision that added integrity constraints, adopted as FIPS 127-1.

1992

SQL-92

SQL2, FIPS 127-2

Major revision (ISO 9075), Entry Level SQL-92 adopted as FIPS 127-2.

1999

SQL:1999

SQL3

Added regular expression matching, recursive queries (e.g. transitive closure), triggers, support for procedural and control-of-flow statements, non-scalar types (arrays), and some object-oriented features (e.g. structured types). Support for embedding SQL in Java (SQL/OLB) and vice versa (SQL/JRT).

2003

SQL:2003

Introduced XML-related features (SQL/XML), window functions, standardized sequences, and columns with auto-generated values (including identity-columns).

2006

SQL:2006

ISO/IEC 9075-14:2006 defines ways that SQL can be used with XML. It defines ways of importing and storing XML data in an SQL database, manipulating it within the database, and publishing both XML and conventional SQL-data in XML form. In addition, it lets applications integrate queries into their SQL code with XQuery.

2008

SQL:2008

Legalizes ORDER BY outside cursor definitions. Adds INSTEAD OF triggers, TRUNCATE statement, FETCH clause.

2011

SQL:2011

Adds temporal data (PERIOD FOR). Enhancements for window functions and FETCH clause.

2016

SQL:2016

Adds row pattern matching, polymorphic table functions, JSON.

SQL pamatkomandas

CREATE TABLE [.] (

[[,]] [DEFAULT

] [,...],...

[,...] [] )

INSERT INTO [(,...)]{ {VALUES ( | | NULL | DEFAULT,...)} | {} }

UPDATE SET { = | | () | NULL | DEFAULT,...}[WHERE ]

SELECT [DISTINCT] [.] | * | [AS ],...FROM | [[AS] ] [WHERE ][GROUP BY [.],... [HAVING ]][ORDER_BY | [ASC | DESC],...];

SQL valodas SELECT vaicājuma piemērs

select f.committee_level "CC Level", f.name "CC Name",

      (select kk.user_name from gl_loan.ln_cc_member kk where kk.id in

      (select ii.cc_member_id from gl_loan.ln_cc_session_member ii

      where ii.cc_session_id =a.cc_session_id and nvl(ii.deleted,0)=0

      and ii.role='FIRST') and nvl(kk.deleted,0)=0) "1st decision maker",

(select kk.user_name from gl_loan.ln_cc_member kk where kk.id in

      (select ii.cc_member_id from gl_loan.ln_cc_session_member ii

      where ii.cc_session_id =a.cc_session_id and nvl(ii.deleted,0)=0

      and ii.role='SECOND') and nvl(kk.deleted,0)=0) "2st decision maker",

a.case_name "LC Nr.",

a.manager_name "Manager",

a.customer_name "Customer name", a.customer_code "Customer code",

decode(a.customer_type, 'P','Private', 'C', 'Legal', 'X', 'Non-client(error)' ) "Customer type", e.name "Product",

(select mm.name from gl_loan.ln_classificator mm where

e.product_line=mm.code and mm.classif_type_id=15

and mm.language='ENG'

---'LAT'

and nvl(mm.inactive,0)=0)"Product line",

b.currency "Currency",

(select l.decided_value

      from gl_loan.ln_decision_row l, gl_loan.ln_product_row o

      where l.product_decision_id = cc.id and o.product_id=b.product_id

      and o.row_type_id=16 and nvl(o.deleted,0)=0 and l.product_row_id=o.id) "Interest",

to_number(b.DECIDED_AMOUNT_LOCAL) "Amount", to_number(a.customer_new_risk )"New risk",

(select l.decided_value

      from gl_loan.ln_decision_row l, gl_loan.ln_product_row o

      where l.product_decision_id = cc.id and o.product_id=b.product_id

      and o.row_type_id=24 and nvl(o.deleted,0)=0 and l.product_row_id=o.id) "Maturity",

(select mm.name from gl_loan.ln_classificator mm where

      cc.decision=mm.code and mm.classif_type_id=22

      and mm.language='ENG'

      and nvl(mm.inactive,0)=0) "Product decision",

(select mm.name from gl_loan.ln_classificator mm where

      a.final_decision=mm.code and mm.classif_type_id=22

      and mm.language='ENG'

      and nvl(mm.inactive,0)=0) "LC decision",

a.DECISION_DATE "Decision Date",

(select mm.name from gl_loan.ln_classificator mm where

a.current_status_code=mm.code and mm.classif_type_id=12

and mm.language='ENG'

and nvl(mm.inactive,0)=0) "LC status",

(select mm.name from gl_loan.ln_classificator mm where

a.finishing_event_code=mm.code and mm.classif_type_id=2

and mm.language='ENG'

and nvl(mm.inactive,0)=0) "Finishing event",

a.created_by "Creator",

(select zz.nimi  from salesman.mp_kontor zz where zz.country_id='LV'

and zz.business_unit_code in

(select pp.business_unit_code from salesman.mp_haldur pp where pp.uname not in

('ANITAB','ANITA','ANITBERZ') and pp.missing='E'

and pp.lkpv is null and pp.country_id='LV' and pp.nimi=upper (a.created_by))) "Creator branch",

replace(cc.collateral,chr(13)||chr(10),' ') "Collateral", replace(cc.comments,chr(13)||chr(10),' ')  "Comments",

DECODE(B.APPLICATION_TYPE,'N', 'New', 'C', 'Change', 'E' , 'Extend') "Appliation type"

from gl_loan.ln_loan_case a, gl_loan.ln_application_product b,

gl_loan.ln_product e,

gl_loan.ln_committee f, gl_loan.ln_cc_product_decision cc

where  a.id=b.loancase_id

and b.product_id=e.id and a.committee_id=f.id

and nvl(e.deleted,0)=0 and  nvl(a.deleted,0)=0 and nvl(b.deleted,0)=0

and e.country='LV'

and cc.application_product_id=b.id and nvl(cc.deleted,0)=0

and a.DECISION_DATE >=  '17-Apr-2006' and a.DECISION_DATE <=  '23-Apr-2006'

order by f.committee_level, f.name, a.case_name

SELECT vaicājuma pamatelementi

SELECT ...Kādas vērtības gribam iegūt (kolonu un

funkciju)? (5)

FROM ...No kādām datu tabulām? (1)

WHERE ...Kādi ir rindu meklēšanas noteikumi? (2)

GROUP by ...Kādi ir rindu grupēšanas noteikumi? (3)

HAVING ...Kādi ir rindu grupu meklēšanas noteikumi? (4)

ORDER by ... Kā sakārtot iegūtos rezultātus?

Šķirošanas norādes. (6)

Joe Celko – labākais SQL grāmatu autors

Joe Celko is an American relational database expert from Austin, Texas. He has participated on the ANSI X3H2 Database Standards Committee, and helped write the SQL-89 and SQL-92 standards. He is the author of a Morgan-Kaufmann series of books on SQL, and over 1200 published articles on SQL and other database topics. He had been a full-time statistician for several years.

He is credited with coining the term lasagna code and popularizing Michael J. Kamfonas' nested set model for trees in SQL, a taxonomy of data encoding schemes, and several other design patterns in SQL DDL and DML.

Grāmatas par SQL valodu

Datu izgūšanas varianti no datu bāzes

(Savienojums) (Datu bāzes sistēma) (SQL redaktors(komandrindas)SQL> SELECT...)

(SQL) (Datu bāzes vadības sistēma (DBVS)) (Datu bāze (DB))

(SQL redaktors(teksta redaktors + palīdzība))

(SQL)

(IDE rīks (Integrated Development Environment))

(SQL)

(SQL) (Lietojums (lietojumprogramma))

SQL vaicājumu izpilde

(Semantiskā analīze) (SQL vaicājums) (Sintaksiskā analīze) (Datu apstrādesdarbību secības optimizācija) (Statistikas iegūšana) (Datu bāze)

(Relāciju algebras darbību secība)

(Ieteikumi (hints))

(Datora datu apstrādes darbību secība )

(Analīze)

(Izpildes plāns )

(Izpilde)

SQL valodas standartu izmaiņas

1986. g. SQL kļuva par standartu Amerikas Nacionālajā Standartu institūtā (American National Standards Institution (ANSI)) un 1987. g. Starptautiskajā Standartizācijas institūtā (International Organization of Standardization (ISO)).

SQL standartos ir veikti daudzi pilnveidojumi un uzlabojumi. Ir šādas SQL standartu versijas: SQL-89 (datu integritātes noteikumu pilnveidošana), SQL-92 (jauni datu tipi, CASE konstrukcija, CHECK ierobežojumi), SQL:1999 (objektu-relāciju DB vaicājumi), SQL:2003 (XML DB struktūras, analītiskās funkcijas), SQL:2006 (XML vaicājumi), SQL:2008 (INSTEAD OF tipa trigeri, šķērstabulas (pivot tables)). Firma Microsoft ir izstrādājusi U-SQL valodu. Tas ir SQL pilnveidojums darbam ar ļoti lielām DB ("Big Data" database).[footnoteRef:4] Firma Apache Software veido jaunu vienkāršotu pierakstu SQL valodai. Ir jau izstrādātas divas realizācijas NewSQL Jdb un NewSQL S2.[footnoteRef:5] [4: https://msdn.microsoft.com/en-us/library/azure/mt591959.aspx] [5: http://newsql.sourceforge.net/]

Standarts SQL 86

1. Creating and Manipulating Table Definitions ( Create, Alter+Add, Drop )

1.1 CREATE TABLE

1.2 DROP TABLE

1.3 ALTER TABLE

1.4 CREATE [UNIQUE] INDEX

1.5 Use of indexes for performance enhancement

1.6 Use of indexes as a data integrity check

1.7 DROP INDEX

2. Updating the database ( Insert, Update+Set, Delete )

2.1 Adding rows to a table: the INSERT command

2.2 Updating rows of a table: the UPDATE/SET -command

2.3 Deleting rows from a table: the DELETE -command

3. Controlling the Execution of Commands ( Commit / Rollback Work )

3.1 AUTOCOMMIT modes

3.2 The ROLLBACK WORK command

4. Granting and Revoking User Privileges

4.1 GRANT

4.2 REVOKE

SQL-92 was the third revision of the SQL database query language. Unlike SQL-89, it was a major revision of the standard.

New Features

1) SQL Agent

2) New data types defined: DATE, TIME, TIMESTAMP, INTERVAL, BIT string, VARCHAR strings, and NATIONAL CHARACTER strings.

3) Support for additional character sets beyond the base requirement for representing SQL statements.

4) New scalar operations such as string concatenation, date and time mathematics, and conditional statements.

5) New set operations such as UNION JOIN, NATURAL JOIN, set differences, and set intersections.

6) Support for alterations of schema definitions via ALTER and DROP.

7) Bindings for C, Ada, and MUMPS.

8) New features for user privileges.

9) New integrity-checking functionality such as within a CHECK constraint.

10) New schema definitions for "Information".

11) Dynamic execution of queries (as opposed to prepared).

12) Better support for remote database access.

13) Temporary tables.

14) Transaction isolation levels.

15) New operations for changing data types on the fly via CAST.

16) Scrolling cursors.

17) Compatibility flagging for backwards and forwards compatibility with other SQL standards.

18) Call Level Interface

Standarts SQL99

New built-in data types for increased modeling power;

- Boolean

- Large objects (LOBs)

- Enhanced update capabilities

- Update/delete through unions

- Additional predicates (FOR ALL, FOR SOME, SIMILAR TO)

Triggers:

- Enhances integrity mechanism (active DBMS)

- Different triggering events: update/delete/insert

- Optional condition

- Activation time: before or after

- Multi-statement action

- Several triggers per table

Condition and multi-statement action per each row or per statement:

- Roles

- GRANT/REVOKE privileges to roles

- GRANT/REVOKE roles to users and other roles

- Recursion

- Different search strategies (depth first, breadth first)

Savepoints:

- Enhances user-controlled integrity

- Savepoint definition

- Roll back to savepoint

- Nesting

OLAP extensions: CUBE, ROLLUP, Expressions in ORDER BY, Object-relational extensions

- User-defined types

- Encapsulation

- Collection types

- Arrays

- Row types

- Like record structures in programming languages

- Type of rows in tables

- Nesting (rows with row-valued fields)

- Reference types

- Support “object identity”

- Navigational access (path expressions)

- User-defined functions

- User-defined procedures

- SQL and external procedures

- Overloading and overriding

- Subtables (table hierarchies)

The SQL:2003 standard makes minor modifications to all parts of SQL:1999 (also known as SQL3), and officially introduces a few new features such as:

1) XML-related features (SQL/XML)

2) Window functions

3) the sequence generator, which allows standardized sequences

4) two new column types: auto-generated values and identity-columns

5) the new MERGE statement

6) extensions to the CREATE TABLE statement, to allow "CREATE TABLE AS" and "CREATE TABLE LIKE"

7) removal of the poorly-implemented "BIT" and "BIT VARYING" data types

The SQL:2008 standard is split into several parts, covering the Framework, the Foundation, the Call-Level Interface, Persistent Stored Modules, Management of External Data, Object Language Bindings, Information and Definition Schemas, Routines and Types Using Java, and various "Related Specifications."

Additions to the Foundation include

1) enhanced MERGE and DIAGNOSTIC statements,

2) the TRUNCATE TABLE statement,

3) comma-separated WHEN clauses in a CASE expression,

4) INSTEAD OF database triggers

5) partitioned JOIN tables,

6) support for various XQuery regular expression/pattern-matching features, and

7) enhancements to derived column names.

SQL procedurālie paplašinājumi (programmēšanas valodas datu bāzes serverī)