0% found this document useful (0 votes)
6 views8 pages

Lab DDL

Structured Query Language (SQL) is a standard database query language used for managing data in Relational Database Management Systems (RDBMS). SQL allows for various operations such as creating, retrieving, updating, and deleting data, and includes different types of statements like Data Definition Language (DDL) for defining database structures. The document details SQL commands, data types, and examples for creating and modifying database objects.

Uploaded by

Tesfalegn Yakob
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)
6 views8 pages

Lab DDL

Structured Query Language (SQL) is a standard database query language used for managing data in Relational Database Management Systems (RDBMS). SQL allows for various operations such as creating, retrieving, updating, and deleting data, and includes different types of statements like Data Definition Language (DDL) for defining database structures. The document details SQL commands, data types, and examples for creating and modifying database objects.

Uploaded by

Tesfalegn Yakob
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/ 8

STRACTURED QUERY LANGUAGE DDL CHAPTER 7

Structured query language


Structure Query Language (SQL) is a database query language used for storing and managing
data in Relational DBMS. SQL was the first commercial language introduced for E.F
Codd's Relational model of database. Today almost all RDBMS (MySql, Oracle, Infomix,
Sybase, MS Access) use SQL as the standard database query language. SQL is used to perform
all types of data operations in RDBMS. Relational DBMS - this type of DBMS defines database
relationships in form of tables, also known as relations. Unlike network DBMS, RDBMS does
not support many to many relationships. Relational DBMS usually have pre-defined data types
that they can support. This is the most popular DBMS type in the market. Structured Query
language (SQL) pronounced as "S-Q-L" or sometimes as "See-Quel"is actually the standard
language for dealing with Relational Databases. SQL stands for Structured Query Language.
SQL lets you access and manipulate databases. SQL became a standard of the American
National Standards Institute (ANSI) in 1986, and of the International Organization for
Standardization (ISO) in 1987

What Can SQL do?

 SQL can execute queries against a database


 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database
 SQL can create stored procedures in a database
 SQL can create views in a database
 SQL can set permissions on tables, procedures, and views

There are different types of SQL statements. They are:

 DATA DEFINITION LANGUAGE (DDL)


 DATA MANIPULATION LANGUAGE (DML)
 DATA RETRIEVAL LANGUAGE (DRL)
 TRANSATIONAL CONTROL LANGUAGE (TCL)
 DATA CONTROL LANGUAGE (DCL)
SQL Server Data Types

The data type of a column defines what value the column can hold: integer, character, money,
date and time, binary, and so on. Each column in a database table is required to have a name and

1
STRACTURED QUERY LANGUAGE DDL CHAPTER 7

a data type. An SQL developer must decide what type of data that will be stored inside each
column when creating a table. The data type is a guideline for SQL to understand what type of
data is expected inside of each column, and it also identifies how SQL will interact with the
stored data. The following tables describes data types which are used in the sql server.

Data type Description Max size Storage


char(n) Fixed width character string 8,000 characters Defined width

varchar(n) Variable width character 8,000 characters 2 bytes + number


string of chars
varchar(max) Variable width character 1,073,741,824 2 bytes + number
string characters of chars
text Variable width character 2GB of text 4 bytes + number
string data of chars
nchar Fixed width Unicode string 4,000 characters Defined width x 2
nvarchar Variable width Unicode 4,000 characters
string
nvarchar(max) Variable width Unicode 536,870,912
string characters
ntext Variable width Unicode 2GB of text
string data
binary(n) Fixed width binary string 8,000 bytes
varbinary Variable width binary string 8,000 bytes
varbinary(max) Variable width binary string 2GB

Number data types:


Data type Description Storage
bit Integer that can be 0, 1, or NULL
tinyint Allows whole numbers from 0 to 255 1 byte
smallint Allows whole numbers between -32,768 and 32,767 2 bytes
int Allows whole numbers between -2,147,483,648 and 4 bytes
2,147,483,647
bigint Allows whole numbers between - 8 bytes
9,223,372,036,854,775,808 and 9,223,372,036,854,775,807
decimal(p,s) Fixed precision and scale numbers. 5-17
Allows numbers from -10^38 +1 to 10^38 –1. bytes
The p parameter indicates the maximum total number of
digits that can be stored (both to the left and to the right of
the decimal point). p must be a value from 1 to 38. Default
is 18.
The s parameter indicates the maximum number of digits
stored to the right of the decimal point. s must be a value
from 0 to p. Default value is 0

2
STRACTURED QUERY LANGUAGE DDL CHAPTER 7

