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

SQL Commands 4

SQL commands

Uploaded by

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

SQL Commands 4

SQL commands

Uploaded by

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

12 Codd's Rules

Every database has tables, and constraints cannot be referred to as a rational database
system. And if any database has only relational data model, it cannot be a Relational
Database System (RDBMS). So, some rules define a database to be the correct
RDBMS. These rules were developed by Dr. Edgar F. Codd (E.F. Codd) in 1985, who
has vast research knowledge on the Relational Model of database Systems. Codd
presents his 13 rules for a database to test the concept of DBMS against his relational
model, and if a database follows the rule, it is called a true relational database
(RDBMS). These 13 rules are popular in RDBMS, known as Codd's 12 rules.

Rule 0: The Foundation Rule


The database must be in relational form. So that the system can handle the database
through its relational capabilities.

Rule 1: Information Rule


A database contains various information, and this information must be stored in each
cell of a table in the form of rows and columns.

Rule 2: Guaranteed Access Rule


Every single or precise data (atomic value) may be accessed logically from a relational
database using the combination of primary key value, table name, and column name.

Rule 3: Systematic Treatment of Null Values


This rule defines the systematic treatment of Null values in database records. The null
value has various meanings in the database, like missing the data, no value in a cell,
inappropriate information, unknown data and the primary key should not be null.

Rule 4: Active/Dynamic Online Catalog based on the


relational model
It represents the entire logical structure of the descriptive database that must be stored
online and is known as a database dictionary. It authorizes users to access the database
and implement a similar query language to access the database.
Rule 5: Comprehensive Data SubLanguage Rule
The relational database supports various languages, and if we want to access the
database, the language must be the explicit, linear or well-defined syntax, character
strings and supports the comprehensive: data definition, view definition, data
manipulation, integrity constraints, and limit transaction management operations. If
the database allows access to the data without any language, it is considered a
violation of the database.

Rule 6: View Updating Rule


All views table can be theoretically updated and must be practically updated by the
database systems.

Rule 7: Relational Level Operation (High-Level Insert,


Update and delete) Rule
A database system should follow high-level relational operations such as insert,
update, and delete in each level or a single row. It also supports union, intersection
and minus operation in the database system.

Rule 8: Physical Data Independence Rule


All stored data in a database or an application must be physically independent to
access the database. Each data should not depend on other data or an application. If
data is updated or the physical structure of the database is changed, it will not show
any effect on external applications that are accessing the data from the database.

Rule 9: Logical Data Independence Rule


It is similar to physical data independence. It means, if any changes occurred to the
logical level (table structures), it should not affect the user's view (application). For
example, suppose a table either split into two tables, or two table joins to create a
single table, these changes should not be impacted on the user view application.

Rule 10: Integrity Independence Rule


A database must maintain integrity independence when inserting data into table's cells
using the SQL query language. All entered values should not be changed or rely on
any external factor or application to maintain integrity. It is also helpful in making the
database-independent for each front-end application.
Rule 11: Distribution Independence Rule
The distribution independence rule represents a database that must work properly,
even if it is stored in different locations and used by different end-users. Suppose a
user accesses the database through an application; in that case, they should not be
aware that another user uses particular data, and the data they always get is only
located on one site. The end users can access the database, and these access data
should be independent for every user to perform the SQL queries.

Rule 12: Non Subversion Rule


The non-submersion rule defines RDBMS as a SQL language to store and manipulate
the data in the database. If a system has a low-level or separate language other than
SQL to access the database system, it should not subvert or bypass integrity to
transform data.

foreign key

In the relational databases, a foreign key is a field or a column that is used to establish
a link between two tables.

In simple words you can say that, a foreign key in one table used to point primary key
in another table.

Let us take an example to explain it:

Here are two tables first one is students table and second is orders table.

Here orders are given by students.

STUDENT table:

S_Id LastName FirstName CITY

1 MAURYA AJEET ALLAHABAD

2 JAISWAL RATAN GHAZIABAD

3 ARORA SAUMYA MODINAGAR

ORDER table:
O_Id OrderNo S_Id

1 99586465 2

2 78466588 2

3 22354846 3

4 57698656 1

Here you see that "S_Id" column in the "Order" table points to the "S_Id" column in
"Students" table.

o The "S_Id" column in the "Student" table is the PRIMARY KEY in the "Student"
table.
o The "S_Id" column in the "Order" table is a FOREIGN KEY in the "Order" table.

The foreign key constraint is generally prevents action that destroy links between
tables.

It also prevents invalid data to enter in foreign key column.

1. CREATE TABLE Order


2. (
3. O_Id number(2) PRIMAY KEY,
4. Order_No number(12) NOT NULL,
5. S_Id number(2) FOREIGN KEY REFERENCES student (S_Id)
6. ) ;

Adding foreign key later on in a Table –

1. ALTER TABLE Order


2. ADD CONSTRAINT fk_PerOrders
3. FOREIGN KEY(S_Id)
4. REFERENCES Student (S_Id) ;
Dropping Primary key or Foreign key Constraint

1. ALTER TABLE Order


2. DROP CONSTRAINT fk_PerOrders ;

Difference between primary key and foreign key in


SQL:
These are some important difference between primary key and foreign key in SQL-

Primary key cannot be null on the other hand foreign key can be null.

Primary key is always unique while foreign key can be duplicated.

Primary key uniquely identify a record in a table while foreign key is a field in a table
that is primary key in another table.

There is only one primary key in the table on the other hand we can have more than
one foreign key in the table.

UNION operator

The following query shows all records of both tables in one table using the UNION
operator:

1. SELECT * FROM Old_EmployeeUNION SELECT * FROM New_Employee;

SQL INTERSECT operator combines two select statements and


returns only the dataset that is common in both the statements. T

SELECT NAME, AGE, HOBBY FROM STUDENTS_HOBBY


INTERSECT SELECT NAME, AGE, HOBBY FROM STUDENTS;
Examples of INSTR String function
Example 1: The following SELECT query finds the position of the 'P' character in the
JAVATPOINT string:

1. SELECT INSTR( 'JAVATPOINT',' P ') AS INSTR_P_Position;

Output:

INSTR_P_Position

CONCAT

1. SELECT CONCAT (id , name , work_date )


2. FROM employee;

Examples of LTRIM String function


Example 1: The following SELECT query truncates the given space from the
specified string according to the LTRIM function:

1. SELECT LTRIM( ' JAVATPOINT',' ');

Output:

'JAVATPOINT'

1. SELECT LTRIM( 'NEW DELHI IS THE CAPITAL OF INDIA', 'NEW DELHI IS THE ');

Output:

CAPITAL OF INDIA
Example 4: The following SELECT query trims the given symbol from the specified
string:

1. SELECT LTRIM( '####98221545###', '#');

Output:

98221545###

The following SELECT query truncates the given space from the specified string
according to the RTRIM function:

1. SELECT RTRIM( 'JAVATPOINT ',' ');

Output:

'JAVATPOINT'

Example 2: The following SELECT query truncates the space from the specified
string according to the RTRIM function:

1. SELECT RTRIM( ' JAVATPOINT ');

Output:

' JAVATPOINT'

1. SELECT RTRIM( 'NEW DELHI IS THE CAPITAL OF INDIA', 'CAPITAL OF INDIA');

Output:

NEW DELHI IS THE

UPPER

1. SELECT UPPER( New Delhi IS the Capital OF India) from dual ;

Select upper(sname) from student;

You might also like