Expt No 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

EXPT NO 2 Roll No

Name

Title : Creating tables, Renaming tables, Data Performance Date


constraints (Primary Key, Foreign key, Not Null), Data
insertion into a table. Checking Date &
Grade

Theory:

1.1Introduction to SQL
Structured Query Language is a special-purpose programming language designed for
managing data held in a relational database management system (RDBMS) Originally based
upon relational algebra and tuple relational calculus,
SQL consists of a data definition language and data manipulation language. The scope of SQL
includes data insert, query, update and delete, schema creation and modification, and data
access control. Although SQL is often described as, and to a great extent is, a declarative
language (4GL).
SQL was one of the first commercial languages for Edgar F. Codd's relational model. Despite not
entirely adhering to the relational model as described by J.F.Codd’s, it became the most widely
used database language.

1.2 TOP DATABASE SYSTEM VENDORS


1) ORACLE: first commercially available relational database management system (RDBMS).
2) SQL SERVER: Currently, Microsoft touts SQL Server 2008 as the platform for business
intelligence solutions.
3) DB2: DB2 runs on Linux, UNIX, Windows and mainframes. IBM
4) SYBASES: powerful positioning in the next-generation transaction processing space
5) MYSQL: MySQL is currently part of the Oracle
6) POSTGRESQL: PostgreSQL, the world's most advanced open source database
7) TERADATA: Teradata laid the groundwork for the first data warehouse
8) INFORMIX: Another IBM product
9) INGRES : Ingres is the parent open source project of PostgreSQL and other database
systems.

1.3 INTRODUCTION TO POSTGRESQL:


PostgreSQL, often simply "Postgres", is an object-relational database management system
(ORDBMS) with an emphasis on extensibility and standards-compliance. PostgreSQL is
cross-platform and runs on many operating systems including Linux, FreeBSD, Solaris, and
Microsoft Windows. PostgreSQL is developed by the PostgreSQL Global Development Group, a
diverse group of many companies and individual contributors. It is free and open source
software, released under the terms of the PostgreSQL License, permissive free software license.

1.4SQL composed of parts :


A) DDL is abbreviation of Data Definition Language. It is used to create and modify the structure
of database objects in database.
B) DCL is abbreviation of Data Control Language. It is used to create roles, permissions, and
referential integrity as well it is used to control access to database by securing it.
C) DML is abbreviation of Data Manipulation Language. It is used to retrieve, store,
modify, delete, insert.

1.5 Datatypes in SQL:


DataType Description

Datatype Description

Char(Size) Stores fixed-length character data to store alphanumeric values, with


a maximum size of 2000 bytes. Default and minimum size is 1 byte

Varchar2(Size) Stores variable-length character data to store alphanumeric values,


with maximum size of 4000 bytes.

char(Size) Stores fixed-length character data of length size characters or bytes,


depending on the choice of national character set. Maximum size if
determined by the number of bytes required storing each character
with an upper limit of 2000 bytes. Default and minimum size is 1
character or 1 byte, depending on the character set.

Nvarchar2(Size) Stores variable-length character string having maximum length size


characters or bytes, depending on the choice of national character
set. Maximum size is determined by the number of bytes required to
store each character, with an upper limit of 4000 bytes

numeric(p,s) Number having precision p and scale s. The precision p indicates


total number of digit varies from 1 to 38. The scale s indicates the
number of digit in fraction part varies from -84 to 127

Date Stores dates from January 1, 4712 B.C. to December 31, 4712 A.D.
Oracle predefine format of Date data type is DD-MON-YYYY.

Raw(Size) Stores binary data of length size. Maximum size is 2000 bytes. One
must have to specify size with RAW type data, because by default it
does not specify any size.Long Raw Store binary data of variable
length up to 2GB(Gigabytes).

LOBS -LARGE OBJECTS LOB is use to store unstructured information


such as sound and video clips, pictures upto 4 GB size.

CLOB A Character Large Object containing fixed-width multibyte


characters.Varying-width character sets are not supported. Maximum
size is 4GB

BLOB To store a Binary Large Object such graphics, video clips and sound
files.Maximum size is 4GB

BFILE Contains a locator to a large Binary File stored outside the database.
Enables byte stream I/O access to external LOBs residing on the
database server. Maximumsize is 4GB

int Allows whole numbers

float Floating precision number

1.6 Data Definition Language The Data Definition Language (DDL) is used to create and
modify database schema and database objects.

DDL composed of following basic statement:

1) CREATE: to create objects in the database

2) ALTER: alters the structure of the database

3) DROP: delete objects from the database

4) DESC: Describing table structure

5) TRUNCATE: remove all records from a table, including all spaces allocated for the
records are removed

6) COMMENT: add comments to the data dictionary

7) RENAME: rename an object

1.6.1 CREATE TABLE : To create logical structure of the database

CREATE TABLE table_name


(
COLUMN_NAME1 DATATYPE(SIZE) CONSTRAINT,
COLUMN_NAME2 DATATYPE(SIZE) CONSTRAINT,
COLUMN_NAME3 DATATYPE(SIZE) CONSTRAINT,
COLUMN_NAMEn DATATYPE(SIZE) CONSTRAINT
);

1.6.2 ALTER TABLE : ALTER TABLE command to add and drop various constraints on an
existing table.

