SQL_Queries_2
SQL_Queries_2
Now for the common Id we can join the data from 2 tables:
Select Emp.ID,name, salary ,address, phone from emp, emp_detail where emp.Id=
emp_detail.Id
Will give
Name Salary Address Phone
This will match Id in both the tables and give name, salary, address and phone for rows having same
Id in both tables. As Id 1001 is common it took name kamal and salary 30000 from emp table and
corresponding Id 1001 from emp_detail gave Address Nainital and Phone 9991110000, likewise for
Id 1003, 1005 and 1009 as these Id are common in both tables. No data will be given for Id which are
not common.
If we want to give Id also in output then as Id column is present in both the tables so for common
column we should write table_name.Column
Select E.Id, E.name, salary ,ED.address, phone from emp E, emp_detail ED where E.Id = ED.Id
Will give following output:
Id Name Salary Address Phone
A view contains rows and columns, just like a real table. The fields in a view are fields from one or
more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the
data were coming from one single table.
Note: A view always shows up-to-date data! The database engine recreates the data, using the view's
SQL statement, every time a user queries a view.
create a View named DetailsView from the table StudentDetails.
Query:
CREATE VIEW DetailsView AS
SELECT NAME, ADDRESS
FROM StudentDetails
WHERE S_ID < 5;
To see the data in the View, we can query the view in the same manner as we query a table.
SELECT * FROM DetailsView;
Some More DDL Commands:
ALTER TABLE Command:
The ALTER TABLE command is used to change definition of existing tables.
ALTER TABLE is used:
To add a column
To add an integrity constraint
To redefine a column (datatype, size, default value).
A. Adding Columns:
The new column will be added with NULL values for all rows currently in the table.
Several new columns can be added, separated by commas, in a single command.
It may be possible to drop or alter columns.
Syntax: ALTER TABLE <table name> ADD <column name> <data type><size> [ <constraint
name>] ;
Example: ALTER TABLE emp
ADD ( Ph_no integer) ;
Above statement add a new column Ph_no of type integer in table emp.
B. Changing a Column Name:
For changing the name of the column CHANGE clause of ALTER TABLE command is used.
Syntax: ALTER TABLE
CHANGE [ COLUMN ] old_col_name new_col_name column_defination;
Example: ALTER TABLE emp
CHANGE Sal Salary DECIMAL ;
Above statement change the existing column namely Sal of table emp to Salary.
C. Modifying Column Definition:
By using the MODIFY clause we can change any of the following parts of a column definition:
Datatype:We can change any column’s datatype if all rows for the column contains nulls.
Default value: We can decrease any column’s size if all rows for the column contains nulls
whereas we can always increase the size or the precision of a column.
Integrity Constraint: By using the MODIFY clause we can add only NOT NULL constraint to an
existing column.
Order of column: With MODIFY clause we can reorder the columns within a table by using
FIRST or AFTER<colname> clause.
Syntax: ALTER TABLE <table name>
MODIFY (column name newdatatype (newsize) ) [ FIRST | AFTER column] ;
Example: ALTER TABLE emp
MODIFY ( Job char(30) ) ;
Above statement modify column Job of table emp to have new width of 30 characters.
ALTER TABLE dept
MODIFY Dname Char FIRST ;
Above statement reorder an existing column say Dname to be the first column in the table Dept.
D. Modifying Column Definition:
To remove a component of a table, DROP clause of ALTER TABLE is used.
Keywords mostly used with DROP clause of ALTER TABLE command are as following:
PRIMARY KEY – Drops the table’s primary key constraint.
COLUMN <columnname> – Remove mentioned column from the table.
Example: ALTER TABLE dept
DROP PRIMARY KEY , DROP FOREIGN KEY fk_1, DROP COLUMN dno;
Above statement removes the primary key, the foreign key constraint namely fk_1, and the column
namely dno from the Dept table.
The DROP TABLE Command:
The DROP TABLE command is used for drop a table from the database.
Syntax: DROP TABLE [IF EXISTS] <tablename> ;
Example: DROP TABLE dept;
Above statement drop the table namely Dept from the database.
The IF EXISTS clause first checks whether the given table exists in the database or not.
Example: DROP TABLE IF EXISTS emp;
The DROP VIEW Command:
Used for delete a view from database.
When view is dropped it does not cause any change in its base table.
Example: DROP VIEW taxpayee ;
Above statement drops the view taxpayee from the database.
Introduction to Constraint:
A Constraints is a condition or check applicable on a field or set of fields.
Constraints are also known as integrity constraints because they applied to maintain data integrity
Different Constraints:
Following are the different constraints in MySQL:
NOT NULL, UNIQUE, PRIMARY KEY, DEFAULT, CHECK, FOREIGN KEY
1. NOT NULL Constraint
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.
Example: CREATE TABLE emp
( Eno integer NOT NULL, Ename char(20) NOT NULL,
Job char(20) NOT NULL, Salary decimal );
The above table enforces the Eno column and the Ename column to not accept NULL values.
2. UNIQUE Constraint:
This constraint ensure that no two rows have the same value in the specified columns.
When applied to the columns ensure that there cannot exist more than one NULL value in the
column.
Example: CREATE TABLE emp
( Eno integer NOT NULL UNIQUE, Ename char(20) NOT NULL,
Job char(20) NOT NULL,
Salary decimal );
Two or more constrains can be applied in a column as Eno in above case.
3. Primary Key Constraint:
Only one column or one combination can be applied in this constraint.
The primary key cannot allow NULL values, thus this constraints must be applied to columns
declared as NOT NULL.
Example: CREATE TABLE emp
( Eno integer PRIMARY KEY , Ename char(20) NOT NULL,
Job char(20) NOT NULL,
Salary decimal );
4. Default Constraint:
A default value can be specified for a column using the DEFAULT clause.
When a user does not enter a value for the column, automatically the defined default value is
inserted in field.
A column can have only one default value.
Example: CREATE TABLE emp
( Eno integer NOT NULL PRIMARY KEY , Ename char(20) NOT NULL,
Job char(20) DEFAULT = ‘CLERK’, Salary decimal );
Here, if no value is provided for job, the default value of ’CLERK’, will be entered.
5. Check Constraint:
This constraint limits values that can inserted into a column of table.
Example: CREATE TABLE emp ( Eno integer NOT NULL PRIMARY KEY ,
Ename char(20) NOT NULL,
Job char(20) NOT NULL, Salary decimal CHECK > 2000 );
This statement ensure that the value inserted for salary must be greater than 2000.
When a check constraint involves more than one column from the same table, it is specified after
all the column have been defined.
Applying Table Constraints:
When a constraint is to be applied on a group of columns of a table, it is called table constraint.
The table constraint appear in the end of the definition.
Example: CREATE TABLE items ( icode char(5) NOT NULL , descp char(20) NOT NULL,
ROL integer, QOH integer,
CHECK (ROL < QOH),
PRIMARY KEY (icode, descp) );
The above statement define a primary key that have two columns namely icode and descp. It also
ensures that the ROL must be less than QOH in each row.
Referential integrity refers to the accuracy and consistency of data within a relationship. In
relationships, data is linked between two or more tables through primary key and foreign key. They
are linked such that ,if you change or delete a row in one table, you destroy the meaning of rows in
other table
For example, the following figure shows that the customer_num column of the customer table is
a primary key for that table and a foreign key in the orders and cust_call tables. Customer number
106, George Watson™, is referenced in both the orders and cust_calls tables. If customer 106 is
deleted from the customer table, the link between the three tables and this particular customer is
destroyed.