0% found this document useful (0 votes)
14 views16 pages

Y23 Week4 A

Uploaded by

msci.2303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Y23 Week4 A

Uploaded by

msci.2303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

More Constraints…FK

• A FOREIGN KEY is a key used to link two tables together.


• A FOREIGN KEY is an attribute (attributes) in one table that
refers to the PRIMARY KEY in another table.
• The table containing the foreign key(referencing table) is
called the child table, and the table containing the primary
key (referenced table) is called the parent table.
• The FOREIGN KEY constraint prevents invalid data from being
inserted into the foreign key column.
• The FOREIGN KEY constraint is used to prevent actions that
would destroy links between the tables.
Foreign Key
CREATE TABLE part(Part_no NUMBER PRIMARY KEY,
Part_desc VARCHAR2(200) NOT NULL );

CREATE TABLE item (Item_no NUMBER,


Part_no NUMBER,
Item_desc varchar2(200) NOT NULL,
CONSTRAINT fk_item_part FOREIGN KEY (part_no) REFERENCES PART (part_no),
CONSTRAINT pk_item PRIMARY KEY (item_no, part_no) );

If item table existed 


ALTER TABLE Item ADD [CONSTRAINT CONSTRAINT_NAME] FOREIGN KEY
(Part_no) REFERENCES Part(Part_no);

To drop a constraint 
ALTER TABLE ITEM DROP CONSTRAINT FK_Item_Part;
CHECK Constraint

• The CHECK constraint is used to limit the value range that can
be placed in a column.
• If you define a CHECK constraint on a single column it allows
only certain values for this column.
Create a CHECK constraint on the "Age" column when the
"Persons" table is created. The CHECK constraint must ensure
that the age of a person must be older than 18.
CREATE TABLE Persons (ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>18) );

ALTER TABLE persons


ADD CONSTRAINT age_chec CHECK (age > 18);
Check constraint
• To define a CHECK constraint on multiple columns

