Name: Rijna Shrestha Roll No.: 36: 1.short Note On DDL and DML

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Name: Rijna Shrestha

Roll No.: 36
1.Short note on DDL and DML:
Structured Query Language (SQL) is the database language by the use of
which we can perform certain operations on the existing database and also we
can use this language to create a database. SQL uses certain commands like
Create, Drop, Insert etc. to carry out the required tasks. Once data is stored, it
often needs to be modified. Data is inserted, updated and deleted. Commands
that create objects like tables are called SQL DDL commands. On the other hand,
commands that modify data are called SQL DML commands.
These SQL commands are mainly categorized into four categories DDL, DML, DCL
&TCL:
DDL:
DDL (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.
Examples of DDL commands:
o CREATE – is used to create the database or its objects (like table, index, function,
views, store procedure and triggers).
o DROP – is used to delete objects from the database.
o ALTER-is used to alter the structure of the database.
o TRUNCATE–is used to remove all records from a table, including all spaces
allocated for the records are removed.
o COMMENT –is used to add comments to the data dictionary.
o RENAME –is used to rename an object existing in the database.
Query for DDL:

o Create table: CREATE TABLE Tblscript

ID int not null identity(1,1),

Name varchar(50),

Address varchar(50),

Qualification varchar(50),

Age int,
Gender char(50),
)

o Alter table: ALTER TABLE Tblscript

ADD DOB date;

o Create View: SELECT * FROM Tblscript


DML:
The SQL commands that deals with the manipulation of data present in database
belong to DML or Data Manipulation Language and this includes most of the SQL
statements.
Examples of DML:
 SELECT – is used to retrieve data from a database.
 INSERT – is used to insert data into a table.
 UPDATE – is used to update existing data within a table.
 DELETE – is used to delete records from a database table.
Query of DML:
o Insert into table: INSERT INTO Tblscript
values ('Rita','Pokhara','SEE/10','16','Female')
INSERT INTO Tblscript
(Name,Address,Age,Gender)
values ('Sijal','Kathmandu','20','Male')
o Update table: select Name from Tblscript
update Tblscript set name='Jerry'
where name='Sijal'
2. Calculated Field:
A calculated field is a field for querying or outputting information that cannot
be directly queried or output from a database table. A computed column is a
virtual column that is not physically stored in the table, unless the column is
marked PERSISTED. A computed column expression can use data from other
columns to calculate a value for the column to which it belongs. The ability to
manipulate field values in query results without altering the data in the
database itself provides many significant benefits. A calculated field either
performs some calculation on database fields to create a value that is not
stored in the database or selects the value from a particular database field
based on customized criteria.
Some examples include:
1. Data can be stored with the appropriate data type
2. Formatting/conversion is generally faster through the RDBMS in comparison to
the application layer (i.e. PHP, Python)
Query for calculated field:
CREATE TABLE t1
(
COL1 int,
COL2 int,
COL3 int,
COL4 as (COL1*COL2*COL3)/3
)

Insert into t1 values(1,2,3)


select * from t1

ALTER TABLE dbo.tablename ADD Employment AS Select


((m1+m2+m3)/3)

Sample data

m1 20 20 30
m2 15 17 25
m3 60 77 13
desired result.

Name m1 m2 m3 Employment
Auto body 20 20 30 23
Auto Parts 15 17 25 19
Auto Sales 60 77 13 50

3. Check Constraints:
The CHECK Constraint enables a condition to check the value being entered into a
record. If the condition evaluates to false, the record violates the constraint and
isn't entered in the table. The CHECK constraint is used to limit the value range
that can be placed in a column. It performs check on the values, before storing
them into the database. Its like condition checking before saving data into a
column.
If you define a CHECK constraint on a single column it allows only certain values
for this column.

If you define a CHECK constraint on a table it can limit the values in certain
columns based on values in other columns in the row.

When this constraint is being set on a column, it ensures that the specified
column must have the value falling in the specified range.

