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

Lab 2

This document provides an introduction to database systems and the relational model. It discusses key concepts like entities, attributes, tables, keys, and constraints. It explains how to work with SQL to define schemas, tables, data types, and constraints using commands like CREATE, ALTER, and DROP. Examples are provided for how to create tables and add, modify, and drop constraints. Students are instructed to submit SQL commands to create a data type, table, and add constraints.

Uploaded by

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

Lab 2

This document provides an introduction to database systems and the relational model. It discusses key concepts like entities, attributes, tables, keys, and constraints. It explains how to work with SQL to define schemas, tables, data types, and constraints using commands like CREATE, ALTER, and DROP. Examples are provided for how to create tables and add, modify, and drop constraints. Students are instructed to submit SQL commands to create a data type, table, and add constraints.

Uploaded by

ibrahim zayed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

TAFILA TECHNICAL UNIVERSITY

Faculty of Engineering
Department of Communication and Computer Engineering

Database Systems Lab


Lab #2

Objectives:

- Introduce basic concepts of the relational model.


o Key constraints, Entity integrity constraints.
- Learn how to apply these concepts in practice using SQL.
- Learn how to use SQL as a Data Definition Language (DDL):
o Create, Alter, Drop.

Introduction:

The relational model is the most used data model in database systems. It models the data from the
miniworld as tables or relations. Each table usually describes one real world entity (a student, a book, a
product, a bank account …). These entities are described through attributes. For example, we can
describe a student by his first name, last name, student ID, GPA, birth date, department, etc. In a table
or a relation each one of these attributes is represented as a column, and the relation will have a row or
a tuple for every new entity (for every student in our previous example). Every attribute has a domain
from which it takes its value. The simplest types of domains are simple data types like integers,
characters, Boolean values, etc. Also, domains can be constrained, e.g., an age attribute for a student
can be constrained to have values larger than 18.

Two entities in a relation are different if they differ in at least one value for some attribute. Usually we
distinguish between entities using a single attribute called “Key Attribute” or “Primary Key”. The key
attribute has a unique value for each entity instance or in each row in the table, therefore, it is used to
distinguish between and uniquely identify table rows. In our example the student ID can serve as the
primary key because it is unique for every student. Formally, the relational model requires every table or
relation to have a primary key, although this is not enforced in SQL, i.e., a table can be created without a
primary key in SQL. This constraint in the relational model is called “Key constraint”. Another important
constraint in the relational model is called “Entity integrity constraint”, which simply says that the
primary key attribute cannot be null for any row in the table, why? The third type of relational
constraints (which will not be discussed in this lab) is called “Referential integrity constraints”. This
constraint represents the relationship between two tables, and it’s out of the scope of this lab.

Data Definition in SQL:

This lab gives an introduction to the data definition capabilities of SQL. Data definition means that we
can use SQL to define or describe and create data or data structures. The “CREATE” command in SQL is
very powerful and can create any database-related object. We saw in the previous lab how to create
user accounts on the command line. Using the same “CREATE” command we can define new databases,
schemas, tables, data types, etc. In addition to the “CREATE” command, the SQL provides “ALTER” to
modify on data structures after they are created, and “DROP” to delete data structures if they are not
needed anymore.

Data definition can be done on the command line using the commands we discussed above, or it can be
done on the GUI using the SQL Server Management Studio (SSMS). To define a new database using
SSMS, connect to your database instance, right click on the “Databases” folder and choose “New
Database”, and continue with the wizard. Defining tables can be done in a similar fashion. Right-click on
the “Tables” folder in the database you want to create the table in, and choose “New Table”. Then, add
the fields you like, choose the data type for each one of them, indicate whether nulls are allowed or not
in this field, and finally save the table and give it a name.

Altering tables in SSMS is also easy, just right-click on the table you want to modify and choose “Design”.
Then you can add, delete, or modify the columns in the table. Finally, to drop a table from the database,
choose it in the object explorer, right-click and choose delete then click OK.

Note that the operations you do on SSMS are translated to SQL statements first, and then executed on
the server. You can view the code for each operation you do in SSMS. When creating a new database or
dropping an existing database or table you can view the SQL statements by choosing “Script Action to
New Query Window” from the scripts menu in the wizard. You can also view the SQL statements for
creating and altering tables by clicking the “Generate Change Script” button in the SSMS toolbar. Be
careful when reading these scripts because a lot of the code in these scripts is redundant and executes a
lot of unneeded commands.

