CREATE, DROP,ALTER INSERT, UPDATE, DELETE

Slides:



Advertisements
Similar presentations
Using DDL Statements to Create and Manage Tables
Advertisements

9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Baze de Date Anca Ion Baze de Date -Limbajul SQL-Introducere- Universitatea din Craiova, Facultatea de Automatica, Calculatoare si Electronica.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Oracle8 - The Complete Reference. Koch & Loney1 Chapter 17 Creating, Dropping, and Altering Tables and Views Presented by Victor Matos.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Objectives After completing this lesson, you should be able to do the following: Categorize the main database objects Review the table structure List.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
10-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Copyright  Oracle Corporation, All rights reserved. 4 Introduction.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
Copyright © 2004, Oracle. All rights reserved. Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes ORACLE.
DDL and Views. Database Objects Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage;
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
Including Constraints
Subinterogări multiple
Subinterogari.
Oracle Academy Lead Adjunct
Oracle Academy Conversion Functions.
Funcţii Excel definite de utilizator (FDU) în VBA
Instrumente CASE Curs nr. 7.
Căutarea şi regăsirea informaţiei.
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Căutarea şi regăsirea informaţiei.
Paxos Made Simple Autor: Puşcaş Radu George
DATABASE PROGRAMMING.
Date Semistructurate, C12. Oracle / OR (2) Date Semistructurate,
CONVERSII INTRE SISTEME DE NUMERATIE
5-2-7 : تقسيم الصفوف في الجدول إلي مجموعات :-
WebSite Social Tema 2 WebSite Social.
Tipuri structurate Tipul tablou
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Modificarea structurii unei tabele
Curs 6: Introducere în programarea SAS
Funcții C/C++ continuare
Forms (Formulare).
Funcții NULL.
Harti de imagini, Cadre, Stiluri
Tabele WEB.
(SQL) Manipulating Data
SQL Statements SELECT INSERT UPDATE DELETE CREATE ALTER DROP RENAME
Administrare Oracle 9i Suport de curs
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Presentation transcript:

CREATE, DROP,ALTER INSERT, UPDATE, DELETE Laboratorul 6 CREATE, DROP,ALTER INSERT, UPDATE, DELETE

Crearea unui tabel Reguli pentru numele noului tabel: începe cu o literă maxim 30 de caractere conţine doar caractere din setul A-Z, a-z, 0-9, _, #, $ este unic în schema curentă nu este un cuvânt rezervat

Copierea unui tabel CREATE TABLE nume_tabel [(coloana1, coloana2, …)] AS subquery; Exemplu: CREATE TABLE copy_emp AS (SELECT * FROM emp);

CREATE TABLE ... Sintaxa de bază: CREATE TABLE nume_tabel (nume_coloana1 tip_date1 [DEFAULT expr1] [,nume_coloana2 tip_date2 [DEFAULT expr2] …])

Tipuri de date Şiruri de caractere: Numere: Date calendaristice: CHAR – maximum 2000 VARCHAR2 – maximum 4000 CLOB – maximum 128 terabytes Numere: NUMBER Date calendaristice: DATE, TIMESTAMP Format binar: RAW, BLOB

Restricţii de integritate Formulate la nivelul coloanei: NOT NULL, cheie primară, cheie unică, cheie externă, CHECK Formulate la nivelul tabelei (după definirea ultimei coloane): cheie primară, cheie unică, cheie externă, CHECK

Restricţii de integritate Reguli: orice restricţie are asociat un nume (explicit sau implicit); Dacă folosim CONSTRAINT atunci numele trebuie dat explicit; Cheile multiple pot fi definite doar la nivelul tabelei; NOT NULL doar la nivelul coloanei.

NOT NULL ... nume_coloana tip_date NOT NULL ... ... nume_coloana tip_date nume_restrictie NOT NULL ... ... nume_coloana tip_date CONSTRAINT nume_restrictie NOT NULL ...

UNIQUE ... nume_coloana tip_date UNIQUE ... ... nume_coloana tip_date nume_restrictie UNIQUE ... ... nume_coloana tip_date CONSTRAINT nume_restrictie UNIQUE ... ... ,CONSTRAINT nume_restrictie UNIQUE (nume_coloana1 [,nume_coloana1 …]) ...

PRIMARY KEY ... nume_coloana tip_date PRIMARY KEY ... ... nume_coloana tip_date nume_restrictie PRIMARY KEY... ... nume_coloana tip_date CONSTRAINT nume_restrictie PRIMARY KEY ... ... ,CONSTRAINT nume_restrictie PRIMARY KEY (nume_coloana1 [,nume_coloana1 …]) ...

FOREIGN KEY ... nume_coloana1 tip_date CONSTRAINT nume_restrictie REFERENCES nume_tabel_parinte (nume_coloana2) [ON DELETE CASCADE|SET NULL] ... ... ,CONSTRAINT nume_restrictie FOREIGN KEY (nume_colana1[,nume_coloana2 …]) REFERENCES nume_tabel_parinte (nume_coloana3 [,nume_coloana4…]) [ON DELETE CASCADE|SET NULL] ... În tabela părinte trebuie să fie definită o restricţie de tip UNIQUE sau PRIMARY KEY