numeric(p,s) Fixed precision and scale numbers. 5-17


Allows numbers from -10^38 +1 to 10^38 –1. bytes
The p parameter indicates the maximum total number of
digits that can be stored (both to the left and to the right of
the decimal point). p must be a value from 1 to 38. Default
is 18.
The s parameter indicates the maximum number of digits
stored to the right of the decimal point. s must be a value
from 0 to p. Default value is 0
smallmoney Monetary data from -214,748.3648 to 214,748.3647 4 bytes
money Monetary data from -922,337,203,685,477.5808 to 8 bytes
922,337,203,685,477.5807
float(n) Floating precision number data from -1.79E + 308 to 1.79E 4 or 8
+ 308. bytes
The n parameter indicates whether the field should hold 4 or
8 bytes. float(24) holds a 4-byte field and float(53) holds an
8-byte field. Default value of n is 53.
real Floating precision number data from -3.40E + 38 to 3.40E + 4 bytes
38

Date data types:


The DATE data type has ten positions, and its components are YEAR, MONTH, and DAY in
the form YYYY-MM-DD. The TIME data type has at least eight positions, with the components
HOUR, MINUTE, and SECOND in the form HH:MM:SS. Only valid dates and times should be
allowed by the SQL implementation. This implies that months should be between 1 and 12 and
dates must be between 1 and 31; furthermore, a date should be a valid date for the corresponding
month.
Data type Description Storage
datetime From January 1, 1753 to December 31, 9999 with 8 bytes
an accuracy of 3.33 milliseconds
datetime2 From January 1, 0001 to December 31, 9999 with 6-8 bytes
an accuracy of 100 nanoseconds
smalldatetime From January 1, 1900 to June 6, 2079 with an 4 bytes
accuracy of 1 minute
date Store a date only. From January 1, 0001 to 3 bytes
December 31, 9999
time Store a time only to an accuracy of 100 3-5 bytes
nanoseconds
datetimeoffset The same as datetime2 with the addition of a time 8-10 bytes
zone offset
timestamp Stores a unique number that gets updated every
time a row gets created or modified. The timestamp
value is based upon an internal clock and does not
correspond to real time. Each table may have only

3
STRACTURED QUERY LANGUAGE DDL CHAPTER 7

one timestamp variable

Lab Experment 1
DDL(Data Definition Language) : DDL or Data Definition Language actually consists of the
SQL commands that can be used to define the database schema. It simply deals with descriptions
of the database schema and is used to create and modify the structure of database objects in
database. DDL is used to create and destroy databases and database objects. These commands
will primarily be used by database administrators during the setup and removal phases of a
database project.
Examples of DDL commands:
 CREATE – is used to create the database or its objects (like table, index, function, views,
store procedure and triggers).
 DROP – is used to delete objects from the database.
 ALTER-is used to alter the structure of the database.
 COMMENT –is used to add comments to the data dictionary.
 RENAME –is used to rename an object existing in the database
The Data Definition Language (DDL) is used to create and destroy databases and database
objects. These commands will primarily be used by database administrators during the setup and
removal phases of a database project.
Let's take a look at the structure and usage of four basic DDL commands:
1. CREATE 2. ALTER 3. DROP 4. RENAME
1. CREATE:
CREATE DATABASE: A Database is defined as a structured set of data. So, in SQL the very
first step to store the data in a well-structured manner is to create a database. The CREATE
DATABASE statement is used to create a new database in SQL.
Syntax: CREATE database <databasename>
Example: CREATE DATABASE AMUDB OR CREATE DATABASE AMUDB;
If you want the name of the database to be in different words, include them in square brackets.
Here is an example:
CREATE DATABASE AMUDB OR CREATE DATABASE [AMU DB];
When you have multiple databases in your SQL Schema, then before starting your operation, you
would need to select a database where all the operations would be performed.
USE statement: is the SQL statement that used to select any existing database in SQL schema.
Syntax: USE Database_name;
Example: if you want to work with AMUDB database, then you can execute the following
SQL command and start working with AMUDB database.
USE AMUDB;

4
STRACTURED QUERY LANGUAGE DDL CHAPTER 7

