Operating Systems

Operating Systems

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering ECOM 4113: DataBase Lab Lab # 4 Data Definition Language (DDL) Eng. Haneen El-Masry November, 2014 2 DataBase Lab Objective To be familiar with SQL Language especially with Data Definition Language (DDL). Relational Database (RDB) Relational Database means data stored in relations. Relation is essentially a mathematical term for table. Each table is a named collection of rows (tuples). Each row of a given table has the same set of named Columns (attributes), and each column is of a specific data type. Database Keys A key is a logical way to access a record in a table. There are many types of key in RDB: Candidate Key A candidate key is any field, or combination of fields, that uniquely identifies a record. The field/s of the candidate key must contain unique values, and cannot contain a null value. Primary Key (PK) A primary key is the candidate key that has been chosen to identify unique records in a particular table. Foreign Key (FK) A relationship between two tables is created by creating a common field to the two tables. The common field must be a primary key to the one table. 3 DataBase Lab SQL Language SQL is presently expanded as Structured Query Language. Originally, SQL was called SEQUEL. SQL is now the standard language for commercial relational DBMSs. SQL Statements SQL has insensitive statements for data definitions, queries, and updates. SQL commands are grouped into major categories depending on their functionality: Data Definition Language (DDL): These SQL commands are used for creating, modifying, and dropping the structure of database objects. It also define keys, specify links between tables, and impose constraints between tables. The commands are ALTER, ANALYZE, AUDIT, COMMENT, CREATE, DROP, FLASHBACK, GRANT, PURGE, RENAME, REVOKE and TRUNCATE. Data Manipulation Language (DML): These SQL commands are used for storing, retrieving, modifying, and deleting data. These Data Manipulation Language commands are CALL, DELETE, EXPLAIN, INSERT, LOCK TABLE, MERGE, SELECT and UPDATE. Transaction Control Language (TCL): These SQL commands are used for managing changes affecting the data. These commands are COMMIT, ROLLBACK, and SAVEPOINT. In this lab, we will concentrate on DDL. Schema Creating To create a new schema in default XE database, we have to create a new user and the Oracle creates a schema for that user with the same name. 4 DataBase Lab To create a new user, you have to follow the next steps: 1- Connect to XE database by SYSTEM user. 2- Expand the connection and R-click on other users >> Create User. 5 DataBase Lab 3- User tab >> enter user name and password and choose the default and temporary table spaces. Tablespaces are the logical locations where data base objects are stored. These tablespaces are typically included in the XE database by default. The USERS and TEMP tablespaces are generally assigned to users that generate examples and demonstrations. 6 DataBase Lab 4- System Privileges tab >> Grant the necessary privileges for the user. Granted Privileges: Create session: to be able to connect to XE database by the new user. Create table/ view/ procedure/ synonym/ sequence/ trigger/ type/materialized view: to be able to create tables, views, procedures, synonyms, sequences, user defined data types and materialized views respectively inside the new schema. Unlimited tablespace: to permit a user to use an unlimited amount of any tablespace in the database. Alter session: to set or modify any of the conditions or parameters that affect your connection to the database. 7 DataBase Lab 5- Apply. 6- You can create a new user by SQL commands. Create User Create User user_name identified by password Default tablespace “tablespace” Temporary tablespace “tablespace”; Priviliges Grant Privilige To user_name ; On the worksheet of SYSTEM connection, write the following statements and execute them. 8 DataBase Lab 7- After user creation, Connect to XE database by the new user. 8- Now you can issue SQL statements for the new schema. 9 DataBase Lab Table Creating Create Table Create Table tablename ( Col.1 DataType [col. Default] [InLine Constraints], Col.2 DataType [col. Default] [InLine Constraints], .... [Out Of Line Constraints] ); DATA TYPES NUMERIC TYPES Name Description Range Number [(p[, s])] User-specified precision, scale. The precision is the number of digits to both sides of the decimal point. The positive scale is the count of decimal The precision p can range digits in the fractional part, to the right of from 1 to 38. the decimal point. If the scale of a value to be stored is greater The scale s can range from than the declared scale of the column, the -84 to 127. system will round the value to the specified number of fractional digits. A NUMBER value requires When the scale is positive and the number from 1 to 22 bytes. of digits to the left of the decimal point exceeds (p-s), an error is raised. Number(p) = Number(p,0) s=0. Number: specifies the maximum range and precision for an Oracle number. Integer Number (32) -2 ^31 – (2 ^31)-1 INT 10 DataBase Lab Float(p) A subtype of the NUMBER data type having precision p. A FLOAT value is represented internally as A FLOAT value requires from NUMBER. 1 to 22 bytes. The precision p can range from 1 to 126 binary digits. To convert from binary to decimal precision, multiply p by 0.30103. BINARY_FLOAT BINARY_FLOAT is a 32-bit, single-precision 15 decimal digit. floating-point number data type. Each BINARY_FLOAT value requires 4 bytes. Binary_Double BINARY_DOUBLE is a 64-bit, double-precision Each BINARY_DOUBLE value floating-point number data type. requires 8 bytes. CHARACTER TYPES Definition Description Notes char A single character. Char A set of characters exactly n characters in To specify a value to (n[byte|char]) length, padded with spaces. character data types, If you attempt to store a string that is too it is placed between long, an error will be generated. single quotation NCHAR[(n)] A fixed length Unicode character string marks (‘), and it is a having maximum length n characters. case sensitive, e.g., Default and minimum n is 1 character. ‘Hello’. VARCHAR2(n A Variable length character string having [byte|char]) maximum length n bytes or characters. Use concatenation You must specify size for VARCHAR2. operator (||) to Minimum size is 1 byte or 1 character. concatenate two NVARCHAR2(n) A Variable length Unicode character string strings, e.g. ‘abc’ || having maximum length n characters. ‘def’ results ‘abcdef’. You must specify size for NVARCHAR2. BINARY DATA TYPES Definition Description RAW(n) Raw binary data of max length n bytes. You must specify size for a RAW value. LONG RAW Raw binary data of variable length up to 2 gigabytes. 11 DataBase Lab DATE/TIME DATA TYPES Name Description Notes Example Date Date Only. To specify a value to Date types, you can: Its components are YEAR, Place the value between MONTH, and DAY. single quotation marks ‘31-MAY-02’ The default format is: in DD-MON-YY/YYYY ’31-MAY-2002’ format. Date ‘2002-05-31’ DD-MON-YY. Use Date keyword and Date ’02-05-31’ YY/YYYY-MM-DD SYSDATE: special value Format. that is current system date. Includes the DATE and timestamp TIME fields, with a blank To specify a value for ’31-MAY-02 [(p)] space between two fields. Timestamps DataType, you 10:30:56’ can: ’31-MAY-02 The Default format is: Place the value in (‘) marks 10:30:56 PM’ DD-MON-YY HH.MM.SS.p in DD-MON-YY AM/PM HH:MM:SS.p [AM/PM] Timestamp ‘2002- [+/-HH:MM/time zone] 05-02 10:30:56.25’ Format. Timestamp Timestamp with time . Hour from (1-12). ’31-MAY-03 [(p)] with zone displacement value. +/-HH:MM/time zone 10:30:56 AM time zone for timestamp with time +08:00’ The Default format is: zone data type. DD-MON-YY HH.MM.SS.p . AM/PM required for '04-Jul-05 4:5:6 PM AM/PM +/- HH:MM/time timestamp with time ASIA/JERUSALEM' zone zone data type. Timestamp ‘2002- . If you don’t specify 05-02 15:30:56.36 - AM/PM for timestamp 07:00’ and timestamp with Timestamp ‘2002- local time zone data 05-02 15:30:56.36 types the default is AM. ASIA/JERUSALEM' 12 DataBase Lab Timestamp Timestamp with the local Use Timestamp literal and ’31-MAY-14 [(p)] with time zone. YYYY-MM-DD HH:MM:SS.p 10:30:56’ local time [+/- HH:MM/time zone] ’31-MAY-14 zone format The Default format is: 10:30:56 PM’ . Hour from (0-23) DD-MON-YY HH.MM.SS.p . +/- HH:MM/time zone AM/PM for timestamp with time zone data type. Timestamp ’2013- 05-06 10:30:56’ P: is an optional fractional seconds precision, it can be specified timestamp, and interval types. P can be from 0 to 9. The default p is 6. If the number of digits of a value to be stored is greater than the declared P, the system will round the value to the specified P. INTERVAL Stores a period of time in To specify a value for this Interval ‘1-2’ year YEAR [(p)] years and months. data type: to month. TO MONTH P: is the number of digits Interval ‘YY-MM’ year to month. in the YEAR field. P can from 0 to 9. The default p is 2. 13 DataBase Lab INTERVAL Stores a period of time in To specify a value for this Interval ‘3 12:4:5’ DAY [(D)] days, hours, minutes, and data type: Day to Second TO seconds.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    27 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us