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

Sample DB

database lab work

Uploaded by

Bayee Atnafu
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)
22 views

Sample DB

database lab work

Uploaded by

Bayee Atnafu
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/ 5

Lab worksheet for twoday lesson

Data definition Language comands (create, alter, drop)

create database dmiot// used to create new data base

use dmiot// this statment used to refere our dara base

CREATE TABLE Persons (

ID int NOT NULL UNIQUE PRIMARY KEY, // to creates a NOT NULL UNIQUE constraint

on the a single column "ID" column when the "Persons" table is created

LastName varchar(255) NOT NULL,

FirstName varchar(255) NOT NULL,

Age int

);

create table course1(cid varchar(20) unique , lec_name varchar(10),dept varchar(20))// used to create new table

alter table course1 add chrs varchar(12)// this column used to add new column in our table

alter table course drop column lec_name// his statment used to drop lec-name column from our table

alter table course add address varchar(10) // to add column address

ALTER TABLE Persons ALTER COLUMN Age int NOT NULL;

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT UC_Person UNIQUE (ID,LastName) // to define a UNIQUE constraint on
multiple columns use this command
);
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)// to defining a PRIMARY
KEY constraint on multiple columns
);

ALTER TABLE Persons ADD PRIMARY KEY (ID); // for single column

ALTER TABLE Persons DROP PRIMARY KEY;

ALTER TABLE Persons ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName);//for


multiple col

ALTER TABLE Persons DROP CONSTRAINT PK_Person;

CREATE TABLE Orders (


OrderID int NOT NULL PRIMARY KEY,
OrderNumber int NOT NULL,
PersonID int FOREIGN KEY REFERENCES Persons(PersonID)//create FOREIGN key for
single column
);

ALTER TABLE Orders ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

ALTER TABLE Orders DROP CONSTRAINT FK_PersonOrder

CREATE TABLE Orders (


OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
//multiple column
);

ALTER TABLE Orders ADD CONSTRAINT FK_PersonOrder


FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)// to define check constraint in single column
);

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255),
CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes')// defining
a CHECK constraint on multiple columns

);

ALTER TABLE Persons ADD CHECK (Age>=18);

ALTER TABLE Persons DROP CONSTRAINT CHK_PersonAge;

ALTER TABLE Persons


ADD CONSTRAINT CHK_PersonAge CHECK (Age>=18 AND City='Sandnes');

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);

ALTER TABLE course ALTER COLUMN lec_name SET DEFAULT 'abebe' // to add a defoult values for a
column lec_name

alter table course drop column lec_name // to drop column lec_name

ALTER TABLE course ALTER COLUMN lec_name DROP DEFAULT //to drop default values from column
lec_name

ALTER TABLE course ADD CHECK (age>10) //to add check constraint…but you should adding age column in
the course table

ALTER TABLE course DROP CONSTRAINT chk_course //to drop cheack constraint

alter table course alter column age varchar(10) //to change data types

drop database dmu //this statment used to drop the entire dmu database

drop table course1 // this statment used to drop the entire course1 table

sp_rename ‘course1’,'cour'//his statment used to change our table name

EXEC sp_renamedb 'dmiot', 'dmu'// this statment used to change our data base names

Data manupulation language comands(Insert, Delete and Update)

insert into course1 values('1220','chala','SE')// his statment used to insert new data into our table

insert into course1 values('2022','alemu','IT')

insert into course1 values('2025','alemu','SE')

update course1 set lec_name='alemu' where cid='1220'//this statment used to change the course1 table by changing

chala by alemu

delete from course1 where cid='1220' this statment used to delete data from course1 table where cid=1220

delete * from course1 // this statment used to delete all records from course1 table

Data Query language comands(Select)


select* from course1 where cid='1220' // this statment used to display only one rows which contains cid 1220

select* from course1 //this statment used to display all records in student table

Select top 2 cid From course1 // this statment used to display only top two records from cid column

Backup and Restor

backup database dmiot TO DISK ='C:\ SE.ldf'// this statment used to take back-up data and put in local C

restore database dmiot from DISK ='C:\ SE.ldf' //this statment used to recover/restore the lost data base

You might also like