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

IT4A IDB Labs

The document outlines the contents of several database labs covering topics like creating and modifying databases and tables in Microsoft Access and SQL Server, adding and querying data, and working with keys. Lab activities include making databases and tables, adding records, importing data, creating and deleting databases, and more.

Uploaded by

Zahoor ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

IT4A IDB Labs

The document outlines the contents of several database labs covering topics like creating and modifying databases and tables in Microsoft Access and SQL Server, adding and querying data, and working with keys. Lab activities include making databases and tables, adding records, importing data, creating and deleting databases, and more.

Uploaded by

Zahoor ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction to Database Systems

Labs’ Contents
BSIT 4th Semester - FICT, BUITEMS
Note: Typos are expected, please inform the instructor if any found

Lab 1

 Introduction to Database Management System, Fields/ Attributes, Records, Tables


 Microsoft Access
 Creating new Database in MS access and renaming them
 Creating new tables in MS Access Database and renaming them
 Adding Fields to Database Table
 Different field Types in MS Access
 Adding Primary to Table
 Modifying field names and modifying their data types
 Deleting tables in MS Access and deleting databases in MS Access
 Adding new records to MS Access DB and modifying existing records in MS Access
 Adjusting Size of database fields in MS Access
 Deleting records in MS Access
 Importing Data to MS Access Database
Lab 2

 Introduction to Microsoft SQL Server


 Interaction with login screen of MS SQL Server, putting your credentials to login and logging
in MS SQL Server
 Interacting with Object Explorer of MS SQL Server Management Studio
 Expanding Database section of Object Explorer and revealing existing databases
 Creating new Database using Objective Explorer
 Renaming new database using Object Explorer
 Deleting existing database
 Creating new table, saving table and deleting table
 Adding fields to new table and setting data type of fields
 Adding data in tables and deleting data in tables using GUI
 Introduction to SQL (Structured Query Language)
 Introduction to Data Definition Language (DDL), Data Manipulation Language (DML) and
Data Query Language (DQL)

 The Create Database Command: Used to create New Database


o Syntax: CREATE DATABASE <database Name>;
o Example: CREATE DATABASE UniversityDB;
 The Create Table Command: - it defines each column of the table uniquely. Each column has
minimum of three attributes, a name , data type and size
o Syntax: CREATE TABLE <table name> (<col1> <datatype>(<size>),<col2>
<datatype><size>));
o Example: CREATE TABLE student(stuID int PRIMARY KEY, firstName varchar(50));

 Inserting Data into Tables: - once a table is created the most natural thing to do is load this
table with data to be manipulated later.
o Syntax: INSERT INTO <table name> (<col1>,<col2>) VALUES(<exp>,<exp>);
o Exmple: INSERT INTO student (stuID, firstName) VALUES(1, ‘Hamza’);

 Viewing data in the tables: - once data has been inserted into a table, the next most logical
operation would be to view what has been inserted
o To view all fields and their data
 Syntax: SELECT * FROM tablename;
 Example: SELECT * FROM students
o To view all fields and their data
 Syntax: SELECT <column name> ,<column name> FROM tablename;
 Example: SELECT stuID FROM tableName;
o Filed Aliases/ Column Aliases
o Syntax: SELECT <column name> AS <alias> FROM tablename;
o Example: SELECT stuID AS ‘IDs of Students’ FROM students;
Lab 3

Changing Table Structure problems: (Solution)

The Save (Not Permitted) dialog box warns you that saving changes is not permitted because
the changes you have made require the listed tables to be dropped and re-created.

The following actions might require a table to be re-created: Adding a new column to the
middle of the table

 Dropping a column
 Changing column nullability
 Changing the order of the columns
 Changing the data type of a column <<<<

To change this option, on the Tools menu, click Options, expand Designers, and then click
Table and Database Designers. Select or clear the Prevent saving changes that require
the table to be re-created check box

 SELECT…WHERE Clause
o SYNTAX: SELECT <fieldName1, FieldName2> FROM <TableName> WHERE
<fieldToWhichConditionRequired> <someOperator> <someValue>
o SELECT * FROM STUDENTS WHERE GPA > 3

o EXAMPLE: SELECT firstName, lastName from students WHERE firstName =


’Hamza’
 AND
o SYNTAX: SELECT <fieldName1, FieldName2> FROM <TableName> WHERE
<fieldToWhichConditionRequired> <someOperator> <someValue> AND
<fieldToWhichConditionRequired> <someOperator> <someValue>

