• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Izmenjena prezentacija sql

Izmenjena prezentacija sql






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • Naziv baze maksimalno 128 karaktera, osim ako nije definisano log ime fajla, kada je broj karaktera jos manji.Nedozvoljeni karakteri “ ’ * / ? : \\ <> -ON Fizicko mesto gde ce fajlovi biti smesteniN oznacava da moze da se definise vise fajlova gde ce baza biti smestenaLOG ON definise fizicki prostor gde ce log fajlovi biti smesteniFilespec – definisanje fajla u kom ce biti smestena bazaName –Is the name used to reference the file in any Transact-SQL statements executed after the database is created. logical_file_name must be unique in the database and conform to the rules for identifiers. The name can be a character or Unicode constant, or a regular or delimited identifier.Filename – specificira ime fajla koji ce biti u operativnom sistemu. Naziv konkretnog fajla koji sadrzi definiciju baze. Size – specificira velicinu fajla – definiciju baze sadrzane u fajluMaxsize – maksimalna velicina koju fajl sme da dostigneFilegrowth – Specifies the growth increment of the file defined in the . The FILEGROWTH setting for a file cannot exceed the MAXSIZE setting.
  • Default vrednost za veličinu baze je MBZANIMLJIVOSTTo answer the original question of why...Back in the dev cycle for SQL Server 7.0 when the storage structure was going to change, a bunch of people were sitting in a room having a discussion of what to call these new fangled things they were going to put on the OS.  You had data files and log files.  There was also the concept of a primary data file which would always be required and contain all of the system objects.  So, after some "discussion" (we all know what happens when you say "naming conventions"), someone had the idea that the primary was a master file and in particular a master data file, so from thenceforth the default extenion was .mdf.  Then everyone took a break and looked out on the landscape and everything was good.  The second part was easy, if they had a Master Data File, then they should have a Log Data File, thus .ldf was born and has grown up into a a pretty unruly teenager at this point.    Now there was this pesky set of files that were secondary data files.  They weren't at the same status level as the mdf, so they should have a different extension.  Only the people in the room know how .ndf was settled on.  (My vote is on either a dart board or someone lost a foosball game.)In short, you see .mdf, .ldf, and .ndf simply because a group of people on the SQL Server Dev Team decided that's what the default should be.  If they would have decided that the default extensions should have been .bob (thingamabob - NOT Microsoft Bob), .doo (doohickey), and .wha (whatchamadinger), then that's what we would have for default extensions.  These are just the defaults.  This is what Management Studio and Enterprise Manager will generate when you use the GUI.  If you create a database using code, you can specify any extension that you feel like specifying.  SQL Server doesn't care if the files even have extensions.  (Homework for today - go ahead and try it and see what you get.)Ldf - Enotate allows you to add hand-written notes to a variety of file formats. LDF was originally used by Informal Software from 1998 onwards for real-time sketching and mark up, using PDAs connected to desktop PCs. It originally stood for LIP Data Format, where LIP stood for Lightweight Informal Protocol. Since then spin-off comapnies DIP and Ischian have extended the file definition to Layered Data Format. It is a real-time data, command, and embedded object definition file, with the concept of embedded overlays of data and other objects.
  • Database name – ime baze koja ce biti modifikovanaADD FILE – specificira da je fajl dodatADD LOG FILE – specificira da je log fajl dodatREMOVE FILE – Uklanja opis fajla iz baze i brise fizicki fajlMODIFY FILE – Specificira fajl koji treba modifikovatiMODIFY NAME – promena imena bazeOption_specstate_option – kontrolise pristup korisnika bazicursor_option – kontrolise opcije kursoraauto_option – kontrolise automatizovane opcijesql option – kontrolise ANSI compliance opcijerecovery option – “oporavak” baze State option – tice se dozvola koje korisnici imaju nad bazom, kao i korisnika koji imaju prava koriscenja baze
  • INSERT – dodavanje torke postojecoj tabeliBULK INSERT – dodavanje vise torki postojecoj tabeli UPDATE – izmena vrednosti kolona tabeleDELETE – brisanje torke-torki u tabeliSELECT – prikaz odabranog sadrzaja relacije u baziKontrolne (upravljačke) funkcije – GRANT – dodela prava korišćenja drugim korisnicima – korisnicima koji nisu kreirali tabelu REVOKE – oduzimanje prava korišćenja drugim korisnicimaCOMMIT – prenos dejstva transakcije na bazuROLLBACK – ponistavanje dejstva transakcije
  • BINARY LARGE OBJECT(BLOB)TIME STAMP (ako se u određenom redu ne zada vrednost za kolonu ovog tipa u kolonu se upisuje vreme kada je red dodat tabeli ili kada je poslednji put izmenjen sadržaj reda.)CHARACTER(CHAR), CHARACTER VARYING(VARCHAR), CHARACTER LARGE OBJECT(CLOB)
  • REAL zauzima 4 bajta memorije, a preciznost mu je 7 cifara.FLOAT zauzima 15 bajtova memorije, a preciznost mu je 15 cifara.U Unicode znakovnim nizovima svaki znak se sastoji od 2 bajta.Ukoliko je veliki broj null vrednosti ili je puno razlika u veličini podataka- VARCHAR.Ako se broj znakova ne razlikuje mnogo od jednog do drugog reda- CHAR.Ne koristiti NVARCHAR ili NCHAR osim ako su 16-bitni znakovni (UNICODE) podaci.
  • Indicates that the grantee will also be given the ability to grant the specified permission to other principals.The full syntax of the GRANT statement is complex. The syntax diagram above has been simplified to draw attention to its structure. Complete syntax for granting permissions on specific securables is described in the topics listed below. The REVOKE statement can be used to remove granted permissions, and the DENY statement can be used to prevent a principal from gaining a specific permission through a GRANT.Granting a permission removes DENY or REVOKE of that permission on the specified securable. If the same permission is denied at a higher scope that contains the securable, the DENY takes precedence. But revoking the granted permission at a higher scope does not take precedence. Database-level permissions are granted within the scope of the specified database. If a user needs permissions to objects in another database, create the user account in the other database, or grant the user account access to the other database, as well as the current database. Caution: A table-level DENY does not take precedence over a column-level GRANT. This inconsistency in the permissions hierarchy has been preserved for the sake of backward compatibility. It will be removed in a future release.

