0% found this document useful (0 votes)
8 views18 pages

1602048262data Defination Language

mvc

Uploaded by

hayatsafi077
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)
8 views18 pages

1602048262data Defination Language

mvc

Uploaded by

hayatsafi077
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
You are on page 1/ 18

Lecture4

Data Definition Language

By Habibi 1
Database Creation
 before creating .MDF and .LDF files by explicitly
we should need to follow the following properties:
 Name: it will represent the logical names of the file
(.MDF and .LDF) and these names should be
different.
 File Name: it will represent the path location of the
files that means anywhere we can store the files.
 Size: it will represent the initial size of the file.
 Maxsize: it will represent the maximum size of the
file.
 File Growth: it will represent the growth of the file
(1 MB), it means that will plus 1 with minimum size
for every single entry in the database.
By Habibi 2
 Example:

 Name=’XYZ’

 File Name=’E: \......’

 Size=8MB

 Maxsize=15MB

 File Growth=1MB

 Syntax: Create database <database name>

 On (<set data file properties>)

 Log on (<set log file properties>)


By Habibi 3
create database Test
on
(
name='Test',
filename='D:\Test.mdf',
size=50mb,
maxsize=100mb,
filegrowth=3mb
)
log on
(
name='Testlog',
filename='D:\Testlog.ldf',
size=55mb,
maxsize=110mb,
filegrowth=2mb
)
By Habibi 4
2- Altering the Database
 Altering Database is nothing but to change or modify
the structure of Database and contain the following
steps.
A-Changing the Size of .MDF and .LDF Files:
 Syntax: alter database <database name> modify
file (set new size of data log files)
 Example1: alter database MyDemo modify file
(name=’XYZ’, size=10MB, Maxsize=40MB,
filegrowth=3MB)
 Example2: alter database MyDemo modify file
(name=’ABC’, size=8MB, Maxsize=30MB)
By Habibi 5
B: -Adding the new .MDF and .LDF Files to an
existing Database (Secondary File):
 Syntax: alter database <database name> add file
(<set data file properties>)
 Alter database <database name> add log file (<set
data file properties>)
 Example1: alter database MyDemo add file
(Name=’PQR’,file name=’D:\files\MyDemo1.MDF’,
size=6MB, Maxsize=50MB, file growth=2MB)
 Example2: alter database MyDemo add file
(Name=’MNO’,filename=’D:\files\MyDemo1_log.L
DF’, size=5MB, Maxsize=25MB, file growth=2MB)

By Habibi 6
C-Remove .MDF and .LDF Files from an
existing Database:
 Syntax: alter database <database name>
remove file <logical name>
 Example1: alter database MyDemo remove file
XYZ or ABC------>we cannot remove primary
files, but it can be removed when database is
removed from server.
 Exmaple2: alter database MyDemo remove file
MNO or PQR------> we can remove secondary
files from a database.

By Habibi 7
D-Modify the Database Name:
 Syntax: alter database <old database name>
modify name <new database name>
 Example: alter database MyDemo modify
name=MyOne
 Note: we can change database name but we cannot
change the file names
E-Dropping a Database: - when we drop a database
then all the associated files are dropped from database
permanently.
 Syntax: drop database <database name>
Example: drop database MyOne

By Habibi 8
Table Creation
 In Database every object will save with an extension of
dbo. <Object name> such as dbo. Employee,
 Syntax: create table <Table Name> (<column-
name1><data type>[size], <column_name2><data
type>[size])
 Create table students (stdID int, SName char(10) SFee
decimal (6,2), Age tinyint)
 Syntax to view the structure of a table is: SP_HELP
Students
By Habibi 9
Rules for creating a new table in Database
1. The table name should be Unique within the same
Database.
2. The column name should be unique within the table
definition.
3. The table name should not start with numbers and
special characters except under score _.
4. Don’t provide the space in the table name if you want
to provide a space in the table name then we use _
symbol.
5. Don’t use the Reserved keywords (insert, select,
update) as a table name.
6. A table name should contain minimum 1 character
and maximum 128 characters only.
7. A table should contain minimum 1 column and
maximum 1024 columns only.
By Habibi 10
Alter the table
To change the structure of a table, Alter command
perform the following 4 operations on an existing table :

1. Changing a Data Type

2. Changing the size of Data Type of a particular column.

3. Adding the new column to an existing table.

4. Changing the column name

5. Dropping UN wanted columns from an existing table.

By Habibi 11
Cont…

1- Alter-Alter Column: it will use to change the Data


Type from old Data Type to a New Data Type, and also
changing the size of Data Type of a column.

 Syntax: Alter table<table name> Alter column


<column name> <new Data Type> [size]

 Example: Alter table students alter column sname


varchar (50)

By Habibi 12
Cont.…..
2- Alter Table: this command will use to add the new
columns to the table.

 Syntax: Alter table <table name> add <column


name> <Data Type> [size]

 Example for single column: alter table students add


SADD varchar (20)

 Example for multiple columns: alter table students


add SMBNO int, SCID int
By Habibi 13
SP_Rename
 it is used to change the column and table names .
 Here SP stands for Stored Procedure. It has 2 sub types
those are:
1- Changing a column name:
 Syntax: SP_Rename ‘<table name>.<old column
name’,’<new column name>’
 Example: SP_Rename ‘students.sname’, ‘student
name’
2- Changing a table name: Syntax: SP_Rename ‘<old
table name>’,’<new table name>’
 Example: SP_Rename ‘students’,’stud’
By Habibi
Alter Drop
 It will drop UN Wanted columns from an existing table.

 Syntax: Alter table <table name> drop column


<column name>

 Example for single column:

alter table students drop column SADD

 Example for multiple columns:

alter table students drop column SMBNO, SCID

By Habibi 15
C-Truncate
 it is used to delete all rows from the table permanently
and cannot restore the dropped rows.
 By using truncate command we cannot delete a specific
row from a table because the truncate command does not
support ‘Where’ key word.
 It will drop all rows form a table but cannot drop the
structure of the table.
 Syntax: truncate table <table name>
 Example: truncate table students
By Habibi 16
D-Drop
 it will drop the entire structure of the table from the
database permanently and cannot restore the structure
of the table.

 Syntax: drop table <table name>

 Example: drop table students

By Habibi 17
Any Questions & Suggestions

By Habibi 18

You might also like