Professional Documents
Culture Documents
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)
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
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):
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
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
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]
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