Download presentation
Presentation is loading. Please wait.
1
1 The Oracle Database System Building a Database Database Course The Hebrew University of Jerusalem
2
2 Technical Basics
3
3 Connecting to the Database At the command line prompt, write: sqlplus login/password@stud.cs In the beginning your password is the same as your login. You can change your password with the command: password To disconnect use the command: quit
4
4 Connecting to the Database
5
5 Running SQL Files Instead of typing SQL commands into the SQLPLUS terminal, you can load commands from a file Use the command @file from SQLPLUS to load the file file.sql Invoke the SQLPLUS command with the extra parameter @file to load the file at connection: sqlplus login/password@stud.cs @file
6
6 Spooling the Output Output can be placed in a file: spool myFile.out Spooling can be turned off with: spool off
7
7 Tables Creation
8
8 Creating a Table The basic format of the CREATE TABLE command is: CREATE TABLE TableName( Column1 DataType1 ColConstraint, … ColumnN DataTypeN ColConstraint, TableConstraint1, … TableConstraintM );
9
9 An Example If you issue the command describe Cars you get: Name Null? Type -------- ----- ------------ LICENSE NUMBER COLOR VARCHAR2(15) CREATE TABLE Cars( LicenseNUMBER, Color VARCHAR2(15)); Note that the definition is case insensitive
10
10 Data Types CHAR(n)String of length n (n <= 2000) VARCHAR2(n)Variable length string of size <= n (n <= 4000) DATEValid dates CLOBCharacter large object (<= 4Gb) NUMBERUp to 40 digits NUMBER(n)Number of size n NUMBER(n,m)Number of size n with m digits after decimal place
11
11 Constraints in Create Table Adding constraints to a table enables the database system to enforce data integrity. However, adding constraints also makes inserting data slower. Different types of constraints: * Not Null* Default Values * Unique * Primary Key * Foreign Key* Check Condition
12
12 Not Null Constraint CREATE TABLE Employee( SSNNUMBER NOT NULL, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1), SalaryNUMBER(5) NOT NULL, DeptNUMBER );
13
13 Default Values CREATE TABLE Employee( SSNNUMBER NOT NULL, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER );
14
14 CREATE TABLE Employee( SSNNUMBER UNIQUE NOT NULL, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER, constraint Emp_UQ UNIQUE(Fname, Lname) ); Unique Constraint The name of the constraint
15
15 Primary Key Constraint CREATE TABLE Employee( SSNNUMBER PRIMARY KEY, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER, constraint Emp_UQ UNIQUE(Fname, Lname) ); Primary Key implies NOT NULL and UNIQUE. There can only be one primary key.
16
16 Another Table CREATE TABLE Department( DeptNumNUMBER PRIMARY KEY, Name VARCHAR2(20), ManagerIdNUMBER ); Shouldn’t all department numbers in Employee appear in Department?
17
17 Foreign Key Constraint (Referential Integrity) CREATE TABLE Employee( SSNNUMBER PRIMARY KEY, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER, constraint Emp_UQ UNIQUE(Fname, Lname), FOREIGN KEY (Dept) REFERENCES Department(DeptNum) ); Must be unique
18
18 Alternative Notation CREATE TABLE Employee( SSNNUMBER PRIMARY KEY, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER REFERENCES Department(DeptNum), constraint Emp_UQ UNIQUE(Fname, Lname) );
19
19 Understanding Foreign Keys The constraint on the last table should be read as: “The field Dept in Employee is a foreign key that references the field DeptNum in Department ” Meaning: Every non-null value in the field Dept in Employee must appear in the field DeptNum in Department. What happens to Employees in department 312 when Department 312 is removed from the Department table?
20
20 Deleting a Referenced Value If nothing additional is specified, then Oracle will not allow Department 312 to be deleted if there are Employees working in this department. If the constraint is written as FOREIGN KEY (Dept) REFERENCES Department(DeptNum) ON DELETE CASCADE then Employees working in 312 will be deleted automatically from the Employee table
21
21 Cyclic Foreign Keys CREATE TABLE Department( DeptNumNUMBER PRIMARY KEY, Name VARCHAR2(20), ManagerIdNUMBER REFERENCES Employee(SSN) ); Do you see a problem in inserting data now? We should revise the Department table:
22
22 Solution to Cyclic Constraints Add one of the constraints later on (after insertion): ALTER TABLE Department ADD(FOREIGN KEY (ManagerId) REFERENCES Employee(SSN));
23
23 Check Conditions A check condition is a Boolean expression: “And”s and “Or”s of conditions of the type X > 5… On a column: it can refer only to the column On a table: it can refer only to multiple columns in the table
24
24 Check Constraints CREATE TABLE Employee( SSNNUMBER PRIMARY KEY, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’) CHECK(Gender = ‘F’ or Gender = ‘M’), SalaryNUMBER(5) NOT NULL, CHECK (Gender = ‘M’ or Salary > 10000) );
25
25 Table Alteration
26
26 Altering Tables Table definition can be altered after its creation Adding columns Changing columns’ definition Dropping columns Adding constraints And more… Use the reserved word ALTER
27
27 Altering Tables (continues) Adding a column: ALTER TABLE Employee ADD ( Mname VARCHAR2(20), Birthday DATE ); Changing columns’ definition: ALTER TABLE Emplyee Modify ( Mname VARCHAR2(10) ); Cannot be NOT NULL unless the table is empty
28
28 Altering Tables (continues) Dropping columns: ALTER TABLE Employee DROP COLUMN Mname; Dropping multiple columns: ALTER TABLE Employee DROP (Mname, Birthday); Adding constraints: ALTER TABLE Department ADD( FOREIGN KEY (ManagerId) REFERENCES Employee(SSN));
29
29 Deleting a Table To delete the table Employee : DROP TABLE Employee;
30
30 User’s Table List ORACLE may print tables that hold some general information about the tables in your database Such Tables are: Tab, Cat, User_Tables (too detailed...) To see the list of all your tables you can print: SELECT * FROM Cat; SELECT tname FROM Tab; SELECT table_name from User_Tables;
31
31 Table Data Maintenance
32
32 The Employee Table > Describe Employee Name Null? Type -------- -------- ------------ SSN NUMBER FNAME VARCHAR2(20) LNAME VARCHAR2(20) GENDER CHAR(1) SALARY NOT NULL NUMBER(5)
33
33 Inserting a Row To insert a row into the Employee table: INSERT INTO Employee(SSN, Fname, Lname, Salary) VALUES(121, ‘Sara’, ‘Cohen’,10000); The remaining columns get default values (or NULL) Order is not important
34
34 Some More Details… The fields needn’t be specified if values are specified for all columns and in the order defined by the table Example: INSERT INTO Employee VALUES(121, ‘Sara’, ‘Cohen’, `F’, 10000);
35
35 Deleting Rows General format: DELETE FROM Table WHERE Cond; Deletes all rows satisfying Cond from Table For example, to remove the employee with SSN 121 from the Employee table: DELETE FROM Employee WHERE SSN = 121;
36
36 Deleting Rows (continues) To remove all male employees having a salary greater than 15000 shekels: DELETE FROM Employee WHERE Gender = ‘M’ AND Salary > 15000; We will later discuss WHERE clauses… Case sensitive
37
37 Updating Rows (continues) We can update fields of rows in a table General format: UPDATE Table SET Field1=value1,,,FieldN=valueN WHERE Cond Now we can reduce salaries instead of firing employees: UPDATE Employee SET Salary = 15000 WHERE Gender = ‘M’ AND Salary > 15000;
38
38 The ORACLE Bulk Loader A tool that provides easy insertion of large amounts of rows into tables. The idea: the field values of the rows are kept in a file, the format of which is defined by us. For example, it can automatically load 3 employees from the file myEmployees.dat that contains the following lines: Sara|Cohen|121 Benny|Kimelfeld|134 Yaron|Kanza|156
39
39 The Control File The control file is the direct input of the loader A simple control file: LOAD DATA INFILE [APPEND] INTO TABLE FIELDS TERMINATED BY ' ‘ ( )
40
40 The Control File (continues) : The name of the data file : The name of the table into which the data will be loaded (appended if APPEND is specified, or else the table must be empty) : A string that separates two field values of a row The attributes are separated by commas and enclosed in parentheses
41
41 The Control File (continues) As an example, the following control file loads the employees from myEmployees.dat: LOAD DATA INFILE myEmployees.dat INTO TABLE Employees FIELDS TERMINATED BY '|' (Fname, Lname, SSN) The attributes that are unspecified will be set to NULL
42
42 The Data File The Bulk Loader considers every single line to represent one row in the table Even an empty line! (which will usually result in an error) Spaces are not ignored in the data file! thus the rows ‘sara| cohen|121’ and ‘sara|cohen|121’ define different functionalities The NULL value is implied by the NULL keyword or the empty string
43
43 The Data File (continues) The control and the data files can be combined into one.ctl file using the following format: LOAD DATA INFILE * INTO TABLE Employees FIELDS TERMINATED BY '|' (Fname, Lname, SSN) BEGINDATA Sara|Cohen|121 Benny|Kimelfeld|134 Yaron|Kanza|156
44
44 The Bulk Invocation To invoke the bulk loader, issue the following command directly from the Unix shell: sqlldr control= log= bad= All fields are optional File names that have no extension are automatically extended (by.dat,.log or.bad) Erroneous lines in the data file are ignored and written into badFile, and any other relevant information is written into logFile.
45
45 Bulk Loader Important Remarks Before using the Bulk Loader, make sure your personal ORACLE environment is properly set up The tables you fill using the Bulk Loader should be created prior to the loader invocation Before invoking the Bulk Loader you have to make sure that NO SQLPLUS SESSIONS ARE OPENNED!
46
46 Table Printing and Formatting
47
47 Format Example Consider the following table print:
48
48 Format Example (continues) Absolutely not readable! Solution: we write some commands in a format file and we get:
49
49 Printing a Table To print a table use the sql command: SELECT * FROM table_name;
50
50 Add a Title We can add a title to a table printing page using the command: TTITLE ‘title’
51
51 Change a Column’s Heading We can change a column’s heading using the command : COLUMN ‘col_name’ HEADING ‘heading’
52
52 Define a Column Size We can define the size of a column: COLUMN ‘col_name’ format ‘format’ Format Examples: a18 : an ASCII text of size 18 90.99 : a number with 4 digits, a decimal point, minus sign (for negative numbers) and 0 before the dot for numbers smaller than 1 We can ask ORACLE to cut words to fit the defined space using the command: COLUMN ‘col_name’ truncated
53
53 Define a Column Size
54
54 The Whole Picture
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.