0% found this document useful (0 votes)
2 views7 pages

6-Inserting _ Updating _ Deleting Records in a Table - Using Transaction Control Commands-29!01!2024

The document provides instructions on using SQLPlus for database management, including login credentials and basic SQL commands. It explains the structure of relational databases, including tables, primary and foreign keys, and constraints. Additionally, it covers data entry, transaction control commands, and how to define and manage constraints within a database.

Uploaded by

itsrajsharma29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

6-Inserting _ Updating _ Deleting Records in a Table - Using Transaction Control Commands-29!01!2024

The document provides instructions on using SQLPlus for database management, including login credentials and basic SQL commands. It explains the structure of relational databases, including tables, primary and foreign keys, and constraints. Additionally, it covers data entry, transaction control commands, and how to define and manage constraints within a database.

Uploaded by

itsrajsharma29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

type sqlplus at command prompt

alternatively type sqlplus in search text box

username: 22BIT0025@SITEORA

password: 22BIT0025

To change your password type

SQL>password

SQL (Structured Query Language) -- This is not a programming language

Command based language

SQL is a comprehensive database language to create and maintain a database.

A database is a collection of related data with implicit meaning.

Relational database

A relational database is a collection of inter-related tables.

A table is formally called a relation.

SQL is a command based language:

create

alter command
insert

update

delete

select (data retrieval. read command)

Metadata means data (schema data) about data (user data)

The description of a database is called schema of the database.

Data present in a database at a particular instance of time is called database instance.

SQL is not a case sensitive language.

create table Employee (SSN char(9) primary key,

fname varchar(20) not null,

Minit char(1),

Lname varchar(20) not null,

Sex char(1),

Address varchar(100),

salary number(10),

DOB date,

Department number(1),

designation varchar(20),

SupervisorSSN char(9)

);
create table Department (Dnumber number(1) primary key,

Name varchar(30) not null unique ,

ManagerSSN char(9),

Manager_DOB date,

Location varchar(60)

);

unique means duplicate values are not permitted.

not null means value is mandatory.

SQL> select * from tab;

you can view all tables from the database

tab is system table.

SQL>drop table table_name; -- removes a table

A table can have at the most one primary key; but it can have multiple secondary keys.

Use the keyword unique to define a secondary key.

create table Dependent (Name varchar(50),

DOB date,

Sex char,

Relationship varchar(30),
EmployeeSSN char(9),

primary key(name, employeessn)

);

create table Project (Pnumber number(2) primary key,

Name varchar(20),

Location varchar(40),

ControllingDepartment number(1),

Budget number(20)

);

create table Works_on (SSN char(9),

ProjectNum number(2),

Hours number(4, 2),

primary key(ssn, projectnum)

);

Data entry (interactive data entry)

***********

insert into employee1 values (&SSN, &fname, &Minit, &Lname, &Sex, &Address, &salary, &DOB,
&Department, &designation, &SupervisorSSN);

To save the changes in user data; use

commit;

To undo the changes use


rollback;

But committed data cannot be rolled back.

To see the data that you have entered into the table use

select * from employee;

To execute commands using .sql file:

Create a file containing necessary SQL commands with extension .sql

Now at the SQL prompt

SQL>@file-path

Data entry

***********

insert into employee values (&SSN, &fname, &Minit, &Lname, &Sex, &Address, &salary, &DOB,
&Department, &designation, &SupervisorSSN);

Constraint definition

==================

ALTER TABLE table_name ADD CHECK (condition);

ALTER TABLE table_name ADD [CONSTRAINT constraint_name] CHECK (condition);


i. Department number should be in the range 1000 to 2000.

i. Department number should be in the range 1 to 9.

alter table department add check (dnumber between 1 and 9);

alter table department add [constriant dept_chk] check (dnumber between 1 and 9);

To find out constraint name:

select constriant_name, constraint_type from user_constraints where table_name = 'DEPARTMENT';

To drop a constriant use:

ALTER TABLE table_name DROP CONSTRAINT constraint_name;

Foreign key:

A foreign key establishes a relationship between two tables.

A foreign key is a column that appears as a key column in some other table.

The other table can be the same table.

A foreign key can be composite in nature.

The key column can be primary key / secondary key.

The domain of foreign key column must be the same as that of the referencing column (key column it
refers to).

The data type along with size (may be not null also) defines domain of a column.

ssn char(9) Here char(9) is the domain of ssn

fname varchar(20) Here varchar(20) is the domain of fname.


How to define a foreign key?

ALTER TABLE table_name ADD FOREIGN KEY (col1, col2,...) REFERENCES other_table_name(col1,
col2, ...);

alter table employee12 add foreign key (department) references department12(dnumber);

alter table employee12 add foreign key (supervisorssn) references employee12(ssn);

Foreign key constraint is also called Referential Integrity constraint.

Check constraint & Foreign key

Check constraint

===============

ii) Relationship of the dependents to an employee should be only Spouse, Son, Daughter, and Parent.

alter table dependent12 add check (relationship in ('Spouse', 'Son', 'Daughter', 'Parent') );

iii. Name of the project doesn’t exceed 4 characters.

iii. Name of the project doesn’t exceed 10 characters.

alter table project12 add check ( length(pname) <= 10);

You might also like