CS代考计算机代写 SQL database MONASH

MONASH
INFORMATION TECHNOLOGY
Creating & Populating the Database – Data Definition Language

User requirements
Relational model theories
Oracle syntax
Draw Conceptual conceptual ERD
ERD
Draw logical level Model
Logical level Model
Write database schema
Database schema
Business rules
ERD notations
Normalisation
2
2

SQL general syntax
▪ A single statement is ended with SEMICOLON.
▪ Predefined KEYWORDs represent clauses (components) of a statement.
▪ Keywords are NOT case sensitive. ▪ Examples:
3
3

SQL Statements
▪ Data Definition Language (DDL) – Creating database structure
• CREATE TABLE, ALTER TABLE, DROP TABLE ▪ Data Manipulation Language (DML)
– Adding and Manipulating database contents (rows) • INSERT, UPDATE, DELETE
– Retrieving data from database • SELECT
▪ Data Control Language (DCL) – Set permissions on objects
• GRANT
4
4

CREATE A TABLE (DDL)
6

Common ORACLE data types
▪ Text: CHAR(size), VARCHAR2(size)
– e.g., CHAR(10), VARCHAR2(10)
– CHAR(10) → ‘apple’ = ‘apple ‘
– VARCHAR2(10) → ‘apple’ != ‘apple ‘
▪ Numbers: NUMBER(precision, scale)
–Weight NUMBER(7) or NUMBER(7,0) → Weight = 7456124 –Weight NUMBER(9,2) → Weight = 7456123.89
–Weight NUMBER(8,1) → Weight = 7456123.9
▪ Data/Time: DATE, TIMESTAMP
– DATE can store a date and time (time to seconds), stored as Julian date
– TIMESTAMP can store a date and a time (up to fractions of a second)
– TIMESTAMP WITH TIME ZONE
8
8

Column VS Table Level Constraints
CREATE TABLE STUDENT (
stu_nbr NUMBER(6) NOT NULL,
stud_lname VARCHAR2(50) NOT NULL,
stud_fname VARCHAR2(50) NOT NULL,
stu_dob DATE NOT NULL,
CONSTRAINT STUDENT_PK PRIMARY KEY (stu_nbr)
column constraints
);
table constraint
9
9

CREATE TABLE student (
stu_nbr
stu_lname
stu_fname
stu_dob
CONSTRAINT pk_student PRIMARY KEY (stu_nbr)
);
CREATE TABLE unit (
unit_code CHAR(8) NOT NULL,
unit_name VARCHAR(50) CONSTRAINT uq_unit_name UNIQUE NOT NULL , CONSTRAINT pk_unit PRIMARY KEY (unit_code)
);
NUMBER(8) VARCHAR(50) VARCHAR(50) DATE
NOT NULL, NOT NULL, NOT NULL, NOT NULL,
12 12

CREATE
TABLE enrolment (
stu_nbr
unit_code
enrol_year
enrol_semester CHAR(2)
enrol_mark NUMBER(3) ,
enrol_grade CHAR(2),
CONSTRAINT pk_enrolment PRIMARY KEY (stu_nbr, unit_code, enrol_year, enrol_semester), CONSTRAINT fk_enrolment_student FOREIGN KEY (stu_nbr) REFERENCES student (stu_nbr), CONSTRAINT fk_enrolment_unit FOREIGN KEY (unit_code) REFERENCES unit (unit_code)
);
NUMBER(8) NOT NULL, CHAR(8) NOT NULL, NUMBER(4) NOT NULL,
NOT NULL,
13 13

CREATE TABLE enrolment (
stu_nbr unit_code enrol_year enrol_semester mark
);
ALTER TABLE enrolment ADD
( CONSTRAINT fk_enrolment_student FOREIGN KEY (stu_nbr) REFERENCES student ( stu_nbr),
CONSTRAINT fk_enrolment_unit FOREIGN KEY (unit_code) REFERENCES unit (unit_code));
NUMBER(8) NOT NULL, CHAR(8) NOT NULL, NUMBER(4) NOT NULL, CHAR(2) NOT NULL, NUMBER(3),
CHAR(2),
CONSTRAINT pk_enrolment PRIMARY KEY (stu_nbr, unit_code, enrol_year, enrol_semester)
grade
14 14

Referential Integrity
▪ To ensure referential integrity, SQL defines three possible actions for FKs in relations when a deletion of a primary key occurs:
– RESTRICT (Oracle No Action basically equivalent)
• Deletion of tuples is NOT ALLOWED for those tuples in the table referred by the FK (the table containing PK) if there is corresponding tuple in the table containing the FK.
– CASCADE
• A deletion of a tuple in the table referred by the FK (the table containing PK) will result in the deletion of the corresponding tuples in the table containing the FK.
– NULLIFY
• A deletion of a tuple in the table referred by the FK (the table containing PK) will result in the update of the corresponding tuples in the table containing the FK to NULL.
15 15

Referential Constraints SQL Data Modeller
16 16

What Referential Integrity Constraint to implement?
▪ Use the model to decide on what referential integrity constraint to implement.
– Mandatory vs Optional participation.
▪ The constraints must be decided at the design phase.
17 17

▪ Used to change a tables structure.
▪ For example:
– Adding column(s).
– Removing column(s).
– Adding constraint(s).
– Removing constraint(s)
21 21

Referential Integrity Definition – Example
ALTER TABLE enrolment
DROP CONSTRAINT fk_enrolment_student;
ALTER TABLE enrolment
DROP CONSTRAINT fk_enrolment_unit;
ALTER TABLE enrolment ADD
( CONSTRAINT fk_enrolment_student FOREIGN KEY (stu_nbr) REFERENCES student ( stu_nbr) ON DELETE CASCADE,
CONSTRAINT fk_enrolment_unit FOREIGN KEY (unit_code) REFERENCES unit (unit_code) ON DELETE CASCADE
);
22 22

DELETING A TABLE
▪ Use the DROP statement. ▪ Examples:
– –
23 23

ADDING TUPLES/ROWS TO A TABLE (DML)
24

INSERT
• •
25 25

COMMIT and ROLLBACK
27 27







28 28

PUTTING THIS TO WORK
30

Leave a Reply

Your email address will not be published. Required fields are marked *