Академический Документы
Профессиональный Документы
Культура Документы
1.Create:
- To make a new database, table, index, or stored procedure
CREATE TABLE statement
A commonly used CREATE command is the CREATE TABLE command. The typical usage is:
CREATE TABLE [table name] ( [column definitions] ) [table parameters].
column definitions: A comma-separated list consisting of any of the following
Column definition: [column name] [data type] {NULL | NOT NULL} {column options}
Primary key definition: PRIMARY KEY ( [comma separated column list] )
Constraints: {CONSTRAINT} [constraint definition]
RDBMS specific functionality
For example, the command to create a table named employees with a few sample columns would
be:
CREATE TABLE employees (
id
INTEGER
PRIMARY KEY,
first_name
VARCHAR(50) NULL,
last_name
VARCHAR(75) NOT NULL,
dateofbirth
DATE
NULL
);
2.Alter
Alter - To modify an existing database object.
An ALTER statement in SQL changes the properties of an object inside of a relational database
management system (RDBMS). The types of objects that can be altered depends on which
RDBMS is being used. The typical usage is:
ALTER objecttype objectname parameters.
For example, the command to add (then remove) a column named bubbles for an existing table
named sink would be:
ALTER TABLE sink ADD bubbles INTEGER; ALTER TABLE sink DROP COLUMN bubbles;
3.Drop
DML:
A data manipulation language (DML) is a family of syntax elements similar to a
computer Experimentming language used for inserting, deleting and updating data in a
database. Performing read-only queries of data is sometimes also considered a component of
DML.
A popular data manipulation language is that of Structured Query Language (SQL), which is
used to retrieve and manipulate data in arelational database.
Data manipulation language is used to append, change or remove data in a table.
In the case of SQL, the verbs are:
SELECT ... FROM ... WHERE ...
INSERT INTO ... VALUES ...
UPDATE ... SET ... WHERE ...
DELETE FROM ... WHERE ...
1. Select:
Select clause is used to list the attributes desired in the result of a query. It corresponds to the
projection operation of the relational algebra
Eg. select *from EMPLOYEE -all attributes
select fname, SSN from EMPLOYEE -only fname and SSN
2.Insert
A newly created relation is empty initially. We can use the insert command to load data into
the relation.
insert into <table name> values(A1,A2,An)
The values are specified in the order in which the corresponding attributes are listed in the
relation schema.
3. Update :
In certain situations we may wish to change a value in a tuple without changing all the values in
the tuple. For this purpose, the update statement can be used.
Eg. update EMPLOYEE set age=20
where SSN=514065
SQL provides a case construct which we can use to perform both the update with a single update
statement avoiding the problem with the order of updates.
Eg. update account set balance =case when balance<=1000 then balance*1.05 else balance*1.06
end
4. Delete:
To delete a tuple from relation r, we use the following command delete from r where r is the
name of the relation
Delete from pies
Where flavor=Lemon;
DDL
DML
CREATE - to create objects in database
SELECT - retrieve data from the a database
ALTER - alters the structure of database
INSERT - insert data into a table
DROP - delete objects from the database
UPDATE - updates existing data within table
TRUNCATE - remove all records from
DELETE - deletes all records from a table,
table, including all spaces allocated for
the space for the records remain
records are removed
o MERGE - UPSERT operation (insert/ update)
o COMMENT - add comments to data
o CALL - call a PL/SQL or Java subExperiment
dictionary
o EXPLAIN PLAN - explain access path to
o RENAME - rename an object
data
o
o
o
o
o
o
o
o
Experiment 2
Aim: To create Table queries using the following constraints
Primary Key constraint
Foreign Key constraint
Check Constraint
Unique Constraint
Not null constraint
Theory:
1.Primary key
The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary
keys must contain unique values.
A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have only ONE primary key.
2.Foreign Key
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
3. Check:
The check clause in SQL can be applied to relation declarations as
well to domain declarations when applied to a relation declaration, the clause check(p)
specified a predicate p that must be specified by every tuple in a relation. A common use of
the check clause is to ensure that the attribute value
satisfy specified condition.
4.Unique
The UNIQUE constraint uniquely identifies each record in a database table.
The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a
column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY
constraint per table.
5) Not Null:
The not null specification prohibits the insertion of a null value.
For a attribute any database modification that would cause null to be inserted in
an attribute declared to be not null generates an error diagnostic. If an attribute
is declared as the primary key then it cannot take a null value.
Query:
create table salary
(
id int primary key, salary float,
designation varchar(10) not null, check(salary>1000)
);
Output
Table created.
Query
Output
Table created.
Experiment 3
Aim: To perform the function of Insert statement in table.
Theory: The INSERT INTO statement is used to insert new records in a
table.
Query:
insert into salary values(1,2000,'accountant');
insert into salary values(2,1500,'driver');
ID
SALARY
DESIGNATION
2000
accountant
1500
driver
ID
SALARY
DESIGNATION
2000
accountant
1500
driver
10000
manager
ID
SALARY
DESIGNATION
2000
accountant
1500
driver
10000
manager
5000
developer
EMPID
NAME
CITY
EID
aman
delhi
2
EMPID
NAME
CITY
EID
aman
delhi
2
ankush
bihar
1
EMPID
NAME
CITY
EID
1
aman
delhi
2
2
ankush
bihar
1
3
aamna
bu.p.
4
Experiment 4
Aim:
To perform the function of update statement with and without WHERE clause
Theory:
The UPDATE statement is used to update existing records in a table. Notice the WHERE
clause in the SQL UPDATE statement!
The WHERE clause specifies which record or records that should be updated. If you omit the
WHERE clause, all records will be updated!
Query:
1.Using where clause:
UPDATE emp
SET name='akash', city='Hamburg'
WHERE name='aamna';
EMPID
NAME
CITY
EID
1
aman
delhi
2
2
ankush
bihar
1
3
akash
Hamburg
4
update salary
set designation='worker' where id=2
ID
SALARY
DESIGNATION
2000
accountant
1500
worker
10000
manager
5000
developer
update salary
set salary='12000' where id=3
ID
SALARY
DESIGNATION
2000
accountant
1500
worker
12000
manager
5000
developer
ID
SALARY
DESIGNATION
6000
accountant
6000
worker
6000
manager
6000
developer
ID
SALARY
DESIGNATION
7000
accountant
7000
worker
7000
manager
7000
developer
Experiment 5
Aim: To perform the function of Delete statement with and without
WHERE clause
Theory:
The DELETE statement is used to delete rows in a table. Notice the WHERE clause in the SQL
DELETE statement!
The WHERE clause specifies which record or records that should be deleted. If you omit the
WHERE clause, all records will be deleted!
Query:
1.Using where clause
Select * from salary
ID
SALARY
DESIGNATION
5000
accountant
1000
worker
7000
manager
3000
developer
ID
SALARY
DESIGNATION
5000
accountant
1001
worker
3000
developer
EMPID
NAME
CITY
EID
aman
delhi
2
ankush
bihar
1
akash
Hamburg
4
3 row(s) deleted.
0.03 seconds
no data found
Experiment 6
Aim: To perform the function of Alter statement for adding new
columns and constraints, modifying column type,etc
Theory:
The ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify
columns in an existing table.
Query:
1.To add a column in a table
Select * from emp
EMPID
NAME
CITY
EID
aamna
mp
aman
delhi
ankush
bihar
seconds
EMPID
NAME
CITY
EID
AGE
aamna
mp
4
-
aman
delhi
2
-
ankush
bihar
1
-
EMPID
NAME
CITY
EID
AGE
1
aamna
mp
4
40
2
aman
delhi
2
40
3
ankush
bihar
1
40
Experiment 7
Aim: To perform the function of SQL inbuilt functions like
SUM,MAX,MIN,AVG,COUNT,DISTINCT etc
Theory:
1.Sum()
The SUM() function returns the total sum of a numeric column.
select * from salary
ID
SALARY
DESIGNATION
5000
accountant
1001
worker
3000
developer
2.Max()
The MAX() function returns the largest value of the selected column.
select max(salary)as maximum_salary from salary
MAXIMUM _SALARY
5000
3.Min()
The MIN() function returns the smallest value of the selected column. select
min(salary)as minimum_salary from salary
MINIMUM_ SALARY
1001
4.Avg()
The AVG() function returns the average value of a numeric column. select
avg(salary)as average_salary from salary
AVERAGE_ SALARY
3000.33333333333333333333333333333333333
5.Count()
The COUNT() function returns the number of rows that matches a specified criteria.
SELECT count(*) from emp
COUNT(*)
3
6.Distinct()
select * from emp
EMPID
NAME
CITY
EID
AGE
DATE_ OF_ JOIN
aamna
mp
4
40
2001-12-12 , tuesday
sameer
bihar
4
50
2000-01-01
sadhna
delhi
4
50
2000-01-01
aman
delhi
2
40
2014-01-02
, monday
ankush
bihar
1
40
2009-11-02
, sunday