0% found this document useful (0 votes)
5 views

Unit 3 Note 2 for lab activity

The document provides instructions for creating a relational database in Microsoft Access, including SQL code to create five tables: Students, Subject, MarkList, SubjectAssigned, and Teachers. It details the data types and constraints used in the tables, as well as examples of INSERT and UPDATE SQL statements for populating and modifying the database. Additionally, it includes exercises for further practice with SQL commands.

Uploaded by

kuuyemariam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unit 3 Note 2 for lab activity

The document provides instructions for creating a relational database in Microsoft Access, including SQL code to create five tables: Students, Subject, MarkList, SubjectAssigned, and Teachers. It details the data types and constraints used in the tables, as well as examples of INSERT and UPDATE SQL statements for populating and modifying the database. Additionally, it includes exercises for further practice with SQL commands.

Uploaded by

kuuyemariam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

First create a database and give it a name, ex. YourFnameSection and save it in your folder.

To create the database tables in Microsoft Access based on the relational database model on page
64 of your textbook, you can use the following SQL code. This code will create the five tables:
Students, Subject, MarkList, SubjectAssigned, and Teachers.

CREATE TABLE Students (


SCode VARCHAR(10) PRIMARY KEY,
SName VARCHAR(50),
FName VARCHAR(50)
);

CREATE TABLE Subject (


Tid VARCHAR(10) PRIMARY KEY,
SubjectName VARCHAR(100),
Grade VARCHAR(10),
CHours INT
);

CREATE TABLE MarkList (


SCode VARCHAR(10),
SMark INT,
Tid VARCHAR(10),
PRIMARY KEY (SCode, Tid),
FOREIGN KEY (SCode) REFERENCES Students(SCode),
FOREIGN KEY (Tid) REFERENCES Subject(Tid)
);

CREATE TABLE SubjectAssigned (


SCode VARCHAR(10),
Tid VARCHAR(10),
PRIMARY KEY (SCode, Tid),
FOREIGN KEY (SCode) REFERENCES Students(SCode),
FOREIGN KEY (Tid) REFERENCES Subject(Tid)
);

CREATE TABLE Teachers (


Tid VARCHAR(10) PRIMARY KEY,
TName VARCHAR(50),
Specialize VARCHAR(50)
);
Tables of the database:-
 Students: Contains student code, student name, and father's name.
 Subject: Contains subject ID, subject name, grade, and credit hours.
 MarkList: Links students to their marks in specific subjects.
 SubjectAssigned: Links students to the subjects they are assigned.
 Teachers: Contains teacher ID, name, and specialization.
The data types and constraints used in the SQL code for the Microsoft Access
database tables are given as follows:

1. VARCHAR(n)

 This data type is used for variable-length strings. The n specifies the maximum number
of characters that can be stored.
 Example: VARCHAR(10) can store up to 10 characters.
 Use Case: Suitable for text fields like names, codes, or identifiers where the length can
vary.

2. INT

 This data type is used for integer values. It stores whole numbers without decimals.
 Use Case: Ideal for storing numerical data such as marks, counts, or quantities.

3. PRIMARY KEY

 This is not a data type but a constraint that uniquely identifies each record in the table.
Each table can have only one primary key, which can consist of one or more columns.
 Use Case: Ensures that each entry in the table is unique and helps maintain data integrity.

4. FOREIGN KEY

 Again, this is a constraint rather than a data type. It establishes a relationship between
two tables by linking a column (or a group of columns) in one table to the primary key of
another table.
 Use Case: Used to enforce referential integrity, ensuring that the value in one table
corresponds to a valid entry in another.

Summary of Data Types Used:

 VARCHAR(n): For variable-length string data (e.g., names, codes).


 INT: For integer values (e.g., marks, credit hours).

These data types help define the structure of the database, ensuring that data is stored in a
consistent and efficient manner.
 So far we used the DDL to create the database schema or structure. The next task is data
insertion which is DML usage. Learn the following codes and insert the data into the
tables,
INSERT INTO Students (SCode, SName, FName) VALUES
('S001', 'Chaltu', 'Gemeda'),
('S002', 'Alemene', 'Setegen'),
('S003', 'Muna', 'Haftom'),
('S004', 'Haftom', 'Kiros');

INSERT INTO Subject (Tid, SubjectName, Grade, CHours) VALUES


('T101', 'Mathematics for Grade 9', '9', 4),
('T102', 'Mathematics for Grade 10', '10', 4),
('T103', 'Physics for Grade 9', '9', 4),
('T104', 'Physics for Grade 10', '10', 4);

INSERT INTO MarkList (SCode, SMark, Tid) VALUES


('S001', 85, 'T101'),
('S002', 90, 'T102'),
('S003', 75, 'T101'),
('S004', 80, 'T104');

INSERT INTO SubjectAssigned (SCode, Tid) VALUES


('S001', 'T101'),
('S002', 'T102'),
('S003', 'T101'),
('S004', 'T104');

INSERT INTO Teachers (Tid, TName, Specialize) VALUES


