Professional Documents
Culture Documents
alter table customer drop column sex; CID CNAME AGE ADDRESS SALARY
ALTER TABLE customer ADD PRIMARY KEY (teacher_id)
ALTER command to create relationship
ALTER TABLE COURSE ADD FOREIGN KEY (C_Department) REFERENCES DEPARTMENT (D_Number);
INSERT COMMAND
Syntax: INSERT INTO table_name VALUES (value1, value2, value3 ...)
Example: INSERT INTO CUSTOMERS VALUES (7, ‘Dawit', 24, ‘Hawassa', 10000 );
CID CNAME AGE ADDRESS SALARY
7 Dawit 24 Hawassa 10000
UPDATE COMMAND
The UPDATE command does not add new records to a table, nor does it remove records. It
simply updates existing records in the table.
The UPDATE command is used to change a value of one or more fields in an existing table
row or number of rows.
Syntax: UPDATE table_name SET column1 = value1…WHERE condition;
UPDATE customer SET SALARY = 15000 WHERE SALARY<10000;
UPDATE Teacher SET SALARY = 20000 WHERE SEX=’female’;
DELETE COMMAND
The DELETE command is used to delete a record or multiple records from the database.
Syntax: DELETE FROM table_name WHERE condition;
Example:DELETE FROM Teacher WHERE T_IDNO =’Teach/2312/91’;
To delete all records from a table, you can use one of the following commands:
DELETE FROM table_name; or DELETE * FROM table_name
3.2.4 Data Query Language – SELECT Command
DQL provides a SELECT command for querying all or subset of records from one or
more tables of a database.
The SELECT command provides options for filtering and getting more meaningful
results from the database.
The syntax to retrieve all records/columns from a table is: SELECT * FROM
table_name; The statement is read as “Select all records from a table”.
The asterisk is equivalent to listing all fields in the table.
SELECT * FROM customer;
Selecting a Limited Number of Columns
SELECT field1, [field2] FROM table [WHERE criterion];
SELECT CNAME, SALARY FROM CUSTOMER WHERE SEX = ‘M’;
FILTER RECORDS ON MULTIPLE CRITERIA: USING AND & OR
SELECT* FROM CUSTOMER WHERE SEX = ‘M’ AND SALARY>5000;
SELECT* FROM CUSTOMER WHERE SEX = ‘M’ OR SALARY>5000;
SELECT Command ORDER BY Clause ORDER BY clause is used to order the
output in ascending or descending
Example: SELECT with ORDER BY clause SELECT column1, [column2], …FROM
table_name ORDER BY [column] [ASC][DESC]
SELECT*FROM customer ORDER BY Salary DESC;
SELECTING RECORDS FROM TWO TABLES The SELECT command can be
used to select records from two tables.
SELECT table1.column1, [table1.column2], table2.column1, [table2.column2] FROM
table1, table2 WHERE table1.column1 = table2.column2
SELECT COURSE.Course_id, COURSE.Course_name, COURSE.Grade,
CourseOffering.Semester FROM COURSE, CourseOffering WHERE COURSE.Course_id =
CourseOffering.Course_code AND Semester=’I’;
LABORATORY ACTIVITY
Create a blank database, name it ‘School’.
DEPARTMENT table with attribute –DNumber(CHAR(10),DName(CHAR(30)). Use Dnumber as a primary
KEY .
course table with attribute – courseCode (CHAR(10)), contactHour integer, Corsename(CHAR(30)),
Cdepartment(char(30)). Use Cdepartment as a FOREIGN KEY to create a relationship with the Department
Student table with attribute – student id (CHAR(10)), name (CHAR(20)), sex(CHAR(1)), age(INTEGER),
grade level (INTEGER).
Teacher table with attribute – teacher id (CHAR(10)), name (CHAR(20)), sex(CHAR(1)), age(INTEGER),
specialization (CHAR(15)) salary integer. Set the Teacher id as primary key.
Grade table with attribute – student id (CHAR(10)), teacher id (CHAR(10), course code(CHAR(20)),
mark(INTEGER). Use course code as a FOREIGN KEY to create a relationship with the COURSE
Add column to department called Dlocation by using Alter command and delete column contactHour from
course.
Create the relationship between 5 tables.
Insert 5 records in each table.
Update , delete records
Select records from the table (use where )