You are on page 1of 4

DEPARTMENT OF COMPUTER SCIENCE

( Rachna College of Engineering and Technology Gujranwala )

DATABASE SYSTEMS

Name: Registration No:

LAB 07

Data Definition, Data Manipulation and Accessing Database through a C# Application

Creating Table
Data definition queries are used to create tables in database. Following constraints can be
applied while creating table.

CREATE TABLE Persons


(
P_Id int NOT NULL UNIQUE ,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
CREATE TABLE Persons

(
P_Id int NOT NULL PRIMARY KEY CHECK (P_Id > 0 AND P_Id < 21),
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Sandnes'

)
// To name PK constraint and to set PK on multiple columns
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
)
//Create table with Foreign Key Constraint
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
)

ALTER TABLE COMMANDS:


// to Add new column in table
ALTER TABLE table_name ADD column_name datatype
// To drop a column from table
ALTER TABLE table_name DROP COLUMN column_name
// To change data type of a column
ALTER TABLE department
modify address int
// To add primary key in table
ALTER TABLE Persons
ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
// To drop primary key from table
ALTER TABLE Persons DROP PRIMARY KEY
ALTER TABLE Persons DROP CONSTRAINT pk_PersonID
// To add foreign key in table
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
// To drop foreign key from table
ALTER TABLE Orders
DROP CONSTRAINT fk_PerOrders
DATA MANIPULATION LANGUAGE

A DML statement is executed when you:


– Add new rows to a table
– Modify existing rows in a table
– Remove existing rows from a table

Insert Statement:

INSERT INTO departments(P_ID, LastName, FirstName, Address, City) VALUES (1, 'Ali’
Khan', ‘xyz’, ‘Lhr’);

INSERT INTO departments VALUES (1, 'Ali’ Khan', ‘xyz’, ‘Lhr’);

Update Statement:

UPDATE Persons SET Last_Name= ‘Arslan’ WHERE P_ID = 1;

All rows in the table are modified if you omit the WHERE clause:

Removing a Row from a Table:

DELETE FROM Persons WHERE Last_Name = 'Arslan';

All rows in the table are deleted if you omit the WHERE clause.

Truncate Statement:

Removes all rows from a table, leaving the table empty and the table structure intact

TRUNCATE TABLE Persons;

Drop Table:
Deletes the table from the database completely.

DROP TABLE Persons;

ACCESSING DATABASE THROUGH A C# APPLICATION

Following steps are used to connect C# application to an Oracle Database;


1. Create Connection String
2. Open the Connection
3. Write the SQL query
4. Create an Oracle Command
5. Create Oracle Data Reader
6. Execute the Oracle Command
7. Close the Oracle Connection

You might also like