0% found this document useful (0 votes)
24 views12 pages

SQL Database Fundamentals Lab Manual

This lab manual provides comprehensive instructions and exercises for using Microsoft SQL Server, covering essential SQL commands such as CREATE, INSERT, UPDATE, DELETE, and various operators. It also includes information on database constraints, joins, and table alterations, along with examples for practical application. Students are encouraged to provide feedback and contact the instructors for assistance.

Uploaded by

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

SQL Database Fundamentals Lab Manual

This lab manual provides comprehensive instructions and exercises for using Microsoft SQL Server, covering essential SQL commands such as CREATE, INSERT, UPDATE, DELETE, and various operators. It also includes information on database constraints, joins, and table alterations, along with examples for practical application. Students are encouraged to provide feedback and contact the instructors for assistance.

Uploaded by

ramkeltesfa246
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

College of natural and computational

science
Department of computer science Lab manual
for fundamental database Ⅰ

Dear Student
This manual covers all required steps and practical exercises on Microsoft SQL server. All
steps are necessary to accomplish one given task. Please perform without any exhaustion.
Dear our students We need your suggestions and comments as well questions and problems
which could be included in this lab module. In general, speaking it is open for advanced.
The module is prepared as a beginner and We are your students as far as we accept your
interests and finally thank you for all.

Be free to Contact as by the following Address


1. Ayenew K. (BSc)
Gmail: ayu21kassa@[Link]
2. Tegbaru N. (BSc)
Gmail: [Link]@[Link]
3. Hulunayehu M. (BSc)
Gmail: hulumolla1@[Link]
SQL command

✓ To create DATABASE
• The SQL CREATE DATABASE statement is used to create a new SQL
database.
SYNTAX
Create database database_name
E.g

• SQL ─ DROP or DELETE Database


Syntax
• DROP DATABASE DatabaseName;

E.g drop database injibara


• SQL ─ select database, use statement
Syntax
USE DatabaseName;
Always the database name should be unique within the RDBMS.

E.g use injibara


✓ To create TABLE
• Creating a basic table involves naming the table and defining its columns
and each column's data type.
✓ SYNTAX
Create table table_name(
Colomn 1 data type,
Colomn 2 data type,
..
..
)
E.g
create table course(
cname char(23),
cid int primary key,
chours int)
drop table course
create table has(
id int references student(id),
cid int references course(cid)
)

✓ SQL ─ DROP or DELETE Table


Syntax
DROP TABLE table_name;
E.g
drop table student
✓ The SQL INSERT INTO Statement

• The INSERT INTO statement is used to insert new records in a table.

✓ INSERT INTO Syntax


• It is possible to write the INSERT INTO statement in two ways.
• The first way specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Example
insert into student(name,sex,age)values('hh','m',45)

✓ The INSERT INTO syntax would be as follows:


• If you are adding values for all the columns of the table, you do not need to
specify the column names in the SQL query.
• However, make sure the order of the values is in the same order as the columns
in the table.
INSERT INTO table_name VALUES (value1, value2, value3, ...);

Example
insert into student values('ff','f',55)
insert into student values('ff','f',55)

✓ The SQL UPDATE Statement

• The UPDATE statement is used to modify the existing records in a table.


✓ UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example
update student set id=6 where id =5

✓ The SQL DELETE Statement


• The DELETE statement is used to delete existing records in a table.
• DELETE Syntax
DELETE FROM table_name WHERE condition;
Example
delete from student where cid

✓ The SQL IN Operator


The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.

✓ IN Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

or:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

✓ The SQL BETWEEN Operator

The BETWEEN operator selects values within a given range. The values can
be numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.

✓ BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example

select *from student where id in(1,2,4,5,12)


select *from student where id in(select from course)
select *from student where id between 1 and 11
✓ SQL Aliases

SQL aliases are used to give a table, or a column in a table, a temporary name.
Aliases are often used to make column names more readable.
An alias only exists for the duration of the query.
✓ Alias Column Syntax
SELECT column_name AS alias_name
FROM table_name;

✓ Alias Table Syntax


SELECT column_name(s)
FROM table_name AS alias_name;

✓ The SQL AND, OR and NOT Operators


• The WHERE clause can be combined with AND, OR, and NOT operators.
• The AND and OR operators are used to filter records based on more than one
condition:
• The AND operator displays a record if all the conditions separated by AND are
TRUE.
• The OR operator displays a record if any of the conditions separated by OR is
TRUE.
The NOT operator displays a record if the condition(s) is NOT TRUE.
✓ AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
✓ OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

✓ NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

✓ The SQL ORDER BY Keyword

• The ORDER BY keyword is used to sort the result-set in ascending or


descending order.
• The ORDER BY keyword sorts the records in ascending order by default. To
sort the records in descending order, use the DESC keyword.

✓ ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Example
select id from student where [Link] <4 or id>4 order by id desc

✓ The SQL MIN() and MAX() Functions


• The MIN() function returns the smallest value of the selected column.
• The MAX() function returns the largest value of the selected column.
✓ MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;

✓ MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;

✓ The SQL COUNT(), AVG() and SUM() Functions


The COUNT() function returns the number of rows that matches a specified
criteria.
The AVG() function returns the average value of a numeric column.
The SUM() function returns the total sum of a numeric column.
✓ COUNT() Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;

✓ AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;
✓ SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;

Example

select min(id) as ee from student


select max(age) from student
select count(name) from student
select sum(id) from student

✓ The SQL LIKE Operator


The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:

• % - The percent sign represents zero, one, or multiple characters


• _ - The underscore represents a single character

✓ Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern
Example

select * from course where cname like'd%d'

✓ SQL ─ Constraints
• Constraints are the rules enforced on the data columns of a table. These are used
to limit the type of data that can go into a table.
• This ensures the accuracy and reliability of the data in the database.
• Constraints could be either on a column level or a table level.
• The column level constraints are applied only to one column, whereas the
table level constraints are applied to the whole table.
✓ Following are some of the most commonly used constraints available in SQL.
• . NOT NULL Constraint: Ensures that a column cannot have a NULL value
• DEFAULT Constraint: Provides a default value for a column when none is
specified.
• UNIQUE Constraint: Ensures that all values in a column are different.
• PRIMARY Key: Uniquely identifies each row/record in a database table.
• FOREIGN Key: Uniquely identifies row/record in any of the given database
tables.
• CHECK Constraint: The CHECK constraint ensures that all the values in a
column satisfy certain conditions.
• INDEX: Used to create and retrieve data from the database very quickly

Example

create table student(


name char(33) not null,
id int identity(4,4) primary key,
sex char(3) check (sex='m'or sex='f'),
age int default(34))
drop table student
drop database injibara
create table course(
cname char(23),
cid int primary key,
chours int)
create table has(
id int references student(id),
cid int references course(cid)
)

✓ SQL ─ Using Joins


• The SQL Joins clause is used to combine records from two or more tables in a
database.
• A JOIN is a means for combining fields from two tables by using values
common to each.
✓ There are different types of joins available in SQL:
• INNER JOIN: returns rows when there is a match in both tables.
• LEFT JOIN: returns all rows from the left table, even if there are no matches in
the right table.
• RIGHT JOIN: returns all rows from the right table, even if there are no matches
in the left table.
• FULL JOIN: returns rows when there is a match in one of the tables.

Example

select * from student inner join course on cid=id


select * from course inner join student on cid =id
select * from course left join student on cid =id
select * from student right join course on cid=id
select *from student full outer join course on cid=id
✓ SQL ALTER TABLE Statement
• The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table.
• The ALTER TABLE statement is also used to add and drop various constraints on
an existing table.
✓ ALTER TABLE - ADD Column
• To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype;
Example
ALTER TABLE student
ADD Email varchar(255);
✓ ALTER TABLE - DROP COLUMN
• To delete a column in a table, use the following syntax (notice that some database
systems don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name;
The following SQL deletes the "Email" column from the "student table:
Example
ALTER TABLE student
DROP COLUMN Email;
✓ ALTER TABLE - ALTER/MODIFY COLUMN
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
ALTER COLUMN column_name datatype;
ALTER TABLE student
ALTER COLUMN name varchar(90);
Example

create database Injibara


use injibara
create table student(
name char(33) not null,
id int identity(4,4) primary key,
sex char(3) check (sex='m'or sex='f'),
age int default(34))
drop table student
drop database injibara
create table course(
cname char(23),
cid int primary key,
chours int)
drop table course
create table has(
id int references student(id),
cid int references course(cid)
)
drop table has
select top 3 *from student where not id<6
select cid from course
select * from course,student where cid=88
--insert data
insert into student(name,sex,age)values('hh','m',45)
insert into student values('ff','f',55)
insert into student values('ff','f',55)

insert into student values ('tt',5,'m',44)


insert into course values ('java',099,4)
insert into course values('drrd',0858,3)
insert into course values('c++',045,2)
insert into course values('ne',05,6)
--retraive,agragation function and join
select * from student inner join course on cid=id
select * from course inner join student on cid =id
select * from course left join student on cid =id
select * from student right join course on cid=id
select *from student full outer join course on cid=id

select *from student t1 union all select *from student t2


select id from student where id between 1 and 7
select id from student where [Link] <4 or id>4 order by id desc

select min(id) as ee from student


select max(age) from student
select count(name) from student
select sum(id) from student

select *from student where id in(1,2,4,5,12)


select *from student where id in(select from course)
select *from student where id between 1 and 11
select * from course where cname like'd%d'
select name,age as dd from student as lookd
select name,age from student where EXISTS(select *from course)
select name,age from student where id = any (select cid from course where cid=4)
select name,age from student where id = all(select cid from course where cid in(1,2,3,12))
select *into t2 from student
select *into t3 from student where 1=0
select *from t3
insert into t3 select*from t2
insert into t3(name,id,sex)select name,id,sex from t2
--database backup
backup database uniond to disk ='E:/uniond.'
backup database uniond to disk='E:/uniondr.' with differential
--update
update student set id=6 where id =5
--delete
delete from course where cid in(45,99)
--alter
--add column
alter table student add birthdate date
--delete column
alter table student drop column birthdate
--change datatype
alter table student alter column name varchar(34)
--add constraint
alter table student alter column age int not null
alter table student alter column name char(33) unique
--add and drop primary key
alter table student drop constraint primary key(id)
alter table student add primary key(id)
alter table student add constraint pkostudent primary key(id,name)
-- create index
create index index_v on student (id,name)
select *from index_v
--veiw
create view hulu as select id, name from student where name='ff';
select *from hulu
drop view hulu
--login
create login arb with password ='223366'
create user tew for login arb
grant select on student to tew
grant select,update,insert,delete on student to tew

You might also like