To use the create command in SQL, either use the SQL command line we saw in previous lab, or click on
the “New Query” button in the SSMS toolbar. It is worth mentioning that SQL syntax is not
case sensitive, so it does not matter who you write the commands below as long as you get the spelling
correct. However, to distinguish SQL commands from variable names we write all SQL commands in
capital letters. Optional syntax will be enclosed within brackets. The following shows some simple data
definition commands to create, and alter different data structures using SQL:

- To create a schema:
o CREATE SCHEMA schema_name;
- To create a table:
o CREATE TABLE [schema_name.]table_name (column_name1 <data type> [NULL|NOT
NULL] [PRIMARY KEY], …);
o Recall that if the schema name is not included the new table will belong to the “dbo”
schema (dbo stands for DataBase Owner).
o <data_type> can be int, float, char, varchar, datetime, etc.
o Note that by default null values are allowed in table attributes unless the “NOT NULL”
option is used.
o Also note that the “PRIMARY KEY” option can be used only with one column.
o The “PRIMARY KEY” option covers two relational model constraints; key constraints and
entity integrity constraints. That is, if column X is selected as a primary key, then it is
unique for every row (key constraint) and cannot be null for any row (entity integrity
constraints).
- To create a simple user-defined data type from existing data types:
o CREATE TYPE type_name FROM <data type> [NULL|NOT NULL];
o For example:
 CREATE TYPE student_name FROM varchar(30) NOT NULL;
 This statement creates a new data type called “student_name”, which is
basically a variable length character type of maximum length of 30 characters
that cannot be left empty when a new record is created.
- To add a column to a table after it has been created:
o ALTER TABLE table_name ADD column_name <data_type>;
- To drop a column from a table after it has been created:
o ALTER TABLE table_name DROP COLUMN column_name;
- To drop a table from the database:
o DROP TABLE table_name;
- To drop a schema from the database:
o DROP SCHEMA schema_name;

Working with constraints:

In the previous section we saw how to enforce “NOT NULL” constraints on columns of a newly created
table. We also saw the “PRIMARY KEY” constraint, which specifies the primary key attribute of a table. In
this section we show few more constraints, and how to deal with them.

- A column can be constrained to have a unique value for every row by using the “UNIQUE”
constraint. Note that with unique constraints if column X is chosen to be unique, then at most
one row can have a null value for X.
- A default value can be specified for a column in case it was not filled with data when a new row
is added. This is done using the “DEFAULT” constraint.
- The domain of an attribute or a column can be constrained to have values within a certain range
or that satisfy a certain condition using the “CHECK” constraint.

The syntax for adding or defining constraints (except for NULL or NOT NULL constraints) on an existing
table is as follows:

ALTER TABLE table_name ADD CONSTRAINT constraint_name <constraint definition>;

To remove or drop a constraint:

ALTER TABLE table_name DROP CONSTRAINT constraint_name;


Try to understand and explain the following examples on a table called T1 with the following fields (A
int, B char, C int):

1. ALTER TABLE t1 ADD CONSTRAINT C_unq UNIQUE (C);


2. ALTER TABLE t1 ADD CONSTRAINT B_default DEFAULT ‘B’ FOR B;
3. ALTER TABLE t1 ADD CONSTRAINT A_chk CHECK (A > 10 and A < 100);
4. ALTER TABLE t1 DROP CONSTRAINT a_chk, b_default, c_unq;

Submission:

You should submit a report that includes the SQL commands to do the following:

1. Create a new data type, which is basically a fixed length string of 10 characters, and name this
new data type “ID”.
2. Create a table named “Student”. The table should include the attributes (Student_ID,
First_name, Last_name, Birth_date, Gender, Major, GPA). The Student_ID is of type ID (created
in previous step), the names are strings that can be at most 30 characters long, the gender is a
single character (either F or M), the Major is a string that can be 50 characters long, and the GPA
is a floating number.
3. Alter the table to add the following constraints:
a. Make the Student_ID attribute a primary key, and name the constraint STDNT_PK.
b. Make the combination of the first and last names unique, and name the constraint
STDNT_UNQ.
c. Add a constraint to check that the value for gender is either ‘M’ or ‘F’, and name the
constraint STDNT_CHK.
d. Add a default value constraint for the GPA to be 0 by default, and name the constraint
STDNT_DEF.
4. Write some possible records (table rows) that violate each one of the constraints you added.
This is not SQL, just show what possible data entry scenarios could violate the constraints.
5. How can you remove the constraints? Write the command and test it in the lab.
6. What is the difference between the primary key constraints and the unique constraints? Give
one difference.
In addition, the report should include any problems and solutions you have encountered in the lab and a
summary of the things you have learned.

You might also like