The basic syntax of ALTER TABLE to ADD COLUMN in an existing table is as follows −

ALTER TABLE table_name ADD COLUMN new_column_name Datatype(SIZE);


The basic syntax of ALTER TABLE to DROP COLUMN in an existing table is as follows −

ALTER TABLE table_name DROP COLUMN column_name;

The basic syntax of ALTER TABLE to change the DATA TYPE of a column in a table is as
follows −

ALTER TABLE table_name ALTER COLUMN column_name TYPE datatype;

The basic syntax of ALTER TABLE to add a NOT NULL constraint to a column in a table is as
follows −

ALTER TABLE table_name MODIFY column_name datatype NOT NULL;

1.6.3 DROP TABLE statement is used to remove a table definition and all associated
data, indexes, rules, triggers, and constraints for that table.

Basic syntax : DROP TABLE table_name;

1.6.4 TRUNCATE TABLE command is used to delete complete data from an existing
table

The basic syntax : TRUNCATE TABLE table_name;

1.7 Constraints in SQL


1) Primary key
2) NOT NULL
3) Check
4) UNIQUE
5) Foreign key

1) A PRIMARY KEY is a field in a table which uniquely identifies each row/record in a


database table. Primary keys must contain unique values. A primary key column cannot
have NULL values.
2) The NOT NULL constraint enforces a column to NOT accept NULL values. The NOT
NULL constraint enforces a field to always contain a value. This means that you cannot insert a
new record, or update a record without adding a value to this field.
3) CHECK Constraint
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 into the table.
4) 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.


5) A foreign key constraint specifies that the values in a column (or a group of columns) must
match the values appearing in some row of another table. We say this maintains the referential
integrity between two related tables. They are called foreign keys because the constraints are
foreign; that is, outside the table. Foreign keys are sometimes called a referencing key.

1.8 Dropping Constraints: To remove a constraint you need to know its name. If the name is
known, it's easy to drop. Else you need to find out the system generated name. The psql
command \d tablename can be helpful here; the general syntax is:

ALTER TABLE table_name DROP CONSTRAINT some_name;

2.1 Data Manipulation Language (DML)


1. Data Manipulation is: retrieval ,insertion, deletion, modification of information in/from the
database
2. A DML is a language which enables users to access and manipulate data.
3. There are two types of DML:
● procedural: the user specifies what data is needed and how to get it
● nonprocedural: the user only specifies what data is needed
4. A query language is a portion of a DML involving information retrieval only. The terms DML
and query language are often used synonymously
2.2 INSERT STATEMENT QUERY
The PostgreSQL INSERT INTO statement allows one to insert new rows into a table. One can
insert a single row at a time or several rows as a result of a query.
Syntax
Basic syntax of INSERT INTO statement is as follows.
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
Here, column1, column2,...columnN are the names of the columns in the table into which you
want to insert data.
The target column names can be listed in any order. The values supplied by the VALUES clause
or query are associated with the explicit or implicit column list left-to-right.
The SQL INSERT INTO syntax would be as follows:
INSERT INTO TABLE_NAME VALUES (value1,value2,value3...valueN);
2.3 SELECT STATEMENT QUERY
PostgreSQL SELECT statement is used to fetch the data from a database table which returns
data in the form of result table. These result tables are called result-sets.
Syntax:
SELECT column1, column2, columnN FROM table_name;
Here, column1, column2...are the fields of a table, whose values you want to fetch. If you want to
fetch all the fields available in the field, then you can use the following syntax:
SELECT * FROM table_name;
The PostgreSQL WHERE clause is used to specify a condition while fetching the data from a
single table or joining with multiple tables.
If the given condition is satisfied, only then it returns specific value from the table. You can filter
out rows that you don't want included in the result-set by using the WHERE clause. The WHERE
clause is not only used in SELECT statement, but it is also used in UPDATE, DELETE
statement, etc.
Syntax
SELECT column1, column2, columnN
FROM table_name
WHERE (search_condition);
2.4 UPDATE STATEMENT Query
The PostgreSQL UPDATE Query is used to modify the existing records in a table. You can use
WHERE clause with UPDATE query to update selected rows, otherwise all the rows would be
updated.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE (condition);
2.5 DELETE STATEMENT Query
The PostgreSQL DELETE Query is used to delete the existing records from a table. You can use
WHERE clause with a DELETE query to delete selected rows, otherwise all the records would
be deleted.
Syntax:
DELETE FROM table_name
WHERE [condition];
The WHERE clause is not only used in SELECT statement, but it is also used in UPDATE,
DELETE statement, etc.which we would examine in subsequent chapters.
Syntax
The basic syntax of SELECT statement with WHERE clause is as follows:
SELECT column1, column2, columnN
FROM table_name
WHERE [search_condition];
OPERATORS
An operator is a reserved word or a character used primarily in a PostgreSQL statement
WHERE clause to perform operation(s), such as comparisons and arithmetic operations.
Operators are used to specify conditions in a PostgreSQL statement and to serve as
conjunctions for multiple conditions in a statement.
Arithmetic operators
Comparison operators
Logical operators
Bitwise operators

LAB EXERCISES:

CREATE UNIVERSITY SCHEMA and Apply Various Constraints.

USE DML OPERATIONS TO INSERT DELETE UPDATE AND RETRIEVE RECORD

You might also like