Example:
CREATE TABLE STUDENT
(
ROLL_NO INT NOT NULL CHECK(ROLL_NO>1000),
STU_NAME VARCHAR(50) NOT NULL,
STU_AGE INT NOT NULL,
EXAM_FEE INT DEFAULT 10000,
STU_ADDRESS VARCHAR(50),
PRIMARY KEY(ROLL_NO)
)

In the above example we have set the check constraint on ROLL_NO column of
STUDENT table. Now, the ROLL_NO field must have the value greater than 1000.

4. Table level constraints:


A CONSTRAINT can be one of the following:

 a column-level constraint

Column-level constraints refer to a single column in the table and do not


specify a column name (except check constraints). They refer to the column
that they follow.
 a table-level constraint

Table-level constraints refer to one or more columns in the table. Table-level


constraints specify the names of the columns to which they apply. Table-level
CHECK constraints can refer to 0 or more columns in the table.

Table constraints have syntax and behavior similar to column constraints. Note
the following differences:

 The syntax for table constraints is separated from column definitions by


commas.

 Table constraints must follow the definition of columns they refer to.

 Table constraint definitions can include more than one column.

Syntax
table_constraint ::

PRIMARY KEY ( column [, ... ] )

| UNIQUE ( column [, ... ] )

| FOREIGN KEY ( column [, ... ] )

REFERENCES [ owner_name. ] table_name [ ( column [, ... ] ) ]

| CHECK ( search_condition )

Table constraints include:

1.PRIMARY KEY:

Specifies the column or columns that uniquely identify a row in the table. NULL
values are not allowed. The following example shows creation of a table-level
primary key. Note that its definition is separated from the column definitions by a
comma:
CREATE TABLE supplier_item

supp_no INTEGER NOT NULL,

item_no INTEGER NOT NULL,

qty INTEGER NOT NULL DEFAULT 0,

PRIMARY KEY (supp_no, item_no)

);

2.UNIQUE:

Specifies that values in the columns must be unique.You can specify more than
one UNIQUE table-level constraint in a table definition.

The following example shows creation of a table with two UNIQUE table-level


constraints:

CREATE TABLE order_item (

order_no INTEGER NOT NULL,

item_no INTEGER NOT NULL,

qty INTEGER NOT NULL,

price MONEY NOT NULL,

UNIQUE (order_no, item_no),

UNIQUE (qty, price)

);
3.FOREIGN KEY:

Specifies that the values in the columns must correspond to values in referenced
primary key or unique columns or that they are NULL.

Note: If the foreign key consists of multiple columns, and any column is NULL, the
whole key is considered NULL. The insert is permitted no matter what is on the
non-null columns.

4.CHECK: Specifies a wide range of rules for values in the table.

Search Condition:

[CONSTRAINT constraint-Name]

{
CHECK (searchCondition) |
{
PRIMARY KEY ( Simple-column-Name [ , Simple-column-Name ]* ) |
UNIQUE ( Simple-column-Name [ , Simple-column-Name ]* ) |
FOREIGN KEY ( Simple-column-Name [ , Simple-column-Name ]* )
REFERENCES clause
}
}
-- the table-level primary key definition allows you to
-- include two columns in the primary key definition:
CREATE TABLE SAMP.SCHED
(
CLASS_CODE CHAR(7) NOT NULL,
DAY SMALLINT NOT NULL,
STARTING TIME,
ENDING TIME,
PRIMARY KEY (CLASS_CODE, DAY)
);
-- Use a table-level constraint
-- to make sure that a employee's taxes does not
-- exceed the bonus
CREATE TABLE SAMP.EMP
(
EMPNO CHAR(6) NOT NULL CONSTRAINT EMP_PK PRIMARY KEY,
FIRSTNME CHAR(12) NOT NULL,
MIDINIT vARCHAR(12) NOT NULL,
LASTNAME VARCHAR(15) NOT NULL,
SALARY DECIMAL(9,2) CONSTRAINT SAL_CK CHECK (SALARY >= 10000),
BONUS DECIMAL(9,2),
TAX DECIMAL(9,2),
CONSTRAINT BONUS_CK CHECK (BONUS > TAX)
);
THANKYOU!

You might also like