Sunteți pe pagina 1din 8

1.

CREATE AND MANIPULATE VARIOUS DB OBJECTS FOR A TABLE DATA DEFINITION LANGUAGE
1. Oracle Internal Data Types following data types. Character Char Used when a fixed length character string is required. Can store alphanumeric values. Column length can vary between 1 2000 bytes. If the user enters a value shorter than the specified length then the database would blank-pad to the fixed length. Varchar2 Supports a variable length character string. Can store alphanumeric values. Column length can vary between 1 4000 bytes. Using varchar2 disk space can be saved when compared to char. Consider a column assigned with varchar2 data type of size 30 bytes, if the user enters 10 bytes of character, then the column length in that row would only be 10 bytes and not 30 bytes. Long Used to store variable character length. Maximum size is 2 GB. Only one column in a table can have long data type, which should not contain unique of primary key constraint. The column cannot be indexed. Procedures or stored procedures cannot accept long data type as arguments. Number Can store positive numbers, negative numbers, zeroes, fixed point numbers, floating point numbers with a precision of 38. Column_name number { p = 38, s= 0 } Column_name number (p) { fixed point } Column_name number (p, s) { floating point } Where p is the precision which refers to the total number of digits, it varies between 138, s is the scale which refers to number of digits to the right of the decimal point. Date Used to store date and time in a table. Used to define the columns in a table. Oracle supports the

Oracle makes use of its own format to store date in a fixed length of 7 bytes each for century, month, day, year, hour, minute, and second. Default date data type is dd-mon-yy. To view systems date and time, use the SQL function sysdate. Raw (size) Used to store byte oriented data like binary data or byte strings and the maximum size is 255 bytes. Only storage and retrieval of data are possible. Raw data type can be indexed. Long raw Used to store binary data of variable length. Maximum size is 2 GB. All limitations faced by long data type also holds good for long raw data type. LOB Otherwise known as Large Object data types. Can store unstructured information such as sound clips, video files, etc., upto 4 gigabytes in size. To manipulate LOBs the DBMS_LOB package can be used. LOBs can be either external or internal depending on their location with regard to the database. Internal LOB Stored in the database tablespace. The changes to the values of the internal LOB during transaction can be committed or rolled back. Changes to the internal LOBs can be done by using SQL DML or through a series of API calls from the Oracle Call Interface (OCI). The different internal LOBs are: CLOB Stores character objects with single byte characters. Cannot contain character sets of varying widths. A table can have multiple columns with its data type as CLOB. BLOB Can store large binary objects such as graphics, video clips and sound clips. A table can have multiple columns with its data type as BLOB. External LOB Also referred to as BFILE. Stored in operating system files outside the database tablespace. May be stored in CD-ROMs, PhotoCDs or hard disks, etc. Does not participate in transactions. BFILE A BFILE column stores file pointers to LOBs managed by file systems external to the database.

A BFILE column may contain filenames for photos stored on a CD-ROM. type. These new data types can be used in tables and other database objects. Syntax: create or replace type <type name> as object (column1 data type, column2 data type, ) / Example: create or replace type address as object (add1 number (5), add2 varchar2 (10), add3 varchar2 (5), add4 number (7)) / Output: Type created. 2. CREATE TABLE COMMAND A table is a unit of storage which holds data in the form of rows and columns. In a table, A unique column name should be specified. Proper data type along with its width should be specified. Not null condition can be included when needed, by default it is Null. User defined data types Use the create or replace command to create user-defined data

Syntax: create table <table name> (column definition1, column definition 2, ); Example: create table student (regno number (11), name varchar2 (25), address varchar2(30), dept varchar2 (3)); Output: Table created.

3. ALTER TABLE COMMAND This command is used to add a new column, modify the existing column definition, include or drop integrity constraints. Syntax: alter table <table name> modify (column definition ); alter table <table name> add (column definition );

Example: I. alter table student modify (name varchar2 (30)); Output: Table altered. II. alter table student add (comments long); Output: Table altered.

4. TRUNCATE TABLE COMMAND The truncate command deletes all rows from the table. Only the structure of the table remains. Syntax: Truncate table <table name>; Example: Truncate table student; Output: Table truncated. 5. DESC COMMAND This command will display the structure of the table. Syntax: Desc <table name>; Example: Desc student; Output: Name Null? Type ---------------------------------- -------- ---------------------------REGNO NUMBER(11) NAME VARCHAR2(30) DEPT VARCHAR2(3) COMMENTS LONG

