Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

SHRI YOGINDRA SAGAR INSTITUTE OF

TECHNOLOGY & SCIENCE

PRACTICAL JOURNAL
OF
DATABASE MANAGEMENT SYSTEM
CS-502

Submitted To: Submitted By:


Prof. Imran Hussain Deepraj chouhan
HOD E.N. 0718CS191011
CSE Dept. V Sem CSE Dept.
Index

Sr. No Name Of Experiment Signature

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
PRACTICAL: 1

Aim: To study CREATE TABLE Syntax

CREATE TABLE creates a table with the given name.

A) FOR GENERAL TABLE CREATION


Create Table TableName(fieldname datatype,fieldname1 datatype,…)
Example: CREATE TABLE Student (StudentId int(6) not null default 0,Student
varchar(25) not null default ‘’, PrimaryKey(StudentId));

B) FOR CREATING TABLE USING AN EXISTING TABLE(ALONG WITH DATA)


Create Table TableName AS Select * from ExistingTableName
Example: CREATE TABLE StudentEmail AS SELECT * FROM PersonEmail;

C) FOR CREATING TABLE USING AN EXISTING TABLE(WITHOUT DATA)


Create Table TableName like ExistingTableName
Example: CREATE TABLE StudentEmail like PersonEmail;
PRACTICAL: 2

Aim: To study ALTER TABLE Syntax

ALTER TABLE enables you to change the structure of an existing table. For example, you can add or
delete columns, change the type of existing columns, or rename columns or the table itself.
A) TO ADD A NEW COLUMN
a) To add the column as the first column of the table.
Alter Table TableName add NewColumnName datatype FIRST
Example: Alter table Student add School varchar(30) not null default ‘’ FIRST

b) To add the column after a particular column of the table.


Alter Table TableName add NewColumnName datatype AFTER ExistingColumnName
Example: Alter table Student Add School varchar(30) not null default ‘’ AFTER Student

B) TO DELETE A COLUMN
Alter Table TableName DROP ColumnName
Example: Alter table Student DROP School

C) TO RENAME A COLUMN

Alter Table TableName CHANGE OldColumnName NewColumnName datatype


Example: Alter table Student change School SchoolName varchar(30) not null default ‘’;

D) TO MODIFY A COLUMN DATA-TYPE/SIZE


Alter Table TableName modify ColumnName datatype
Example: Alter table Student modify School varchar(50) not null default ‘’;

E) TO MAKE A COLUMN UNIQUE

a) To make a single column unique.


Alter Table TableName add UNIQUE(fieldname)
Example: Alter table Student add UNIQUE(School);
b) To make multiple columns unique.
Alter Table TableName add UNIQUE(fieldname1, fieldname2, fieldname3)

Example: Alter table Ashtadhyayee add UNIQUE(Bhav, Paad, Sootra);


PRACTICAL: 3

Aim: To study RENAME TABLE Syntax


This statement renames one or more tables.

Rename OldTableName to NewTableName;


Example: Rename Student to StudentDetail
PRACTICAL: 4

Aim: To study DROP TABLE Syntax


This statement will allow deleting a table from the database

DROP table TableName;


Example: DROP table StudentDetail
PRACTICAL: 5

Aim: To study MYSQL COMMAND REFERENCE (1)

A) TO SEE ALL THE TABLES IN THE DATABASE


SHOW tables;

B) TO SEE DATABASE'S FIELD FORMATS.


Desc tablename;
PRACTICAL: 6

Aim: To study INSERT Syntax


INSERT inserts new rows into an existing table
a) To Insert data in all columns of a table.
INSERT INTO TableName values ()
Example: Insert into StudentDetail values(1,’Ravi’,’WoodBine’),;

b) To Insert data in a column(s) of the table.


INSERT INTO TableName (ColName1, ColName2,..) values(val1,val2,..)
Example: Insert into StudentDetail (StudentId, StudentName) values(2,’Shyaam’),;

