0% found this document useful (0 votes)
17 views

LAB EXPERIMENT6

The document outlines SQL commands for creating tables with and without constraints, including primary keys, foreign keys, and unique constraints. It also covers various SELECT statements with conditions, grouping, and ordering, along with examples for each command. Additionally, it provides instructions for creating a specific EMP table and adding constraints to its fields.

Uploaded by

Simran Prabhakar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

LAB EXPERIMENT6

The document outlines SQL commands for creating tables with and without constraints, including primary keys, foreign keys, and unique constraints. It also covers various SELECT statements with conditions, grouping, and ordering, along with examples for each command. Additionally, it provides instructions for creating a specific EMP table and adding constraints to its fields.

Uploaded by

Simran Prabhakar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB EXPERIMENT -6

1. Creating Tables Without Constraints(Key/Domain)

CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype);

SYNTAX:

EXAMPLE:

2. Creating Tables With Constraints(Key/Domain)


CREATE TABLE TableName (

Column1 DataType CONSTRAINT ConstraintName PRIMARY KEY, -- Primary Key constraint

Column2 DataType NOT NULL, -- NOT NULL constraint

Column3 DataType CONSTRAINT ConstraintName UNIQUE, -- UNIQUE constraint

Column4 DataType CHECK (Condition), -- CHECK constraint

Column5 DataType,

CONSTRAINT FK_ConstraintName FOREIGN KEY (Column5) REFERENCES


OtherTable(ReferencedColumn), -- FOREIGN KEY constraint

...

);

SYNTAX:
EXAMPLE:

3. Creating Tables (With Referential Integrity Constraints)


SYNTAX:

CREATE TABLE TableName (

Column1 DataType CONSTRAINT ConstraintName PRIMARY KEY, -- Primary Key constraint

Column2 DataType,

Column3 DataType,

ForeignKeyColumn DataType,

CONSTRAINT FK_ConstraintName FOREIGN KEY (ForeignKeyColumn) REFERENCES


ReferencedTable(ReferencedColumn) -- Foreign Key constraint

);
EXAMPLE:

LAB EXPERIMENT-7

1. SELECT WITH WHERE CLAUSE:

SELECT column1, column2, column3, column4 FROM table_name WHERE condition;

SYNTAX:

EXAMPLE:
2. SELECT with GROUP BY and Aggregate Functions
SYNTAX:

SELECT column1, COUNT(column2) AS alias_name1, AVG(column3) AS alias_name2 FROM


table_name GROUP BY column1;

EXAMPLE:

3. SELECT WITH HAVING CLAUSE


SYNTAX:

SELECT column1, aggregate_function(column2) FROM table_name WHERE condition GROUP BY


column1,HAVING aggregate_condition;
EXAMPLE:

4. SELECT with ORDER BY Clause


SYNTAX:

SELECT column1, column2, column3 FROM table_name WHERE condition ORDER BY


column ASC|DESC;

EXAMPLE:

5. Combining GROUP BY, HAVING, and ORDER BY


SYNTAX:

SELECT column1, COUNT(column2) AS alias_name1, SUM(column3) AS alias_name2 FROM


table_name WHERE condition GROUP BY column1 HAVING condition ORDER BY alias_name2
ASC|DESC;
EXAMPLE:

LAB EXPERIMENT-8
1. Create a table called EMP with the following structure. Name Type EMPNO NUMBER
(6) ENAME VARCHAR2 (20) JOB VARCHAR2 (10) DEPTNO NUMBER (3) SAL NUMBER
(7,2) Allow NULL for all columns except ename and job.

SYNTAX:

CREATE TABLE table_name (column1 datatype CHECK (condition),column2 datatype NOT NULL,column3
datatype NOT NULL,column4 datatype UNIQUE,column5 datatype);

EXAMPLE:
2. Add constraints to check, while entering the empno value (i.e) empno > 100.
SYNTAX:

ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (column1);

EXAMPLE:

3. Define the field DEPTNO as unique.


SYNTAX:

ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column_name);

EXAMPLE:

4. Create a primary key constraint for the table(EMPNO).


SYNTAX:

ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (column_name);

EXAMPLE:

You might also like