o EXAMPLE: SELECT firstName, lastName from students WHERE


firstName=’Hamza’ AND lastName=’Khan’

 OR
o SYNTAX: SELECT <fieldName1, FieldName2> FROM <TableName> WHERE
<fieldToWhichConditionRequired> <someOperator> <someValue> OR
<fieldToWhichConditionRequired> <someOperator> <someValue>

o EXAMPLE: SELECT firstName, lastName from students WHERE


firstName=’Hamza’ OR firstName=’Shayan’

 NOT Operator
o SYNTAX: SELECT <fieldName1, FieldName2> FROM <TableName> WHERE
<fieldToWhichConditionRequired> <!someOperator> <someValue> OR/AND
<fieldToWhichConditionRequired> <!someOperator> <someValue>
o EXAMPLE: SELECT firstName, lastName from students WHERE firstName!
=’Hamza’ OR firstName!=’Shayan’

 Between Clause
o SYNTAX: SELECT <fieldName1, FieldName2> FROM <TableName> WHERE
<fieldToWhichConditionRequired> <BETWEEN> <someValue> AND
<someValue2>

o EXAMPLE: SELECT firstName, lastName from students WHERE Age


BETWEEN 10 AND 20
 Data Ordering
o SYNTAX: SELECT * FROM <TableName> ORDER BY
<fieldToWhichOrderRequired> <ASC/DESC>

Lab 4
o EXAMPLE: SELECT * from students ORDER BY Age ASC

Adding Primary Keys and Foreign Keys to “New Table”


Adding Primary Key while creating new table
CREATE TABLE table_name
(
<column1> <datatype> [ NULL | NOT NULL ],
<column2> <datatype> [ NULL | NOT NULL ],
...
CONSTRAINT <constraint_name> PRIMARY KEY (Column of PK)
)

Adding Composite Primary Key while creating table


CREATE TABLE <table_name>
(
<column1> <datatype> [ NULL | NOT NULL ],
<column2> <datatype> [ NULL | NOT NULL ],
...
CONSTRAINT <constraint_name> PRIMARY KEY (column1, column2, ... column_n)
)

Adding Foreign Key while creating table

CREATE TABLE child_table


(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
...

CONSTRAINT fk_name
FOREIGN KEY (child_col1)
REFERENCES parent_table (parent_col1)

)
Adding Composite Foreign Key while creating table
CREATE TABLE child_table
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
...

CONSTRAINT fk_name
FOREIGN KEY (child_col1, child_col2, ... child_col_n)
REFERENCES parent_table (parent_col1, parent_col2, ... parent_col_n)
)

Alter Command, Primary Keys, Foreign Keys


Adding new column to existing table
ALTER TABLE <table Name> ADD <new field name> <new field data type><new filed size>
ALTER TABLE STUDENT ADD TESTFIELD INT

Deleting column from existing table


ALTER TABLE <table Name> DROP COLUMN <name of field to be deleted>
ALTER TABLE STUDENT DROP COLUMN TESTFIELD

Adding Primary Key later on after creating table


ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name> PRIMARY KEY (Column of PK)

Adding Composite Primary Key later on after creating table


ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name> PRIMARY KEY (column1, column2, ... column_n)

Dropping Primary Key


ALTER TABLE <Table Name> DROP CONSTRAINT pk_<Primary Key field Name>
ALTER TABLE Persons DROP CONSTRAINT pk_PersonID

Adding Foreign Key later on after creating table


ALTER TABLE child_table
ADD CONSTRAINT fk_name
FOREIGN KEY (child_col1)
REFERENCES parent_table (parent_col1)

Adding Composite Foreign Key later on after creating table


ALTER TABLE child_table
ADD CONSTRAINT fk_name
FOREIGN KEY (child_col1, child_col2, ... child_col_n)
REFERENCES parent_table (parent_col1, parent_col2, ... parent_col_n);

Dropping Foreign Key


ALTER TABLE table_name
DROP CONSTRAINT fk_name;

Deletions from table and table itself


The DELETE command can be used to remove a record(s) from a table
DELETE FROM tablename
WHERE condition

To delete all the records from a table without deleting the table do
TRUNCATE TABLE <table Name>

To remove an entire table from the database use the DROP command.
DROP TABLE tablename

Lab 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy