Data Definition Language (Ddl)
Total Page:16
File Type:pdf, Size:1020Kb
DATA DEFINITION LANGUAGE (DDL) CREATE CREATE SCHEMA AUTHORISATION Authentication: process the DBMS uses to verify that only registered users access the database - If using an enterprise RDBMS, you must be authenticated by the RDBMS - To be authenticated, you must log on to the RDBMS using an ID and password created by the database administrator - Every user ID is associated with a database schema Schema: a logical group of database objects that are related to each other - A schema belongs to a single user or application - A single database can hold multiple schemas that belong to different users or applications - Enforce a level of security by allowing each user to only see the tables that belong to them Syntax: CREATE SCHEMA AUTHORIZATION {creator}; - Command must be issued by the user who owns the schema o Eg. If you log on as JONES, you can only use CREATE SCHEMA AUTHORIZATION JONES; CREATE TABLE Syntax: CREATE TABLE table_name ( column1 data type [constraint], column2 data type [constraint], PRIMARY KEY(column1, column2), FOREIGN KEY(column2) REFERENCES table_name2; ); CREATE TABLE AS You can create a new table based on selected columns and rows of an existing table. The new table will copy the attribute names, data characteristics and rows of the original table. Example of creating a new table from components of another table: CREATE TABLE project AS SELECT emp_proj_code AS proj_code emp_proj_name AS proj_name emp_proj_desc AS proj_description emp_proj_date AS proj_start_date emp_proj_man AS proj_manager FROM employee; 3 CONSTRAINTS There are 2 types of constraints: - Column constraint – created with the column definition o Applies to a single column o Syntactically clearer and more meaningful o Can be expressed as a table constraint - Table constraint – created when you use the CONTRAINT keyword o Can apply to multiple columns in a table o Can be given a meaningful name and therefore modified by referencing its name o Cannot be expressed as a column constraint NOT NULL This constraint can only be a column constraint and cannot be named. It specifies that this column cannot hold NULL values. Syntax: column_name data type NOT NULL, PRIMARY KEY Using the PRIMARY KEY constraint enforces entity integrity - Ensures that each entry has a unique data value and is not null in the primary key column Syntax for column constraint: PRIMARY KEY(column_name); FOREIGN KEY Using the FOREIGN KEY constraint enforces referential integrity using ON DELETE - ON DELETE CASCADE – if you delete the primary key record, the foreign key record is also deleted - ON DELETE SET NULL – if you delete the primary key record, the foreign key is set to NULL - ON DELETE NO ACTION – you can’t delete the primary key record if a foreign key record exists (checks integrity after deletion; default if ON DELETE is not specified) - ON DELETE RESTRICT – you can’t delete the primary key record if a foreign key record exists (checks integrity before deletion; not available in Oracle) Syntax for column constraint: FOREIGN KEY(column_name) REFERENCES table_name ON DELETE CASCADE UNIQUE Specifies that the column’s data value cannot be duplicated. Syntax for column constraint: column_name data type UNQIUE, 4 Syntax for table constraint: CONSTRAINT constraint_name UNIQUE(column1, column2) DEFAULT Defines a default value for a column Example of a column constraint: emp_city VARCHAR2(255) DEFAULT ‘Melbourne’, Example of a table constraint: CONSTRAINT emp_city_dft DEFAULT ‘Melbourne’ FOR emp_city, CHECK Specifies the rules for values in the column Example of a column constraint: emp_age SMALLINT CHECK(emp_age >= 18), emp_city VARCHAR(255) CHECK(emp_city IN ‘Melbourne’, ‘Geelong’, ‘Bendigo’), Example of a table constraint: CONSTRAINT inv_chk1 CHECK(inv_date > to_date(‘01-01-2019’, ‘dd-mm-yyyy’) MULTIPLE CONTRAINTS A column can have multiple constraints Example: emp_postcode CHAR(4) DEFAULT ‘3160’ NOT NULL CHECK(emp_postcode IN ‘3160’, ‘3161’, ‘3162’), ALTER AND DROP DROP TABLE The DROP TABLE command deletes a table permanently. Syntax: DROP TABLE table_name; 5 ALTER TABLE The ALTER TABLE command is used to make changes to the table structure - It is followed by a keyword: ADD, MODIFY or DROP Syntax: ALTER TABLE table_name … … ADD AND DROP COLUMN Example of deleting a column: ALTER TABLE Employee DROP COLUMN emp_age; Example of adding a column: ALTER TABLE Employee ADD(emp_dob DATE NOT NULL); MODIFYING A COLUMN You can change a column’s data type. Example of modifying a column’s data type: ALTER TABLE Employee MODIFY(emp_code CHAR(5)); However, you can’t change a column’s data type if it’s a foreign key and it has at least one record that is not null. This ensure referential integrity so that the primary and foreign key remains the same data type. In some cases, you could still change the data type if the existing data matches the new data type. Eg. Increasing the width of an attribute from 6 digits to 9 digits. ADDING PRIMARY AND FOREIGN KEYS If you created a table based on another table (using CREATE TABLE AS), the new table won’t include the primary and foreign key constraints. You can add them, using ALTER TABLE and ADD commands. Example: ALTER TABLE project ADD PRIMARY KEY(proj_code) ADD FOREIGN KEY(proj_manager) REFERENCES employee(emp_code); 6 DATABASE THEORY CONTENTS Introduction to Databases ............................................................................................................................. 1 The importance of databases .................................................................................................................. 1 Data à Information à Knowledge ......................................................................................................... 1 File system data processing ..................................................................................................................... 1 Manual vs Computerised files systems ..................................................................................... 1 Terminology .............................................................................................................................. 2 Problems ................................................................................................................................... 2 Database system ...................................................................................................................................... 3 Database management systems .............................................................................................................. 4 Types of databases .................................................................................................................... 5 The Relational Model .................................................................................................................................... 6 Characteristics of a relational table ......................................................................................................... 6 Keys ......................................................................................................................................................... 6 Dependencies ............................................................................................................................ 6 Types of keys ............................................................................................................................. 7 Integrity rules ............................................................................................................................ 7 Relational algebra .................................................................................................................................... 8 Relational Set operators ............................................................................................................ 8 Types of Joins ............................................................................................................................ 9 The Database Life cycle ............................................................................................................................... 10 Ansi/Sparc Database Architecture ......................................................................................................... 10 Terminology at different levels .............................................................................................................. 11 Conceptual Design ....................................................................................................................................... 12 Entity relationship modelling ................................................................................................................. 12 Attributes ............................................................................................................................................... 12 Types of attributes .................................................................................................................. 12 Relationships ......................................................................................................................................... 13 ERD Notations ........................................................................................................................................ 14 Chen’s Notation ....................................................................................................................... 14 Crow’s Foot Notation .............................................................................................................