Referential Integrity Constraints
Total Page:16
File Type:pdf, Size:1020Kb
Referential Integrity Constraints Referential Integrity constraints in DBMS are based on the concept of Foreign Keys. A foreign key is an important attribute of a relation which should be referred to in other relationships. Referential integrity constraint state happens where relation refers to a key attribute of a different or same relation. However, that key element must exist in the table. Example: In the above example, we have 2 relations, Customer and Billing. Tuple for CustomerID =1 is referenced twice in the relation Billing. So we know CustomerName=Google has billing amount $300 Operations in Relational Model Four basic update operations performed on relational database model are Insert, update, delete and select. • Insert is used to insert data into the relation • Delete is used to delete tuples from the table. • Modify allows you to change the values of some attributes in existing tuples. • Select allows you to choose a specific range of data. Whenever one of these operations are applied, integrity constraints specified on the relational database schema must never be violated. Insert Operation The insert operation gives values of the attribute for a new tuple which should be inserted into a relation. Update Operation You can see that in the below-given relation table CustomerName= 'Apple' is updated from Inactive to Active. Delete Operation To specify deletion, a condition on the attributes of the relation selects the tuple to be deleted. In the above-given example, CustomerName= "Apple" is deleted from the table. The Delete operation could violate referential integrity if the tuple which is deleted is referenced by foreign keys from other tuples in the same database. Select Operation In the above-given example, CustomerName="Amazon" is selected Best Practices for creating a Relational Model • Data need to be represented as a collection of relations • Each relation should be depicted clearly in the table • Rows should contain data about instances of an entity • Columns must contain data about attributes of the entity • Cells of the table should hold a single value • Each column should be given a unique name • No two rows can be identical • The values of an attribute should be from the same domain Advantages of using Relational Model • Simplicity: A Relational data model in DBMS is simpler than the hierarchical and network model. • Structural Independence: The relational database is only concerned with data and not with a structure. This can improve the performance of the model. • Easy to use: The Relational model in DBMS is easy as tables consisting of rows and columns are quite natural and simple to understand • Query capability: It makes possible for a high-level query language like SQL to avoid complex database navigation. • Data independence: The Structure of Relational database can be changed without having to change any application. • Scalable: Regarding a number of records, or rows, and the number of fields, a database should be enlarged to enhance its usability. Disadvantages of using Relational Model • Few relational databases have limits on field lengths which can't be exceeded. • Relational databases can sometimes become complex as the amount of data grows, and the relations between pieces of data become more complicated. • Complex relational database systems may lead to isolated databases where the information cannot be shared from one system to another. Integrity Constraints/ Constraint on Relational Data Model: o Integrity constraints are a set of rules. It is used to maintain the quality of information. o Integrity constraints ensure that the data insertion, updating, and other processes have to be performed in such a way that data integrity is not affected. o Thus, integrity constraint is used to guard against accidental damage to the database. Types of Integrity Constraint 1. Domain constraints o Domain constraints can be defined as the definition of a valid set of values for an attribute. o The data type of domain includes string, character, integer, time, date, currency, etc. The value of the attribute must be available in the corresponding domain. Example: Age ={25 ,26,27,28,29,30} 2. Entity integrity constraints o The entity integrity constraint states that primary key value can't be null. o This is because the primary key value is used to identify individual rows in relation and if the primary key has a null value, then we can't identify those rows. o A table can contain a null value other than the primary key field. Example: 1. Referential Integrity Constraints o A referential integrity constraint is specified between two tables. o In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary Key of Table 2, then every value of the Foreign Key in Table 1 must be null or be available in Table 2. Example: 4. Key constraints o Keys are the entity set that is used to identify an entity within its entity set uniquely. o An entity set can have multiple keys, but out of which one key will be the primary key. A primary key can contain a unique and null value in the relational table. Example: Key : Primary, Candidate, super, unique, composite etc. INTRODUCTION TO SQL SQL, which is an abbreviation for Structured Query Language, is a language to request data from a database, to add, update, or remove data within a database, or to manipulate the metadata of the database. SQL is a declarative language in which the expected result or operation is given without the specific details about how to accomplish the task. The steps required to execute SQL statements are handled transparently by the SQL database. Sometimes SQL is characterized as non-procedural because procedural languages generally require the details of the operations to be specified, such as opening and closing tables, loading and searching indexes, or flushing buffers and writing data to filesystems. Therefore, SQL is considered to be designed at a higher conceptual level of operation than procedural languages because the lower level logical and physical operations aren't specified and are determined by the SQL engine or server process that executes it. Instructions are given in the form of statements, consisting of a specific SQL statement and additional parameters and operands that apply to that statement. SQL statements and their modifiers are based upon official SQL standards and certain extensions to that each database provider implements. Commonly used statements are grouped into the following categories: What Can SQL do? • SQL can execute queries against a database • SQL can retrieve data from a database • SQL can insert records in a database • SQL can update records in a database • SQL can delete records from a database • SQL can create new databases • SQL can create new tables in a database • SQL can create stored procedures in a database • SQL can create views in a database • SQL can set permissions on tables, procedures, and views RDBMS:- RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. The data in RDBMS is stored in database objects called tables. A table is a collection of related data entries and it consists of columns and rows. Some of The Most Important SQL Commands • SELECT - extracts data from a database • UPDATE - updates data in a database • DELETE - deletes data from a database • INSERT INTO - inserts new data into a database • CREATE DATABASE - creates a new database • ALTER DATABASE - modifies a database • CREATE TABLE - creates a new table • ALTER TABLE - modifies a table • DROP TABLE - deletes a table • CREATE INDEX - creates an index (search key) • DROP INDEX - deletes an index. CREATE TABLE IN SQL:- The CREATE TABLE statement is used to create a table in a database.Tables are organized into rows and columns; and each table must have a name. SQL CREATE TABLE Syntax CREATE TABLE table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), .... ); The column_name parameters specify the names of the columns of the table.The data_type parameter specifies what type of data the column can hold (e.g. varchar, integer, decimal, date, etc.).The size parameter specifies the maximum length of the column of the table. SQL PRIMARY KEY Constraint The PRIMARY KEY constraint uniquely identifies each record in a database table.Primary keys must contain UNIQUE values.A primary key column cannot contain NULL values.Most tables should have a primary key, and each table can have only ONE primary key. FOREIGN KEY:- FOREIGN KEY in one table points to a PRIMARY KEY in another table. DDL : Data Definition Language:- All DDL commands are auto-committed. That means it saves all the changes permanently in the database. Command Description create to create new table or database Alter for alteration truncate delete data from table drop to drop a table rename to rename a table DML : Data Manipulation Language:- DML commands are not auto-committed. It means changes are not permanent to database, they can be rolled back. Command Description insert to insert a new row update to update existing row delete to delete a row merging two rows or two merge tables TCL : Transaction Control Language:- These commands are to keep a check on other commands and their affect on the database. These commands can annul changes made by other commands by rolling back to original state. It can also make changes permanent. Command Description to permanently commit save rollback to undo change savepoint to save temporarily DCL : Data Control Language:-Data control language provides command to grant and take back authority. Command Description grant permission of grant right revoke take back permission. DQL : Data Query Language:- Command Description retrieve records from one or more select table .