c) To Insert data into a table from another table.


INSERT INTO TableName (ColName1,ColName2) select ColName1,ColName2 From TableName2.
Example: Insert into StudentName (StudentName) select Name from NameLog;
PRACTICAL: 7

Aim: To study SELECT Syntax


SELECT is used to retrieve rows selected from one or more tables

A) To Select all data from the table


SELECT * FROM TABLENAME;
Example: Select * from StudentDetail;

B) To Select data of a particular Column from the table


SELECT ColumnName FROM TABLENAME;
Example: Select StudentName from StudentDetail;

C) To Select data based on a condition


SELECT * FROM TABLENAME where Condition;
Example: Select StudentName from StudentDetail where StudentId=2;

D) To Select limited number of Records at a time


SELECT * FROM TABLENAME LIMIT no.of records
Example: Select StudentName from StudentDetail LIMIT 10
PRACTICAL: 8

Aim: To study UPDATE Syntax


UPDATE statement updates columns of existing rows in TableName with new values. The SET
clause indicates which columns to modify and the values they should be given. The WHERE clause,
if given, specifies the conditions that identify which rows to update. With no WHERE clause, all
rows are updated.
A) To Update data based on a condition
UPDATE TableName SET ColumnName=’value’ where condition;

Example: UPDATE StudentDetail SET StudentName=’Rohan’ where StudentId=1;

B) To Update all records simultaneously


UPDATE TableName SET ColumnName=’value’ where condition
Example: UPDATE StudentDetail SET School=’Wendy High School’;
PRACTICAL: 9

Aim: To study DELETE Syntax


DELETE statement deletes rows from TableName and returns a count of the number of deleted
rows. The
WHERE clause, if given, specifies the conditions that identify which rows to delete. With no WHERE
clause, all rows are deleted.

A) To Delete data based on a condition


Delete fieldname from TableName where condition;
Example: Delete StudentName where StudentId=1;
B) To Delete all records simultaneously
Delete from TableName

Example: Delete * from StudentDetail


PRACTICAL: 10

Aim: To study MYSQL COMMAND REFERENCE (2)


1. Create Database Syntax:
Create database <db>
2. Drop Database Syntax:
Drop database <db>
3.1 Add Primary Key:

Alter table <table> add Primary Key <Primary Key Name> (<field1>,<field2>..)
3. 2 Add Unique Key:
Alter table <table> add Unique Key <Unique Key Name>
3.3 Drop Primary Key:
Alter table <table> drop Primary Key <Primary Key Name>

3. 4 Drop Unique Key:


Alter table <table> drop Unique Key <Unique Key Name>
3.5 Add Index:
During Table Creation:
CREATE TABLE Station (

StationId int(6)Primary Key,


StationCode VARCHAR(50,
Station VARCHAR(50),
INDEX (StationCode),
INDEX (Station),

);
{Note: For primary key and Unique Key Indices are created by default}
After Table Creation:
CREATE INDEX StationId ON Station(StationId)
3.6 Drop Index:

ALTER TABLE Station DROP INDEX StationId;


3.7 Show Index:
SHOW INDEX FROM table;
SHOW INDEX FROM table FROM dbname;
SHOW INDEX FROM dbname.table;

4.1 Show Active MySQL Connections:


show processlist;
4.2 Kill an Active MySQL Connections:
kill <id> ;
5. Syntax – TO SET THE FIRST CHARACTER IN UPPERCASE AND REST IN LOWERCASE

This command will set the first character of a varchar field in uppercase and the rest in lowercase.
Update TableName setFieldName=CONCAT(UPPER(SUBSTRING(FieldName,1,1)),
LOWER(SUBSTRING(FIELD NAME,2)));

e.g Update Pincode set


PinCodeLocality=CONCAT(UPPER(PinCodeLocality,1,1)),LOWER(PincodeLocality,2
)));

You might also like