The document discusses database management systems and SQL. It covers the Entity Relationship model and enhanced Entity Relationship model. It also discusses the four main parts of the SQL language: data definition language, data manipulation language, data control language, and embedded SQL. Finally, it provides examples of SQL statements for updating, deleting, and creating tables with constraints.
The document discusses database management systems and SQL. It covers the Entity Relationship model and enhanced Entity Relationship model. It also discusses the four main parts of the SQL language: data definition language, data manipulation language, data control language, and embedded SQL. Finally, it provides examples of SQL statements for updating, deleting, and creating tables with constraints.
The document discusses database management systems and SQL. It covers the Entity Relationship model and enhanced Entity Relationship model. It also discusses the four main parts of the SQL language: data definition language, data manipulation language, data control language, and embedded SQL. Finally, it provides examples of SQL statements for updating, deleting, and creating tables with constraints.
Module 5: Enhanced Entity Relationship Disjoint constraints describe the
(EER) Model relationship between members of different subclasses. EER model - the enhanced version of the E- Categorization is a modeling process of a R model, which includes all the basic single subclass having a relationship with concepts of the E-R model with the capability more than distinct superclasses. to support additional semantic concepts of complex applications. Module 6: Query Languages Superclass Entity Type (Supertype) - A superclass entity type is a generic entity type SQL, pronounced as “ess-cue-ell” that includes one or more distinct subclasses or ‘sequel’ - is the abbreviation for the required to be represented in a data model. structured query language; consists of facilities for defining, accessing, and A superclass and a subclass is a one-to- one (1:1) relationship. managing relational databases. All tasks related to relational data management Subclass Entity Type (Subtype) - A subclass entity type is a more specialized creating tables, querying the database, entity type that has a distinct role in the deleting, granting access to users, etc., can be done using SQL. organization. A subclass is a member of It has been accepted as an American the superclass. standard by American National Specialization - includes subgrouping entities within an entity set having some Standards Institute (ANSI) and is a Federal Information Processing distinct nature than other entities. a process by which any existing entity Standard (FIPS). It is also an international standard set is divided into smaller entity sets according to the distinct or different recognized by the ISO. nature of entities. The first commercial DBMS that Generalization - when two or more entity supported SQL was Oracle in 1979. sets can be combined into a single entity set SQL statements can be invoked either interactively in a terminal session or by by determining similarities between the entities. embedding them in application Specialization and generalization lead to programs. attribute inheritance between higher- CHARACTERISTICS OF SQL level entity sets and lower-level entity sets. 1. SQL is extremely flexible. Aggregation - an abstraction process in 2. SQL uses a free-form syntax that which a relationship set is considered a gives the user the ability to higher-level entity set. structure SQL statements in a Specialization and Generalization way best suited. 3. It is a free-formatted language, Constraints are applied to capture important business rules of the relationships i.e., there is no need to start SQL statements in a particular column in an enterprise. Participation Constraints tell the or be finished in a single line. participation of entity set in relationship 4. It has relatively few commands. sets. 5. It is a non-procedural language. 1. Partial participation - If only The SQL language is mainly divided into some entities from entity set E four major parts. participates in relationships in set R. 1. Data-Definition Language 2. Total participation - If every entity (DDL) - The SQL DDL provides from entity set E is participated with commands for defining the relations, at least one relation in relationship deleting the relations, and modifying set R. the existing relation schemas. View Definition Language (VDL) - The SQL DDL provides commands for Database Management System defining and dropping the Specify which column you want to views. update and the new value in the SET Integrity - The SQL DDL clause. provides commands for Specify which rows are to be updated specifying integrity using a condition in the WHERE constraints that must be clause. The WHERE clause is satisfied by the data stored in optional. If you omit it, the UPDATE the database. statement will modify all rows in the Authorization - The SQL table. DDL provides commands for UPDATE table_name specifying access rights to the SET column_name1 =expr1, relations and views. column_name2 =expr2,... Data Manipulation Language (DML) - [WHERE Condition]; provides a query language. This query DELETE Statement Syntax: language is based on relational algebra and Specify the table from which you tuple relational calculus; contains delete data. commands to insert tuples into the database, Use a condition to specify which rows delete tuples from the database, and to delete in the WHERE clause. The modify/update tuples. DELETE statement will delete rows Data Control Language or Transaction that match the condition. Control Language (DCL or TCL) - DELETE FROM table_name provides commands that help the DBA WHERE condition; control the database, such as granting or Creating table revoking privileges to access the database CREATE TABLE student (student_id and storing or removing transactions that INT NOT NULL would affect the database. AUTO_INCREMENT, Embedded SQL defines how the SQL stud_no VARCHAR(11) UNIQUE, statements can be embedded within general- lname VARCHAR(50), purpose programming languages like C, fname VARCHAR(50), C++, Cobol, Pascal, etc. mname VARCHAR(50), dob DATE, Dynamic SQL allows programs to PRIMARY KEY (student_id)) construct and submit SQL queries at run CHARACTER SET latin1 time. COLLATE latin1_general_ci ENGINE=INNODB; Module 7: Query Languages 2 Module 8: Query Languages 3 UPDATE is a DML statement that modifies rows in a table. It allows you to change the ALTER TABLE statement to add a column, values in one or more single row columns or alter a column, rename a column, drop a multiple rows; statement to update data in a column and rename a table. table. UNIQUE constraint to enforce the WHERE clause allows you to specify a uniqueness of values in a column or a group search condition for the rows returned by a of columns in a table; can ensure that values query; clause filters row from the result set. in a column or a group of columns are unique MySQL DELETE statement deletes data ADD COLUMN Syntax: from a single table. For single column UPDATE Statement with WHERE ALTER TABLE table_name clause Syntax: ADD [COLUMN] Specify the name of the table to new_column_name update data after the UPDATE column_definition keyword. [FIRST | AFTER column_name]; For multiple columns ALTER TABLE table_name Database Management System ADD [COLUMN] Module 9: Query Languages 4 new_column_name Foreign key is a column or group of column_definition [FIRST | AFTER column_name], columns in a table that links to a column or ADD [COLUMN] group of columns in another table; places new_column_name constraints on data in the related tables, column_definition which allows MySQL to maintain referential [FIRST | AFTER column_name], integrity. ...; ADDING FOREIGN KEY MODIFY COLUMN Syntax: CONSTRAINT For multiple columns [CONSTRAINT constraint_name] ALTER TABLE table_name FOREIGN KEY [foreign_key_name] MODIFY [COLUMN] column_name (column_name, ...) column_definition REFERENCES [ FIRST | AFTER column_name], parent_table(column_name,...) MODIFY [COLUMN] column_name [ON DELETE reference_option] column_definition [ON UPDATE reference_option] [ FIRST | AFTER column_name], Specify the foreign key constraint ...; name that you want to create after CHANGE COLUMN Syntax: CONSTRAINT keyword. Specify a list of comma-separated CHANGE [COLUMN] original_name new_name foreign key columns after the FOREIGN KEY keywords. column_definition [FIRST | AFTER column_name]; Specify the parent table followed by a list of comma-separated columns to DROP COLUMN Syntax: ALTER TABLE table_name which the foreign key columns reference. DROP [COLUMN] column_name; Specify how the foreign key RENAME COLUMN Syntax: ALTER TABLE table_name maintains the referential integrity RENAME COLUMN original_name between the child and parent tables using the ON DELETE and ON TO new_column_name; RENAME TABLE Syntax: UPDATE clauses. The reference option determines the action MySQL ALTER TABLE table_name RENAME [TO] new_table_name; will take when values in the parent RENAME TABLE original_name TO key columns are deleted (ON new_name; DELETE) or updated (ON UPDATE). UNIQUE Constraint Syntax: MySQL has five reference options: CREATE TABLE table_name(..., CASCADE, SET NULL, NO ACTION, column_name data_type UNIQUE, RESTRICT, and SET DEFAULT. ...); CASCADE - if a row from the parent CREATE TABLE table_name(.., table is deleted or updated, the values column_name1 column_definition, of the matching rows in the child column_name2 column_definition, table are automatically deleted or ..., updated. UNIQUE(column_name1,column_n SET NULL - if a row from the parent ame2) table is deleted or updated, the foreign key column (or columns) ); ALTER TABLE table_name values in the child table are set to MODIFY column_name NULL. column_definition UNIQUE RESTRICT - if a row from the parent table has a matching row in [FIRST | AFTER ] column_name; the child table, MySQL rejects Database Management System deleting or updating rows in the RIGHT JOIN clause is similar to the left parent table. join clause, except that the treatment of NO ACTION - is the same as tables is reversed. The right join starts RESTRICT. selecting data from the right table instead of SET DEFAULT - is recognized by the left table; If a row from the right table the MySQL parser. However, this does not have matching rows from the left action is rejected by both InnoDB and table, the left table column will have NULL in NDB tables. the final result set. DROP FOREIGN KEY CONSTRAINT SYNTAX: ALTER TABLE table_name SELECT column_list DROP CONSTRAINT constraint_name; FROM table1 LEFT JOIN table2 ON Module 10: Query Languages 5 join_condition; SELECT statement to query data from two CROSS JOIN makes a Cartesian product of tables. A relational database consists of rows from the joined tables. The cross join multiple related tables linking together using combines each row from the first table with common columns known as foreign key every row from the right table to make the columns. result set. The cross join clause does not A join is a method of linking data between have a join condition, unlike the inner one (self-join) or more tables based on the join, left join, and right join. values of the common column between the SYNTAX: tables. SELECT column_list INNER JOIN clause joins two tables based FROM table1 on a condition known as a join predicate. The CROSS JOIN table2; inner join clause compares each row from the first table with every row from the second table; the inner join clause includes only rows whose values match. SYNTAX: SELECT column_list FROM table1 INNER JOIN table2 ON join_condition; LEFT JOIN also requires a join-predicate. When joining two tables using a left join, the concepts of left and right tables are introduced. The left join selects data starting from the left table. For each row in the left table, the left join compares with every row in the right table - the left join selects all data from the left table whether there are matching rows that exist in the right table or not. In case there are no matching rows from the right table found, NULLs are used for columns of the row from the right table in the final result set. SYNTAX: SELECT column_list FROM table1 LEFT JOIN table2 ON join_condition;