Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pavel Titenkov © SQL. Pavel Titenkov © Общий обзор англ. Structured Query Language — «язык структурированных запросов»англ. Это наиболее часто используемый.

Similar presentations


Presentation on theme: "Pavel Titenkov © SQL. Pavel Titenkov © Общий обзор англ. Structured Query Language — «язык структурированных запросов»англ. Это наиболее часто используемый."— Presentation transcript:

1 Pavel Titenkov © SQL

2 Pavel Titenkov © Общий обзор англ. Structured Query Language — «язык структурированных запросов»англ. Это наиболее часто используемый язык для работы с БДЭто наиболее часто используемый язык для работы с БД SQL работает с множеством языков 4glSQL работает с множеством языков 4gl SQL

3 Pavel Titenkov © Используется для: Data ManipulationData Manipulation Data DefinitionData Definition Data AdministrationData Administration All are expressed as an SQL statement or command.All are expressed as an SQL statement or command. SQL

4 Pavel Titenkov © SQL Requirements SQL Must be embedded in a programming languageSQL Must be embedded in a programming language SQL is a free form language so there is no limit to the the number of words per line or fixed line break.SQL is a free form language so there is no limit to the the number of words per line or fixed line break. Syntax statements, words or phrases are always in lower case; keywords are in uppercase.Syntax statements, words or phrases are always in lower case; keywords are in uppercase. SQL Not all versions are case sensitive!

5 Pavel Titenkov © SQL is a Relational Database каждый элемент таблицы — один элемент данных все ячейки в столбце таблицы однородные, то есть все элементы в столбце имеют одинаковый тип (числовой, символьный и т. д.) каждый столбец имеет уникальное имя одинаковые строки в таблице отсутствуют порядок следования строк и столбцов может быть произвольным Реляционная модель ориентирована на организацию данных в виде двумерных таблиц. Каждая реляционная таблица представляет собой двумерный массив и обладает следующими свойствами:

6 Pavel Titenkov © Design Операторы SQL делятся на: операторы определения данных (Data Definition Language, DDL)DDL операторы манипуляции данными (Data Manipulation Language, DML)DML операторы определения доступа к данным (Data Control Language, DCL)DCL SQL

7 Pavel Titenkov © Rows describe the Occurrence of an Entity Table Design SQL NameAddress Jane Doe123 Main Street John Smith456 Second Street Mary Poe789 Third Ave Columns describe one characteristic of the entity

8 Pavel Titenkov © Data Retrieval (Queries) Queries search the database, fetch info, and display it. This is done using the keywordQueries search the database, fetch info, and display it. This is done using the keyword SELECT SELECT * FROM publishers pub_idpub_nameaddressstate 0736New Age Books1 1 st StreetMA 0987Binnet & Hardley2 2 nd StreetDC 1120Algodata Infosys3 3 rd StreetCA The * Operator asks for every column in the table.The * Operator asks for every column in the table.

9 Pavel Titenkov © Data Retrieval (Queries) Queries can be more specific with a few more linesQueries can be more specific with a few more lines pub_idpub_nameaddressstate 0736New Age Books1 1 st StreetMA 0987Binnet & Hardley2 2 nd StreetDC 1120Algodata Infosys3 3 rd StreetCA Only publishers in CA are displayedOnly publishers in CA are displayed SELECT * from publishers where state = ‘CA’

10 Pavel Titenkov © Data Input Putting data into a table is accomplished using the keywordPutting data into a table is accomplished using the keyword INSERT pub_idpub_nameaddressstate 0736New Age Books1 1 st StreetMA 0987Binnet & Hardley2 2 nd StreetDC 1120Algodata Infosys3 3 rd StreetCA Table is updated with new informationTable is updated with new information INSERT INTO publishers VALUES (‘0010’, ‘pragmatics’, ‘4 4th Ln’, ‘chicago’, ‘il’) pub_idpub_nameaddressstate 0010Pragmatics4 4 th LnIL 0736New Age Books1 1 st StreetMA 0987Binnet & Hardley2 2 nd StreetDC 1120Algodata Infosys3 3 rd StreetCA Keyword Variable