CREATE TABLE Persons ( ID int NOT NULL,


LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255),
CONSTRAINT CHK_Person CHECK (Age>=18 AND City=‘Panaji') );

insert into persons values(1,'abc','pqr',18,'panaji');  what is


the outcome?
Default constraint
• The DEFAULT constraint is used to provide a default value for a column.
• The default value will be added to all new records IF no other value is specified.

CREATE TABLE myOrders ( ID int NOT NULL,


OrderNumber int NOT NULL,
OrderDate date DEFAULT sysdate );

If table already exists 


ALTER TABLE myorders MODIFY orderdate DEFAULT sysdate;

Dropping a default? 
Cannot drop default value instead we set it to NULL.
Alter table tablename modify columnname default NULL;

Note: Default is not a ‘constraint’ as such, hence it does not show up in user_constraints
SELECT * FROM user_cons_columns WHERE table_name = 'MYORDERS';
select data_default from user_tab_columns where table_name = 'MYORDERS';
Data Dictionary
– Central to every Oracle database
– Describes the database and its objects
– Contains read-only tables and views
– Stored in the SYSTEM tablespace
– Owned by the user SYS
– Maintained by the Oracle server
– Accessed with SELECT

The data dictionary provides information about:


– Logical and physical database structures; Definitions and space allocations of
objects
– Integrity constraints; Users; Roles; Privileges; Auditing Information

Oracle server modifies it when a DDL statement is executed.


– Users and DBAs use it as a read-only reference for information about the
database.
7
Data Dictionary View Categories
– Three sets of static views
– Distinguished by their scope:
 DBA: In all the schemas
 ALL: What the user can access
 USER: In the user’s schema

DBA_xxx All of the objects in the database

ALL_xxx Objects accessible by the current user

USER_xxx Objects owned by the current user

10
Data Dictionary
• User-friendly views are available
– e.g. USER_TABLES
• One row for every table in your schema

• More than 1200 data dictionary views


– Static views, e.g. USER_TABLES
– Dynamic views, e.g. V$NLS_PARAMETERS - monitor ongoing activity

Select table_name from dict;


Select * from dict where table_name=‘ ‘;
desc tabs;
desc user_tables;

11
Querying the data dictionary…contd
Prefix Scope
USER User’s view (what is in the user’s schema)
ALL Expanded user’s view (what the user can access)
DBA Database administrator’s view (what is in all users’ schemas)
“USER_TABLES”, is a view on the data dictionary
contains one row for every table owned by you (i.e. created in your schema).
DESC user_tables ^C
SELECT table_name, num_rows from user_tables;
SELECT owner, object_name, object_type FROM ALL_OBJECTS;
Select distinct owner from all_objects;
SELECT owner, table_name FROM all_tables WHERE owner=‘yma'

select table_name from dict where table_name like 'V$N%';


There are hundreds of data dictionary tables and views.
The views are built on underlying base tables such as SYS.TAB$.
However, the underlying tables such as SYS.TAB$ are difficult to use,
not documented and column names are not intuitive.

Execute the DESCRIBE command on V$NLS_PARAMETERS


12
Querying Oracle Data Dictionary
SQL> desc dict;
SQL>select table_name from dict;
SQL>desc all_all_tables;
SQL>select table_name from all_all_tables where owner=‘YMA’;
SQL>select table_name from user_tables where owner=‘YMA’; error

SQL> select table-name,comments from dictionary


where table-name like ‘DBA_TAB%’

SQL>desc dba_tables;
SQL> select owner,table_name from dba_tables (user_tables;all_tables)
where tablespace_name =‘USERS’

SQL> desc all_users  column is username (see the data)


SQL>desc user_tables
SQL>select table_name from user_tables; [tabs]

14
DML – Familiarize with Data Dictionary
 Find the name of the table that contains tablespace information for users;
SQL>select * from dict where table_name like 'USER_T%';
Find the names of tablespaces in the user tablespace table.
SQL> select tablespace_name from USER_TABLESPACES;

Can you create a tablespace? Try the create tablespace command

CREATE TABLESPACE userdata


DATAFILE 'data01.dbf' SIZE 50M;

Find all the schema objects you have created


List only tables created by you
List only objects created by you from the all_objects table
List view tables starting with V$
 Look for the table V$NLS_Parameters

List all constraints with table name on which they are created - created by you
Oracle - SQL
• Checking the Date Format
select value from V$NLS_Parameters
where parameter=‘NLS_DATE_FORMAT’

• Setting the Date Format


alter session set NLS_DATE_FORMAT=‘dd-mm-yy’

• Setting Locality
alter session set NLS_TERRITORY=‘India’

select value from V$NLS_Parameters


where parameter=‘NLS_CURRENCY’
Date format
• To view current date and its format
SELECT sysdate FROM dual;

• To change the standard date format to ‘YYYY-MM-DD’


ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD';

• Format output using TO_CHAR() function


SELECT TO_CHAR( SYSDATE, 'Month DD, YYYY' ) FROM dual;
select to_char(sysdate,'YEAR DAY, MONTH') FROM DUAL;

SELECT TO_CHAR( '2-10-1966', 'Month DD, YYYY' ) FROM dual;


error

SELECT TO_CHAR( to_date('2-10-1966'), 'Month DD, YYYY' ) FROM dual;


SELECT TO_CHAR( to_date('22-10-1966'), 'Month DD, YYYY' ) FROM dual;
Date format
SELECT TO_CHAR( to_date('22-10-66'), 'Month DD, YYYY' )
FROM dual;
 Changes year to current century

SELECT value FROM V$NLS_PARAMETERS WHERE


parameter = 'NLS_DATE_LANGUAGE';

ALTER SESSION SET NLS_DATE_LANGUAGE = 'FRENCH';


select sysdate from dual;

select to_char(sysdate,'MONTH DD, YYYY') FROM DUAL;


Date datatype-
• Convert string to date
Because Oracle uses an internal format for storing the DATE data, you often
have to convert a string to a date value before storing it in the date column.

• Change format of date


ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-Month-DD';

• Using date ‘yyyy-mm-dd’


select date '2020-09-22' from dual;

insert into tablename values(date '2020-09-22');

insert into tablename values(to_date('09-22-2020','mm-dd-yyyy'));


insert into adate values('b',to_date('14-10-2020','mm-dd-yyyy')); invalid
month
SQL Developer
• The right side of the SQL Developer window has tabs and panes for objects that you select or
open.
• Icons and other controls under the Data tab provide various options

Freeze View (the pin) keeps that object’s tab and information in the window when
you click another object in the Connections navigator; a separate tab and display
are created for that other object.
Refresh .
Insert Row
Delete Selected Row(s) .
Commit Changes
Rollback Change
Sort
Filter predicate
Actions suitable to the table

You might also like