CHECK ... nume_coloana tip_date CONSTRAINT nume_restrictie CHECK (expr_logica) ... ... ,CONSTRAINT nume_restrictie CHECK (expr_logica) ... Expresia nu poate conţine coloane din alte tabele sau următoarele elemente: SYSDATE, USER, CURRVAL, NEXTVAL, LEVEL, ROWNUM, UID, USERENV.

Modificarea restricţiilor ALTER TABLE nume_tabel ADD CONSTRAINT nume_restrictie tip_restrictie (coalana); ALTER TABLE nume_tabel MODIFY (nume_coalana CONSTRAINT nume_restrictie NOT NULL); ALTER TABLE nume_tabel DROP CONSTRAINT nume_restrictie [CASCADE]; ALTER TABLE nume_tabel DISABLE|ENABLE CONSTRAINT nume_restrictie [CASCADE];

ALTER TABLE adăugare: modificare: ALTER TABLE nume_tabel ADD (nume_coloana1 tip_date1 [DEFAUL expr1] [,nume_coloana2 tip_date2 [DEFAUL expr2] …]) modificare: MODIFY (nume_coloana1 tip_date1 [DEFAUL expr1] schimbarea tipului de date sau micşorarea dimensiunii este posibilă doar dacă nu sunt valori pe coloana respectivă sau dacă nu sunt înregistrări.

ALTER TABLE ştergere: ALTER TABLE nume_tabel DROP COLUMN nume_coloana; doar câte o singură coloană; datele nu pot fi recuperate; nu pot fi şterse toate coloanele unui tabel; poate fi costisitoare. SET UNUSED (nume_coloana); DROP UNUSED COLUMNS;

DROP TABLE DROP TABLE nume_tabel; poate fi reversibilă; FLASHBACK TABLE nume_tabel TO BEFORE DROP; SELECT original_name, droptime FROM user_recyclebin;

RENAME, TRUNCATE RENAME nume_tabel TO nume_nou; TRUNCATE TABLE nume_tabel; şterge toate întregistrările; nu este reversibilă (spre deosebire de DELETE).

Comanda INSERT - sintaxa INSERT INTO nume_tabel [(coloana1, coloana2, …)] VALUES (expresie1, expresie2, …) SELECT …

Comanda INSERT INSERT INTO copy_emp Nume empno ename job mgr hiredate sal comm deptno Tip number(4) char(10) char(9) date number(7,2) number(2) NOT NULL X INSERT INTO copy_emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) VALUES (7788, ’Bill Gates’, ’Doorman’, 7839, ’13-JAN-1986’, 450, 10, 30) - în lista de valori poate fi filosit NULL pentru coloanele ce permit acest lucru

Comanda INSERT INSERT INTO copy_emp (empno, ename, job, sal, deptno) Nume empno ename job mgr hiredate sal comm deptno Tip number(4) char(10) char(9) date number(7,2) number(2) NOT NULL X INSERT INTO copy_emp (empno, ename, job, sal, deptno) VALUES (7788, ’Bill Gates’, ’Doorman’,450, 30) - nu pot lipsi colane ce nu permit NULL pentru că acestea primesc inplicit valoarea NULL

Comanda INSERT INSERT INTO copy_emp Nume empno ename job mgr hiredate sal comm deptno Tip number(4) char(10) char(9) date number(7,2) number(2) NOT NULL X INSERT INTO copy_emp VALUES (7788, ’Bill Gates’, ’Doorman’, 7839, ’13/7/1986’, 450, 10, 30)

Comanda INSERT INSERT INTO copy_emp Nume empno ename job mgr hiredate sal comm deptno Tip number(4) char(10) char(9) date number(7,2) number(2) NOT NULL X INSERT INTO copy_emp VALUES (7788, ’Bill Gates’, ’Doorman’, 450, 30) ORA-00947: not enough values

Comanda INSERT INSERT INTO copy_emp Nume empno ename job mgr hiredate sal comm deptno Tip number(4) char(10) char(9) date number(7,2) number(2) NOT NULL X INSERT INTO copy_emp (empno, ename, job, hiredate, sal, comm) VALUES (7788, ’Bill Gates’, ’Doorman’, SYSDATE, 450, 450*0.1) - clauza VALUES poate conţine valori speciale, expresii sau funcţii

Comanda INSERT INSERT INTO copy_emp (empno, ename, job, hiredate, sal, comm) SELECT empno+1, ename, NULL, 0, comm*2 FROM emp WHERE deptno=20 lista de coloane a comenzii INSERT poate lipsi dacă subinterogarea returnează aceleaşi coloane(ca număr, tip de date şi ordine) cu cele dint tabela destinaţie

Comanda UPDATE UPDATE nume_tabel SET coloana1 = expresie1 [WHERE conditie] - expresiei poate fi o subinterogare ce returneaza o singura valoare (o singura coloana si o singura inregistrare), si apare intre paranteze

Comanda UPDATE UPDATE nume_tabel SET (coloana1, coloana2) = (SELECT expresie1, expresie2 FROM …) [WHERE conditie] - subinterogare returneaza câte o singura înregistrare pentru fiecare înregistrare ce trebuie actualizată în tabela destinaţie

Comanda DELETE DELETE [FROM] nume_tabel [WHERE conditie]

Flashback Query sistemul păstrează informaţii despre modificările făcute asupra datelor (valorile vechi pentru UPDATE, respectiv toată înregistrarea pentru DELETE) pentru aprox. 15 min. SELECT versions_starttime, versions_endttime, ... FROM nume_tabel VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE