SQL_PART1
SQL_PART1
VIEWS:-The view does not contain data of its own. It is a kind of relation whose data is
derived from the one or more relations.
KEYS:-
PRIMARY KEY:-Set of one or more than one attribute carries unique values is known as
primary key
CANDIDATE KEY:-In a relation, more than one attribute can prove their candidature to
become the primary key is known as
ALTERNATE KEY:-Candidate key which is not a primary key is called alternate key
FOREIGN KEY:-It manages the relationship between two relations. It is a non key
attribute that derives its values from primary key of another table.
STRUCTURED QUERY LANGUAGE
1. Numeric
2. Date and time
3. String
Numeric data types:-integer, int, tinyint , smallint, mediumint, bigint, float(m,d) (m is the length
and d is the decimal) double(m,d), decimal(m,d)
Date and Time—date-in yyyy-mm-dd format , datetime(yyyy-mm-dd hh:mm:ss format ,
timestamp, time, year(m) m stands for 2 or 4 digit.
String/text type-char(m) length can be between 1-255 characters char (5) can accept 5
characters, char –by default accepts 1 character. BLOB or text to store large amount of data
, varchar© is used to store strings of variable length .
Char is used to store strings of fixed length
STRUCTURED QUERY LANGUAGE
/* This is to implement
Multiline comment statement */
From start button , go to mysql 8.0 command line client. Here the
programmer has to enter mysql (all in small letters )as a password.
STRUCTURED QUERY LANGUAGE
For creating the database use the following command
create database organization;-This command is used for for creating the database
Query OK, 1 row affected (0.87 sec)
Modify is used to change the size of the column. If the data is already in
that particular column, the size of that field can be increased but cannot be
decreased.
mysql> alter table employee
-> modify enm varchar(100);
Query OK, 0 rows affected (2.05 sec)
Change command is used for changing the name of the column and size of
the column can be increased.
mysql> alter table employee
-> change enm ename varchar(100);
Query OK, 8 rows affected (2.88 sec)
STRUCTURED QUERY LANGUAGE
Unique constraint:-
1. This unique constraint ensures that no two rows have the same value
in the column which was assigned with this key constraint.
2. It also ensures that there cannot exist more than one null value in the
column.
3. A column that is unique may or may not be a primary key.
4. Multiple unique constrains can be defined on a table
Create table employee
(
Ecode integer not null, unique
);
STRUCTURED QUERY LANGUAGE
Primary key:-
1. It is similar to unique constraint, The only difference is unique accepts null values
whereas primary key does not.
2. In a table, multiple columns with unique constraints can exist but there can exist only
one column or one combination with primary key.
3. As primary key does not accept null value the column must also be applied with not
null constraint.
Create table employee
(
Ecode integer not null primary key
);
STRUCTURED QUERY LANGUAGE
Default constraint :-
This constraint is specified for a column, using the default clause if user
does not enter a value for a particular column.
Create table employee
(
Ecode integer not null, primary key,
Enm char(10),
gender char(1) default “T”
);
The default value will be entered in case the user does not accept the value
STRUCTURED QUERY LANGUAGE
CHECK CONSTRAINT:-
This constraint checks the values should be as per the criteria mentioned.
create table employee1
(
Ecode integer not null primary key,
Enm char(10),
Grade char(2) default “E2”,
Sal decimal check(Sal>2000)
);
STRUCTURED QUERY LANGUAGE
Foreign key
For inserting records into the table the command should written as follows
For displaying all fields of the relation “employee” “*” is used.
STRUCTURED QUERY LANGUAGE
+------+--------+---------+----------+------------+
| eid | enm | egender | esal | doj |
+------+--------+---------+----------+------------+
| 1009 | rajesh | m | 26670.45 | 2001-04-14 |
+------+--------+---------+----------+------------+
1 row in set (0.00 sec)
STRUCTURED QUERY LANGUAGE
“order by” clause followed by the column name is used to display the
records either in ascending order or in the descending order.
+-----------+-----------+-----------+--------------+----------------+
| max(esal) | min(esal) | sum(esal) | avg(esal) | count(egender) |
+-----------+-----------+-----------+--------------+----------------+
| 86670.45 | 16670.45 | 649114.50 | 64911.450000 | 10 |
+-----------+-----------+-----------+--------------+----------------+
1 row in set (0.00 sec)
STRUCTURED QUERY LANGUAGE
For the purpose of display of annual salary the following command is used.
mysql> select eid, enm, esal, esal*12 as “ anual salary" from employee where
eid=1009;
mysql> select eid, enm, esal, esal*12 “ anual salary" from employee where
eid=1009;
+------+--------+----------+--------------+
| eid | enm | esal | anual salary |
+------+--------+----------+--------------+
| 1009 | rajesh | 26670.45 | 320045.40 |
+------+--------+----------+--------------+
1 row in set, 1 warning (0.00 sec)
STRUCTURED QUERY LANGUAGE
mysql> select dno, max(esal), min(esal), count(esal), avg(esal), sum(esal) from employee group by dno;
+------+-----------+-----------+-------------+--------------+-----------+
| dno | max(esal) | min(esal) | count(esal) | avg(esal) | sum(esal) |
+------+-----------+-----------+-------------+--------------+-----------+
| 10 | 78665.89 | 55000.45 | 2 | 66833.170000 | 133666.34 |
| 20 | 65500.45 | 65000.45 | 2 | 65250.450000 | 130500.90 |
| 60 | 85500.45 | 75000.45 | 2 | 80250.450000 | 160500.90 |
| 40 | 87000.55 | 86500.45 | 3 | 86723.816667 | 260171.45 |
| 30 | 86600.45 | 86600.45 | 1 | 86600.450000 | 86600.45 |
| 50 | 26670.45 | 16670.45 | 2 | 21670.450000 | 43340.90 |
+------+-----------+-----------+-------------+--------------+-----------+
6 rows in set (0.09 sec)
STRUCTURED QUERY LANGUAGE
In order to restrict the display of the records after grouping them , the user has to
use “HAVING” clause.
“WHERE” clause will not work with the combination of grouping clause
delete command is used to delete all the records from the table.
But the table exists
mysql> delete from employee;
+-------------------------+
| substr("EXcellent",2,5) |
+-------------------------+
| Xcell |
+-------------------------+
1 row in set (0.00 sec)
mysql> select char(65,66,67,68)
-> ;
+--------------------------------------+
| char(65,66,67,68) |
+--------------------------------------+
| 0x41424344 |
+
STRUCTURED QUERY LANGUAGE
mysql> select concat(enm,egender) from employee;
+---------------------+
| concat(enm,egender) |
+---------------------+
| sudhirm |
| sudhikshaf |
| kiritt |
| mansif |
| nehalf |
| ayushm |
| rajeshm |
| pyushm |
+---------------------+
8 rows in set (0.00 sec)
STRUCTURED QUERY LANGUAGE
mysql> select employee.eid, employee.enm, department.dno, department.dnm from
employee , department where employee.dno=department.dno;
+------+---------------+-----+----------------------+
| eid | enm | dno | dnm |
+------+---------------+-----+----------------------+
| 1001 | ayan | 10 | computer department |
| 1003 | raju mahato | 10 | computer department |
| 1002 | tanmay mahato | 20 | physics department |
| 1004 | suraj | 30 | chemistry department |
| 1005 | nehaal | 30 | chemistry department |
| 1006 | meha | 40 | maths department |
| 1008 | XYZ | 50 | english department |
| 1012 | ramesh | 50 | english department |
| 1009 | saksham | 60 | sports department |
| 1010 | sejal | 70 | science department |
| 1011 | mehak | 70 | science department |
+------+---------------+-----+----------------------+
STRUCTURED QUERY LANGUAGE
In the above command e is the alias name for employee and d is the alias
name for department.
STRUCTURED QUERY LANGUAGE