Izmenjena prezentacija sql Izmenjena prezentacija sql Presentation Transcript

  • SQL Vežbe V
  • CREATE DATABASE CREATE DATABASE naziv-baze-podataka [ ON < filespec > [ ,...n ] ] [ LOG ON < filespec > [ ,...n ] ] < filespec > ::= ( [ NAME = logical_file_name , ] FILENAME = os_file_name [ , SIZE = size ] [ , MAXSIZE = { max_size | UNLIMITED } ] [ , FILEGROWTH = growth_increment ] ) [ ,...n ] )
  • Tip datoteke EkstenzijaPrimer Primarna datoteka podataka .mdf Sekundarne datoteke .ndf podataka CREATE DATABASE student Transakcioni log fajl .ldf ON ( NAME = ’stud_sl_pod’, FILENAME = C:Program FilesMicrosoft SQL Server MSSQL.1 MSSQLDatastudent.mdf, SIZE = 10, MAXSIZE = 50, FILEGROWTH = 5 ) LOG ON ( NAME = stud_sl_log, FILENAME = C:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLDatastudent.ldf’, SIZE = 5MB, MAXSIZE = 25MB, FILEGROWTH = 5MB )
  • CREATE DATABASE sajt ON(NAME=sajt_pod,FILENAME=‘D:procedurasajt.mdf)LOG ON(NAME=sajt_log,FILENAME=‘D:procedurasajt.ldf)
  • ALTER DATABASE ALTER DATABASE database_name { ADD FILE < filespec > [ ,...n ] | ADD LOG FILE < filespec > [ ,...n ] | REMOVE FILE logical_file_name | MODIFY FILE < filespec > | MODIFY NAME = new_dbname | SET < optionspec > [ ,...n ] [ WITH < termination > ] } < optionspec > = < state_option > | < cursor_option > | < auto_option > | < sql_option > | < recovery_option > < state_option > = { SINGLE_USER | RESTRICTED_USER | MULTI_USER } | { OFFLINE | ONLINE } | { READ_ONLY | READ_WRITE }
  • Primer ALTER DATABASE student ADD FILE ( NAME = Stud_sl_pod2, FILENAME = C:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLDatastudent.ndf, SIZE = 5MB, MAXSIZE = UNLIMITED, FILEGROWTH = 10% )
  • DROP DATABASE DROP DATABASE database_name [ ,...n ] USE database_name
  • Naredbe SQL-a • Naredbe vezane za šemu baze podataka  CREATE, ALTER, DROP • Naredbe vezane za podatke  INSERT, BULK INSERT, UPDATE, SELECT, DELETE • Naredbe vezane za transakcije  COMMIT, ROLLBACK • Naredbe vezane za kontrolu  CALL, RETURN • Naredbe vezane za sesije  SET
  • Tabele i kolone – Osnovna svojstva • Bazna tabela – tabela čiji se podaci smeštaju u bazu podataka • Tabela pogleda – imenovani upit koji se može pozvati korišćenjem datog imena • Izvedena tabela – rezultat izvršavanja upita
  • SQL TIPOVI PODATAKA • Binarni • Logički  BIT, BIT VARYING, BLOB  BOOLEAN • Datumski • Numerički  DATE, TIME ▫ tačni  INT, SMALLINT, DEC ▫ približni  REAL, FLOAT • Tekstualni  CHAR, VARCHAR, CLOB
  • CREATE TABLE CREATE TABLE naziv-tabele ( def.-kolone-1,..., [ def.-ograničenja-na-nivou- tabele-1, ...] ) Definicija-kolone naziv-kolone tip-podatka [DEFAULT klauzula] [def.-ograničenja-na-nivou- kolone] Definicija-ograničenja-na-nivou-kolone [NOT NULL] [UNIQUE | DISTINCT] [PRIMARY KEY] [REFERENCES klauzula] [CHECK klauzula] REFERENCES naziv-tabele [(naziv-kolone-1)] CHECK (uslov) Definicija-ograničenja-na-nivou-tabele [UNIQUE|DISTINCT (naziv-kol-1, ...)] [PRIMARY KEY (naziv-kol-1, ...)] [FOREIGN KEY (naziv-kol-1, ...) REFERENCES klauzula] [CHECK klauzula]
  • Primer CREATE TABLE student (bro_dos CHAR(10) PRIMARY KEY, pre_stu VARCHAR(20) NOT NULL, ime_stu VARCHAR(20) NOT NULL, pol_stu CHAR(1) DEFAULT z CONSTRAINT stud_c_po CHECK (pol_stu IN (z,m)), dat_rod_stu SMALLDATETIME NOT NULL) CREATE TABLE upis (bro_dos CHAR(10) CONSTRAINT upis_r_bd REFERENCES student, sko_god SMALLINT CONSTRAINT upis_c_sg CHECK (sko_god BETWEEN 1970 AND 2010), god_stu SMALLINT CONSTRAINT upis_c_gs CHECK (god_stu BETWEEN 1 AND 4), nac_fin CHAR(1) CONSTRAINT upis_c_ns CHECK (nac_fin=b OR nac_fin=s), CONSTRAINT upis_pk PRIMARY KEY (bro_dos, sko_god)) CREATE TABLE ispit (bro_dos CHAR(10) CONSTRAINT ispit_r_bd REFERENCES student, sko_god SMALLINT CHECK (sko_god BETWEEN 1970 AND 2010), isp_rok SMALLINT CHECK (isp_rok BETWEEN 1 AND 13), sif_pre SMALLINT, ocena SMALLINT CHECK (ocena BETWEEN 5 AND 10 ), dat_isp SMALLDATETIME, PRIMARY KEY (bro_dos, sko_god, isp_rok, sif_pre))
  • ALTER TABLE ALTER TABLE naziv-tabele [ADD klauzula] [DROP klauzula] [ALTER klauzula] ADD klauzula ADD naziv-kolone tip-podatka [DEFAULT klauzula] [def.-ograničenja-na- nivou-kolone] [BEFORE naziv-kolone] ADD [UNIQUE|DISTINCT (naziv-kol-1, ...)] [PRIMARY KEY (naziv-kol-1, ...)] [FOREIGN KEY (naziv-kol-1, ...) REFERENCES klauzula] [CHECK klauzula] DROP klauzula DROP naziv-kolone DROP naziv-ograničenja ALTER klauzula ALTER naziv-kolone tip-podatka [DEFAULT klauzula] [def.-ogranicenja-na- nivou-kolone]
  • CREATE INDEX CREATE [UNIQUE] [CLUSTER] INDEX naziv- indeksa ON naziv-tabele (naziv-kol-1 [DESC], ...)
  • INSERT INSERT [ INTO] { table_name| view_name } [ ( column_list ) ] { VALUES ( { DEFAULT | NULL | expression } [ ,...n] ) | select_statement }
  • INSERT INTO naziv-tabele [ (naziv-kolone-1, ...) ] VALUES klauzula | SELECT iskazVALUES klauzulaVALUES (NULL | broj | "string" | konstanta, ...) konstantaUSER | DATETIME | TODAY | CURRENT | ...
  • BULK INSERT BULK INSERT naziv_tabele FROM data_file } [ WITH ( [ CHECK_CONSTRAINTS ] [ [ , ] FIELDTERMINATOR [ = field_terminator ] ] [ [ , ] FIRSTROW [ = first_row ] ] [ [ , ] LASTROW [ = last_row ] ] [ [ , ] ROWTERMINATOR [ = row_terminator ] ] ) ]
  • UPDATEUPDATE naziv-tabele SET klauzula [ WHERE uslov ]SET klauzulaSET naziv-kolone-1 = izraz | (SELECT iskaz) | NULL, ...SET (naziv-kolone-1,...) = (izraz | (SELECT iskaz) | NULL, ...)uslov[NOT] uslov-poredjenja-1 | uslov-sa-podupitom-1 [AND | OR [NOT] uslov-poredjenja-2, ...]]
  • DELETEDELETE naziv-tabele [ WHERE uslov ]
  • USLOVIuslovi-poredjenja uslovi-sa-podupitomizraz relacioni-operator izraz izraz [NOT] IN (SELECT iskaz)izraz [NOT] BETWEEN izraz [NOT] EXISTS (SELECT iskaz) AND izraz izraz rel.-op. ALL | ANY | SOMEizraz [NOT] IN (skup-vrednosti) (SELECT iskaz)naziv-kolone IS [NOT] NULLnaziv-kolone [NOT] LIKE string Relacioni operatori: <, <=, >, >=, =, <>, !=
  • SELECTSELECT klauzulaSELECT [ALL | DISTINCT | UNIQUE] izraz-1 [, izraz-2, ...] izrazi {naziv-kolone | [naziv-tabele.]* | konstanta | agregatna-funkcija |FROM klauzulaFROM [OUTER] naziv-tabele1 [,[OUTER] naziv-tabele2, ...]WHERE uslov
  • GRANTGRANT db-privilegija TO {PUBLIC | lista-korisnika}GRANT tab-privilegija ON naziv-tabele TO {PUBLIC | lista-korisnika} [WITH GRANT OPTION]GRANT EXECUTE ON naziv-procedure TO {PUBLIC | lista- korisnika} [WITH GRANT OPTION] tab-privilegije db-privilegijeALTER CONNECTDELETE RESOURCEINDEX DBAINSERTSELECT [(naziv-kolone, ...)]UPDATE [(naziv-kolone, ...)]ALL
  • Primeri za GRANTGRANT CREATE DATABASE, CREATE TABLE TO Marija, JovanGRANT SELECT (bro_dos, pre_stu, ime_stu) ON student TO public
  • REVOKEREVOKE tab-privilegija ON naziv-tabele FROM {PUBLIC | lista-korisnika}REVOKE db-privilegija FROM {PUBLIC | lista- korisnika} REVOKE { ALL | statement [ ,...n ] } FROM security_account [ ,...n ]REVOKE [ GRANT OPTION FOR ] { ALL | permission [ ,...n ] } { [ ( column [ ,...n ] ) ] ON { table | view } | ON { table | view } [ ( column [ ,...n ] ) ] | ON { stored_procedure | extended_procedure } }{ TO | FROM } security_account [ ,...n ] [ CASCADE ]
  • DENYDENY { ALL | statement [ ,...n ] } TO security_account [ ,...n ]DENY { ALL | permission [ ,...n ] } { [ ( column [ ,...n ] ) ] ON { table | view } | ON { table | view } [ ( column [ ,...n ] ) ] | ON { stored_procedure | extended_procedure } }TO security_account [ ,...n ] [ CASCADE ]DENY CREATE DATABASE, CREATE TABLE TO Marija, JovanDENY INSERT, UPDATE, DELETE ON student TO Studenti
  • TRANSAKCIJEBEGIN TRAN[SACTION] [ transaction_name ]COMMIT [ TRAN[SACTION] [ transaction_name ] ]ROLLBACK [ TRAN[SACTION] [ transaction_name ] ]