Private Institute of Aga 2018 NETWORK DATABASE LECTURER NIYAZ M. SALIH
Data Definition Language (DDL): String data Types: Data Types CHAR(size) NCHAR(size) VARCHAR2(size) Description A fixed-length character string data with a length of size, where the size between 1 and 2000. A fixed-length Unicode character string data with a length of size, where the size between 1 and 1000. A variable-length character string data with a length of size, where the size between 1 and 4000. NVARCHAR2(size) A variable-length Unicode character string data with a length of size, where the size between 1 and 2000. LONG Variable-length character string up to 2 gigabytes Numeric Data Types Data Types NUMBER (p, s) INTEGER DECIMAL (p, s) FLOAT (p) Description Can stores zero, positive, and negative fixed and floating-point numbers Will only store whole (positive, and negative) numbers, such as 5, 93. A DOUBLE stored as a string, allowing for a fixed decimal point. A small number with a floating decimal point. 2
NUMBER (precision, scale), DECIMAL (precision, scale). Precision: is the digits before the decimal number. Scale: is the digits after the decimal number. For example, NUMBER (7, 2) is a number that has 5 digits before the decimal and 2 digits after the decimal. For example, DECIMAL (3, 1) is a number that has 2 digits before the decimal and 1 digit after the decimal. Date Data Types Data Types Date Description Date and Time values to the nearest second between January 1, 4712 B.C., and December 31, 9999 A.D. Difference between CHAR and NCHAR CHAR 1 Can store only non-unicode string data. 2 It just needs 1 byte to store a character. 3 The storage size is equal to the size defined while declaring the field. NCHAR Can store Unicode string data. It needs 2 bytes to store a character. The storage size is equal to double the size defined while declaring the field. 4 Can store 2000 characters. Can store only 1000 characters. 3
The SQL CREATE TABLE Statement The CREATE TABLE statement is used to create a new table in a database. Syntax CREATE TABLE Aga ( ID NUMBER(2), St_Name VARCHAR2(14), ST_Dept VARCHAR2(13) ); DESC Aga The SQL DROP TABLE Statement The DROP TABLE statement is used to drop an existing table in a database. DROP TABLE Aga; 4
SQL ALTER TABLE Statement The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. The ALTER TABLE statement is also used to add and drop various constraints on an existing table. ALTER TABLE - ADD Column To add a column in a table, use the following syntax: Alter Table Aga Add DateOfBirth Date; ALTER TABLE - DROP COLUMN To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column): Alter Table Aga Drop Column DateOfBirth Date; 5
ALTER TABLE - MODIFY COLUMN To change the data type of a column in a table, use the following syntax: Alter Table Aga Modify ST_Name NVARCHAR2(22) Data Manipulation Language: The SQL INSERT INTO Statement The INSERT INTO statement is used to insert new records in a table. INSERT INTO Syntax It is possible to write the INSERT INTO statement in two ways. The first way specifies both the column names and the values to be inserted: INSERT INTO Aga (ID,ST_Name,ST_Dept) VALUES(1,'Adam','Network'); SELECT * FROM Aga 6
If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. The INSERT INTO syntax would be as follows: INSERT INTO Aga VALUES (2,'Aram','Network'); SELECT * FROM Aga The SQL INSERT INTO SELECT Statement The INSERT INTO SELECT statement copies data from one table and inserts it into another table. INSERT INTO SELECT Syntax Copy all columns from one table to another table: CREATE TABLE Aga2 (ID NUMBER(2),St_Name VARCHAR2(14),ST_Dept VARCHAR2(13)); INSERT INTO Aga2 SELECT * FROM Aga; SELECT * FROM Aga2; Copy only some columns from one table into another table: 7
CREATE TABLE Aga3 (ID NUMBER(2),St_Name VARCHAR2(14)); INSERT INTO Aga3 (ID,ST_Name) SELECT ID,ST_Name FROM Aga2; The SQL UPDATE Statement The UPDATE statement is used to modify the existing records in a table. UPDATE Syntax UPDATE Aga SET ST_Dept='Programming' WHERE ID=1; UPDATE Aga SET ST_Dept='Programming' WHERE ST_Name= Aram ; SELECT * FROM Aga The SQL DELETE Statement The DELETE statement is used to delete existing records in a table. 8
DELETE Syntax DELETE FROM Aga WHERE ID=1; DELETE FROM Aga WHERE ST_Name='Aram'; SELECT * FROM Aga Delete All Records It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be intact: DELETE FROM Aga; SELECT * FROM Aga; 9
SQL Constraints SQL constraints are used to specify rules for the data in a table. Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the table. If there is any violation between the constraint and the data action, the action is aborted. Constraints can be column level or table level. Column level constraints apply to a column, and table level constraints apply to the whole table. The following constraints are commonly used in SQL: NOT NULL - Ensures that a column cannot have a NULL value UNIQUE - Ensures that all values in a column are different PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table FOREIGN KEY - Uniquely identifies a row/record in another table CHECK - Ensures that all values in a column satisfies a specific condition DEFAULT - Sets a default value for a column when no value is specified SQL NOT NULL Constraint By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field. The following SQL ensures that the "ID", "LastName", and "FirstName" columns will NOT accept NULL values: CREATE TABLE Persons1 ( ID INT NOT NULL,LastName VARCHAR(255) NOT NULL,FirstName VARCHAR(255) ); DESC Persons1 10
SQL UNIQUE Constraint The UNIQUE constraint ensures that all values in a column are different. Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint. However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table. SQL UNIQUE Constraint on CREATE TABLE The following SQL creates a UNIQUE constraint on the "ID" column when the "Persons1" table is created: CREATE TABLE Persons2 ( ); ID INT NOT NULL UNIQUE, LastName VARCHAR(255) NOT NULL, FirstName VARCHAR(255) DESC Persons2 SQL UNIQUE Constraint on ALTER TABLE To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following SQL syntax: ALTER TABLE Persons2 ADD CONSTRAINT UC_Person UNIQUE (ID,LastName); 11
DROP a UNIQUE Constraint To drop a UNIQUE constraint, use the following SQL: ALTER TABLE Persons2 DROP CONSTRAINT UC_Person; SQL PRIMARY KEY Constraint The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only one primary key, which may consist of single or multiple fields. SQL PRIMARY KEY on CREATE TABLE The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons3" table is created: CREATE TABLE Persons3 ( ID INT NOT NULL PRIMARY KEY, LastName VARCHAR(255) NOT NULL, FirstName VARCHAR(255)); DESC Persons3 12
SQL PRIMARY KEY on ALTER TABLE To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax: ALTER TABLE Persons3 ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName); DROP a PRIMARY KEY Constraint To drop a PRIMARY KEY constraint, use the following SQL: ALTER TABLE Persons3 DROP CONSTRAINT PK_Person; SQL FOREIGN KEY Constraint A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table. The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table. Look at the following two tables: "Persons4" table: 13
"Orders" table: Notice that the "PersonID" column in the "Orders" table points to the "PersonID" column in the "Persons4" table. The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons4" table. The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table. The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. The FOREIGN KEY constraint also prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the table it points to. CREATE TABLE Persons4 ( PersonID INT NOT NULL PRIMARY KEY, LastName VARCHAR(255) NOT NULL, FirstName VARCHAR(255)); CREATE TABLE Orders ( 14
OrderID INT NOT NULL PRIMARY KEY, OrderNumber INT NOT NULL, PersonID INT, CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID) REFERENCES Persons4(PersonID)); SQL FOREIGN KEY on ALTER TABLE To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is already created, use the following SQL: ALTER TABLE Orders ADD CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID) REFERENCES Persons4(PersonID); DROP a FOREIGN KEY Constraint To drop a FOREIGN KEY constraint, use the following SQL: ALTER TABLE Orders DROP CONSTRAINT FK_PersonOrder; 15
SQL 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. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row. SQL CHECK on CREATE TABLE The following SQL creates a CHECK constraint on the "Age" column when the "Persons5" table is created. The CHECK constraint ensures that you cannot have any person below 18 years: CREATE TABLE Persons5 ( ID INT NOT NULL, LastName VARCHAR(255) NOT NULL, FirstName VARCHAR(255), Age INT CHECK (Age>=18)); SQL CHECK on ALTER TABLE To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax: ALTER TABLE Persons5 ADD CONSTRAINT CHK_PersonAge CHECK (Age>=18 AND Age<=36); 16
DROP a CHECK Constraint To drop a CHECK constraint, use the following SQL: ALTER TABLE Persons5 DROP CHECK CHK_PersonAge; Disable Constraint ALTER TABLE Persons5 DISABLE CONSTRAINT CHK_PersonAge Enable Constraint ALTER TABLE Persons5 ENABLE CONSTRAINT CHK_PersonAge SQL 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. 17
SQL DEFAULT on CREATE TABLE The following SQL sets a DEFAULT value for the "City" column when the "Persons" table is created: CREATE TABLE Persons6 (ID INT NOT NULL, LastName VARCHAR(255) NOT NULL, FirstName VARCHAR(255),City VARCHAR(255) DEFAULT 'Hawler'); DESC Persons6 SQL DEFAULT on ALTER TABLE To create a DEFAULT constraint on the "City" column when the table is already created, use the following SQL: ALTER TABLE Persons6 MODIFY City DEFAULT 'Hawler'; DROP a DEFAULT Constraint To drop a DEFAULT constraint, use the following SQL: ALTER TABLE Persons6 ALTER COLUMN City DROP DEFAULT; 18