You are on page 1of 19

Fundamental of database system

Lab

Part one

by Tadesse S. 1
Database
✓A database is a collection of related data
Database management system (DBMS)
✓ is a collection of programs that enables users to create and maintain a
database.
✓defining, constructing, manipulating and sharing databases among
various applications

by Tadesse S. 2
Structural Query Language (SQL)
✓used to communicate with a database.
➢SQL can create new databases
➢ SQL can create new tables in a database
➢ SQL can set permissions on tables
➢ SQL can execute queries against a database
➢ SQL can retrieve data from a database
➢ SQL can modify records in a database

by Tadesse S. 3
SQL can be divided into two parts:
✓Data Definition Language (DDL)
✓Data Manipulation Language (DML)

Data definition language (DDL) refers to the set of SQL commands


that can create and manipulate the structures of a database.

Data Manipulation Language (DML) deals with the manipulation of


data present in the database

by Tadesse S. 4
DDL statement includes
✓CREATE DATABASE - creates a new database
✓ALTER DATABASE - modifies a database
✓CREATE TABLE - creates a new table
✓ALTER TABLE - modifies a table
✓DROP TABLE - deletes a table
✓CREATE INDEX - creates an index (search key)
✓DROP INDEX - deletes an index

by Tadesse S. 5
DML statements include
✓SELECT - extracts data from a database
✓UPDATE - updates data in a database
✓DELETE - deletes data from a database
✓INSERT INTO - inserts new data into a database

by Tadesse S. 6
by Tadesse S. 7
• Steps to open SQL Server

Start ➔ All programs ➔ Microsoft SQL server 2008 ➔


SQL server management studio

by Tadesse S. 8
Create Database (DDL)
Syntax : CREATE DATABASE Database_Name
Eg. CREATE DATABASE DDUIT
Lab-task-1: create database with name DDUIT
Next execute use command or select your database before create table
use DDUIT
Or

by Tadesse S. 9
Create table (DDL)
• Syntax for creating a table (without constraints):

CREATE table tablename(column1 data type,column2 data


type,…,columnm data type)
Most commonly used data types in SQL: int, float, money, datetime,
varchar, text, char, image etc.

by Tadesse S. 10
Eg. Create Student and Course table in DDUIT database
create table Student
(
id varchar(30),
fname varchar(30),
lname varchar (30),
age int
)
create table course
(
code varchar(30),
cname varchar(30),
crhr int
)
Lab-task-2: create student and course table to your database created in lab-task-1
by Tadesse S. 11
Check as you create successfully
select * from INFORMATION_SCHEMA.TABLES

Drop table from database


Syntax: Drop table Table_Name
Eg. drop table course
Current database

Lab-task-3: drop course table from your database


by Tadesse S. 12
Insert statement (DML)
Syntax : INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

Eg. insert into student(id,fname,lname,age) values


('DDU01693','Tadesse','Shiferawu',26)
Or (if you insert to all column)
insert into student values ('DDU01693','Tadesse','Shiferawu',26)
Lab-task-4: insert your information to student table

by Tadesse S. 13
View data in table using select keyword
Syntax: SELECT column_name(s) FROM table_name
OR SELECT * FROM table_name
Tip: The asterisk (*) is a quick way of selecting all columns!
Eg. select * from student

select id, fname from student

Lab-task-5: view your information from student table


by Tadesse S. 14
Today Exercise(5%)
1. insert your group member to student table
2. view all student list with all attribute
3. view all student with id, fname and age attribute

by Tadesse S. 15
Part two
Finish previous exercise
In previous class, I hope you are familiar with the following statement
in different 5 tasks.
=>create (to create table and database)
=>drop (to remove table from database)
=>insert (for insert data to table)
=>select (to view from table using * and for specific column)

by Tadesse S. 16
Delete all data from table
Syntax: DELETE FROM table_name
Eg. Delete all element from student table
delete from Student
This delete all data, to delete specific data we use where close(next
class)
Lab task 6:- delete all data from student table

by Tadesse S. 17
Alter (DDL)
ALTER TABLE statement can be used to alter, add or drop
individual columns and constraints in the table.
Syntax:
ALTER TABLE table_name
ADD column_definition [column+data type+constraint]

ALTER TABLE table_name


DROP COLUMN column_name

by Tadesse S. 18
From previous task we use course table from this example(if course
table is unavailable recreate it )
1. To add one column on course table with name=“Module” with
varchar type.
alter table course add module varchar(30)
2. To remove unwanted column (eg. Module)from course column
alter table course drop column module
Lab Task 7: add two column on student table (use student table from
previous class)
Lab Task 8: remove age column from student table

by Tadesse S. 19

You might also like