CREATE TABLE: We have learned above about creating databases. Now to store the data we
need a table to do that. The CREATE TABLE statement is used to create a table in SQL. We
know that a table comprises of rows and columns. So while creating tables we have to provide all
the information to SQL about the names of the columns, type of data to be stored in columns,
size of the data etc. Let us now dive into details on how to use CREATE TABLE statement to
create tables in SQL
CREATE TABLE table_name
(
column1 data_type(size),
column2 data_type(size),
column3 data_type(size),
....
);
Where:-
table_name: name of the table.
Column1 name of the first column.
Data_type: Type of data we want to store in the particular column.
For example, int for integer data.
Size: Size of the data we can store in a particular column. For example if for a column we
specify the data_type as int and size as 10 then this column can store an integer number of
maximum 10 digits.
Example:
create table student (
stuId INT PRIMARY KEY, OR CONSTRAINT Student_stuId_pk PRIMARY KEY
(stuId),
lastName CHAR(20) NOT NULL,
firstName CHAR(20) NOT NULL,
sex CHAR(5),
);
Example 2.write sql query for employee relation schema
CREATE TABLE EMPLOYEE
( Fname VARCHAR(15) NOT NULL,
Minit CHAR,
Lname VARCHAR(15) NOT NULL,
Ssn CHAR(9) NOT NULL,
Bdate DATE,
Address VARCHAR(30),
Sex CHAR,
Salary DECIMAL(10,2),
Super_ssn CHAR(9),
Dno INT NOT NULL,

5
STRACTURED QUERY LANGUAGE DDL CHAPTER 7

PRIMARY KEY (Ssn),


FOREIGN KEY (Super_ssn) REFERENCES EMPLOYEE(Ssn),
);

2. ALTER
ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also
used to add and drop various constraints on the existing table and helps to change the width as
well as datatype of fields of existing relations
A. ALTER..ADD...: ADD is used to add columns into the existing table. Sometimes we
may require to add additional information, in that case we do not require to create the
whole database again, ADD comes to our rescue.
Syntax: ALTER TABLE table_name
ADD (Columnname_1 datatype (size),
Columnname_2 datatype,

Columnname_n datatype);
Example: alter table student ADD address varchar(23), department varchar(23);
B. ALTER..MODIFY...: This is used to change the width as well as data type of fields of
existing relations.
 Syntax; ALTER TABLE relation_name MODIFY COLUMN (field_1
newdata_type(Size), field_2 newdata_type(Size),....field_newdata_type(Size));
Example; ALTER TABLE student ALTER COLUMN address char(32);
C. ALTER ..DROP...: This is used to remove any field of existing relations and table.
 Syntax: ALTER TABLE relation_name
 Example; ALTER TABLE student DROP column department,(from student table)
3. DROP is used to delete database and its objects. This is used to delete the structure of a
relation. It permanently deletes the records in the table

A. DROP DATABASE
DROP DATABASE <DATABASENAME>
B. DROP TABLE.
Syntax: DROP TABLE relation_name;
Example: DROP TABLE student;
4. RENAME: It is used to change database, table name and column name.
Renaming a column name

6
STRACTURED QUERY LANGUAGE DDL CHAPTER 7

 Syntax: exec sp_rename ‘tablename.existing column',‘new column','COLUMN'


EXAMPLE: - exec sp_rename 'student1.AGE','STUDAGE','COLUMN'

Renaming a table name


Syntax: EXEC sp_rename ‘existingtable', ‘newtable'
Example EXEC sp_rename ‘student', ‘student12'

Renaming a database
 To change the name of a database, Transact-SQL provides sp_renamedb.
 The formula used would be: EXEC sp_renamedb 'ExistingName', 'NewName';
 The EXEC sp_renamedb expression is required.
 The ExistingName factor is the name of the database that you want to rename.
 The NewName factor is the name you want the database to have after renaming it.
Example: EXEC sp_renamedb 'existing db', 'new db';

LAB PRACTICE ASSIGNMENT 1 (submission date before or on next lab session)


1. Create database named [G2CSSTUDENT DB]
2. Use the database [G2CSSTUDENT DB] and do the following tasks

A. Create a table EMPLOYEE with following schema:


(Emp_no, E_name, E_ph_no, Dept_no, Dept_name , Salary)
B. Add a new column E_address, to the existing relation.
C. Change the datatype of E_name from char to varchar.
D. Change the name of column/field Emp_no to E_no.
E. Modify the column width of the E_name field of emp table
F. Change the name of database name into G2CSDB
G. Change the name of EMPLOYEE table into CS_ EMPLOYEE
H. Change the name of E_ph_no to phone
I. Delete Dept_name field from EMPLOYEE table
J. Delete CS_ EMPLOYEE from G2CSDB
K. Delete databse G2CSDB remember(while u want to delet the Database use another
database b/c if u are working on it,it will not be deleted )

7
STRACTURED QUERY LANGUAGE DDL CHAPTER 7

You might also like