11 Pavel Titenkov © pub_idpub_nameaddressstate 0010Pragmatics4 4 th LnIL 0736New Age Books1 1 st StreetMA 0987Binnet & Hardley2 2 nd StreetDC 1120Algodata Infosys3 3 rd StreetCA Types of Tables User Tables: contain information that is the database management systemUser Tables: contain information that is the database management system System Tables: contain the database description, kept up to date by DBMS itselfSystem Tables: contain the database description, kept up to date by DBMS itself There are two types of tables which make up a relational database in SQL RelationTable TupleRow AttributeColumn

12 Pavel Titenkov © Using SQL SQL statements can be embedded into a program (cgi or perl script, Visual Basic, MS Access) OR OR SQL Database SQL statements can be entered directly at the command prompt of the SQL software being used (such as mySQL)

13 Pavel Titenkov © Using SQL To begin, you must first CREATE a database using the following SQL statement: CREATE DATABASE database_name Depending on the version of SQL being used the following statement is needed to begin using the database: USE database_name

14 Pavel Titenkov © Using SQL To create a table in the current database, use the CREATE TABLE keyword CREATE TABLE authors (auth_id int(9) not null, auth_name char(40) not null) auth_idauth_name (9 digit int)(40 char string)

15 Pavel Titenkov © Using SQL To insert data in the current table, use the keyword INSERT INTO auth_idauth_name Then issue the statement SELECT * FROM authors INSERT INTO authors values(‘000000001’, ‘John Smith’) 000000001John Smith

16 Pavel Titenkov © Using SQL SELECT auth_name, auth_city FROM publishers auth_idauth_nameauth_cityauth_state 123456789Jane DoeDearbornMI 000000001John SmithTaylorMI auth_nameauth_city Jane DoeDearborn John SmithTaylor If you only want to display the author’s name and city from the following table:

17 Pavel Titenkov © Using SQL DELETE from authors WHERE auth_name=‘John Smith’ auth_idauth_nameauth_cityauth_state 123456789Jane DoeDearbornMI 000000001John SmithTaylorMI To delete data from a table, use the DELETE statement:

18 Pavel Titenkov © Using SQL UPDATE authors SET auth_name=‘hello’ auth_idauth_nameauth_cityauth_state 123456789Jane DoeDearbornMI 000000001John SmithTaylorMI To Update information in a database use the UPDATE keyword Hello Sets all auth_name fields to hello

19 Pavel Titenkov © Using SQL ALTER TABLE authors ADD birth_date datetime null auth_idauth_nameauth_cityauth_state 123456789Jane DoeDearbornMI 000000001John SmithTaylorMI To change a table in a database use ALTER TABLE. ADD adds a characteristic. ADD puts a new column in the table called birth_date birth_date.. TypeInitializer

20 Pavel Titenkov © Using SQL ALTER TABLE authors DROP birth_date auth_idauth_nameauth_cityauth_state 123456789Jane DoeDearbornMI 000000001John SmithTaylorMI To delete a column or row, use the keyword DROP DROP removed the birth_date characteristic from the table auth_state..

21 Pavel Titenkov © Using SQL DROP DATABASE authors auth_idauth_nameauth_cityauth_state 123456789Jane DoeDearbornMI 000000001John SmithTaylorMI The DROP statement is also used to delete an entire database. DROP removed the database and returned the memory to system

22 Pavel Titenkov © Conclusion SQL is a versatile language that can integrate with numerous 4GL languages and applications SQL simplifies data manipulation by reducing the amount of code required. More reliable than creating a database using files with linked-list implementation

23 Pavel Titenkov © References “The Practical SQL Handbook”, Third Edition, Bowman.


Download ppt "Pavel Titenkov © SQL. Pavel Titenkov © Общий обзор англ. Structured Query Language — «язык структурированных запросов»англ. Это наиболее часто используемый."

Similar presentations


Ads by Google