('T101', 'Abel', 'Ashenafi'),
('T102', 'Amanuel', 'Mathematics'),
('T103', 'Selam', 'Physics');

How the ‘Inserts’ works:

 Students Table: Adding four students with their respective codes and names.
 Subject Table: Adding four subjects with their IDs, names, grades, and credit hours.
 MarkList Table: Inserting the marks for specific students in given subjects.
 SubjectAssigned Table: Assigning subjects to students.
 Teachers Table: Adding teacher records with their IDs, names, and specializations.
 Make sure to run each INSERT INTO statement sequentially in your Microsoft Access
SQL view to populate your database with the sample data.

Do the following exercises involving UPDATE and INSERT SQL statements

Insert Exercises

Exercise 1 Insert a new student.

INSERT INTO Students (SCode, SName, FName) VALUES ('S005', 'Liya', 'Tesfaye');
Adds a new student with the code 'S005', name 'Liya', and father’s name
'Tesfaye'.

Exercise 2 Insert a new subject.


INSERT INTO Subject (Tid, SubjectName, Grade, CHours) VALUES ('T105',
'Chemistry for Grade 10', '10', 4); Adds a chemistry subject for Grade 10 with
4 credit hours.

Exercise 3 Insert a new teacher.


INSERT INTO Teachers (Tid, TName, Specialize) VALUES ('T104', 'Marta',
'Biology'); Adds a new teacher specializing in Biology.

Exercise 4 Assign a subject to a student.


INSERT INTO SubjectAssigned (SCode, Tid) VALUES ('S005', 'T105'); Assigns the
newly added subject to the student 'S005'.

Exercise 5 Insert a new mark for a student in a subject.


INSERT INTO MarkList (SCode, SMark, Tid) VALUES ('S005', 88, 'T105'); Records
an 88 mark for student 'S005' in the subject 'T105'.

Update Exercises

Exercise 6 Update a student's father's name.


UPDATE Students SET FName = 'Samuel' WHERE SCode = 'S005'; Changes the
father's name of student 'S005' to 'Samuel'.

Exercise 7 Update a subject’s name.


UPDATE Subject SET SubjectName = 'Advanced Mathematics for Grade 9' WHERE Tid
= 'T101'; Updates the subject name for 'T101'.

Exercise 8 Update a teacher’s name.


UPDATE Teachers SET TName = 'Dr. Marta' WHERE Tid = 'T104'; Changes the
teacher's name to 'Dr. Marta'.

Exercise 9 Update a student's mark.


UPDATE MarkList SET SMark = 92 WHERE SCode = 'S005' AND Tid = 'T105'; Updates
the mark for student 'S005' in subject 'T105' to 92.
Exercise 10 Update credit hours for a subject.
UPDATE Subject SET CHours = 5 WHERE Tid = 'T105'; Changes the credit hours for
the subject 'T105' to 5.

Additional Insert Exercises

Exercise 11 Insert multiple students.


INSERT INTO Students (SCode, SName, FName) VALUES
('S006', 'Tina', 'Selam'),
('S007', 'Kofi', 'Abena'); Adds two new students in one statement.

Exercise 12 Insert multiple marks.


INSERT INTO MarkList (SCode, SMark, Tid) VALUES
('S006', 85, 'T102'),
('S007', 78, 'T101'); Records marks for two new students in their respective
subjects.

Additional Update Exercises

Exercise 13 Update a student's name.


UPDATE Students SET SName = 'Tina A.' WHERE SCode = 'S006'; Changes the name
of student 'S006' to 'Tina A.'.

Exercise 14 Update a teacher's specialization.


UPDATE Teachers SET Specialize = 'Chemistry' WHERE Tid = 'T104'; Changes the
specialization of teacher 'T104' to Chemistry.

Exercise 15 Update a student’s assignment.


UPDATE SubjectAssigned SET Tid = 'T102' WHERE SCode = 'S006' AND Tid = 'T105';
Changes the subject assigned to student 'S006' from 'T105' to 'T102'.

Exercise 16 Update mark for a specific subject.


UPDATE MarkList SET SMark = 80 WHERE SCode = 'S007' AND Tid = 'T101'; Updates
the mark for student 'S007' in subject 'T101' to 80.

Final Insert and Update Exercises

Exercise 17 Insert a new subject for Grade 9.


INSERT INTO Subject (Tid, SubjectName, Grade, CHours) VALUES ('T106', 'Biology
for Grade 9', '9', 4); Adds a new biology subject for Grade 9.

Exercise 18 Update a student's assignment to a new subject.


UPDATE SubjectAssigned SET Tid = 'T106' WHERE SCode = 'S005'; Updates the
subject assigned to student 'S005' to the new subject 'T106'.

Exercise 19 Update teacher's specialization.


UPDATE Teachers SET Specialize = 'Physics and Chemistry' WHERE Tid = 'T102';
Changes the specialization of teacher 'T102'.

Exercise 20 Update a subject's grade.


UPDATE Subject SET Grade = '11' WHERE Tid = 'T102'; Updates the grade for
subject 'T102' to Grade 11.

You might also like