6. DROP TABLE COMMAND The drop table command is used to delete the table permanently from the database. Syntax: Drop table <table name>; Example: Drop table student; Output: Table dropped.

DATA MANIPULATION COMMANDS


1. INSERT COMMAND Used to add one or more rows to a table. While using this command the values are separated by commas and the data types char and date are enclosed in apostrophes. The values must be entered in the same order as they are defined in the table.

Syntax: To insert all column values for each row SQL> Insert into <table_name> values (a list of data values); To insert specific column values for each row SQL> Insert into <table_name> (col_names) values (list of values);

Examples: 1. The example given below inserts a record in Student table. The structure of Student table is: Name Null? Type ---------------------------------- -------- ---------------------------REGNO NUMBER(11) NAME VARCHAR2(25) DOB DATE ADDR VARCHAR2(25) DEPT VARCHAR2(3) SQL> Insert into student values (42207621001, 'Ananthi', to_date ('20/04/1987', 'dd/mm/yyyy'), 'Kanchipuram', 'MCA'); Output: 1 row created. 2. To insert more than one record using a single insert command. SQL> Insert into student values (&RegNo, '&Name', '&DOB', '&addr, '&Dept'); Output: Enter value for regno: 42207621035 Enter value for name: Prince Enter value for DOB: 20-APR-87 Enter value for addr: Arakkonam Enter value for dept: MCA old 1: insert into student values (&RegNo, '&Name', '&DOB', '&addr' new 1: insert into student values (42207621035, 'Prince', '20-APR-87', kancheepuram', ' 1 row created. The above command however inserts only one row. To insert another row, type a / in the SQL prompt. 5

3. To insert a record with values for specific columns. SQL> insert into student (RegNo, Name, Dept) values (42207621050, 'Subramani', 'MCA'); Output: 1 row created. 4. The following example copies all the rows from Student1 table to Student table, provided a table named Student exists having the same structure as Student1. SQL> insert into Student (select * from Student1); Output: 3 rows created. 2. SELECT COMMAND Used to perform a query. The query is a request for information. Syntax: Select column_name from table_name where conditions [order by column_name ]; Examples: 1. The following example selects all the columns from the table Student for which RegNo less than or equal to 42207621035 ordered by RegNo in descending fashion. The default ordering is ascending. SQL> Select * from Student where RegNo <= 42207621035 Order By RegNo Desc; Output: REGNO NAME ---------- ------------------------ADDR -------------------------------------------------------------------------------DEP --4.2208E+10 Prince 'Arakkonam', MCA 4.2208E+10 Ananthi 'Kanchipuram', MCA REGNO NAME ---------- ------------------------ADDR -------------------------------------------------------------------------------DEP --6

To select only specific columns, specify the column names instead of * in the select command. 2. The example given below creates a new table Student1 from the existing table Student along with its records. SQL> Create table Student1 as select * from Student; Output: Table created. 3. The following command displays the records from department table whose dcode begins with M. SQL> select * from department where dcode like 'M%'; Output: DCO -----MBA MCA DNAME -----------------------------Management Studies Computer Applications HODNAME -----------------------------Dr. Padmini Dr. Pandurangan

4. The command given below lists all the columns from department table whose dcode is three letters long and the middle letter is C. SQL> select * from department where dcode like '_C_'; Output: DCO DNAME ------ -----------------------------MCA Computer Applications 3. UPDATE COMMAND Examples: 1. The following command updates the values for Name and Dept columns in the Student table for RegNo 42207621050. SQL> update Student set Name = 'Abbas', Dept = 'MBA' where RegNo = 42207621050; Output: 1 row updated. Used to alter the column values in a table. Specific rows could be updated based on a specific condition. The where clause and the set clause can also include queries. HODNAME -----------------------------Dr. Pandurangan

Syntax: Update table_name set field = value, where condition;

The result of the above command can be verified by selecting rows from the Student table. 4. DELETE COMMAND Used to delete one or more rows to a table. Syntax: delete from <table_name> where conditions;

Examples: 1. The following example will delete a row from Student table whose RegNo is 42207621050. SQL> delete from Student where RegNo = 42207621050; Output: 1 row deleted.