Name: Rijna Shrestha Roll No.: 36: 1.short Note On DDL and DML
Name: Rijna Shrestha Roll No.: 36: 1.short Note On DDL and DML
Name: Rijna Shrestha Roll No.: 36: 1.short Note On DDL and DML
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:
Name varchar(50),
Address varchar(50),
Qualification varchar(50),
Age int,
Gender char(50),
)
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.
a column-level constraint
Table constraints have syntax and behavior similar to column constraints. Note
the following differences:
Table constraints must follow the definition of columns they refer to.
Syntax
table_constraint ::
| CHECK ( search_condition )
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
);
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.
);
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.
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!