SQL Notes
SQL Notes
DBMS Terms:
1.
A DB is a collection of logical structures (i.e. tables/relations).
2.
The set of rules to be followed to ensure DBMS behaviour is called the Referential Integrity.
3.
A row is called a Tuple.
4.
A column is called an Attribute.
5.
A table is called as a Relation.
6.
The data type of values in each column is called the Domain.
7.
The number of attributes in a relation is called the Degree of a relation.
8.
The number of rows in a relation is called the Cardinality of a relation.
9.
In a DM, If there are relations exist between data in the tables then it is called as Relational
Database Management System.
Key concepts:
i) Database schema: It is the design/ skeleton of the DB that represents the names of the
tables and columns, data type, data constraint and the relationships among the tables.
ii) Data constraint: It used to ensure accuracy and reliability of data in the DB.
iii) Meta data or data dictionary: DB schema & various data constraints are stored by DBMS
in a catalog or dictionary called meta-data(i.e) data about data)
iv) Database instance: It can have many instances at different times, through data
manipulations.
v) Query: It is a request to a DB for obtaining information in a desired way.
vi) Data manipulation/modification: It consists of three operations such as, insertion,
updation and deletion.
vii) Database engine: It is the underlying component which is a set of programs used by
DBMS to handle various queries.
SQL
SQL (Structured Query Language) is a unified, non-procedural language used to define, manage and
manipulate data stored in a table.
Relation:
A table/relation, from which the values can be derived for other table, is called the BASE table. For
ex, in case of views, the values are extracted from the BASE table only.
NULL
A NULL value in a table is a value in a field which is blank. In other words, a NULL value is an
unidentified or unavailable value not equal to zero. If we do not want any column to hold a NULL
value, then we can define that column with ‘NOT NULL’ constraint.
The most popular data model among the data models is the relational data model.
1
Keys in a relational database
Keys in a relational database refer to the restrictions or constraints on the values of attributes and
contents related to each other.
1. Candidate Key: Candidate keys are those attributes of a table which can be used to identify
the records uniquely. These attributes can be candidates for primary key.
2. Alternate Key: After assigning the candidate key and primary key the remaining attributes of
the relation is alternate key.
3. Primary Key: The primary key is the column which can be used to identify the records
uniquely.
4. Composite Primary Key: When more than one attribute is assigned as primary key, is known
as composite primary key.
5. Foreign Key: The foreign key in a relation is derived from another table, which is primary key
of another table.
Function
A function can be defined as a set of predefined commands which, when called, performs certain
operations and returns a single value. This can be further divided into two categories. 1. Single row
(or) scalar functions, 2. Multiple rows (or) aggregate functions.
Scalar functions
Aggregate functions
Aggregate functions are used to implement calculations based upon a particular column. These
functions always return a single value. Count(), Min(), Max(), Sum(), Avg() are the aggregate
functions. If NULL entertained in any of the column(s) in the relation/table, it will be simply ignored
by all the aggregate functions.
2
Constraints in MySQL
Column Level
The column level constraint is specified along with the column definition.
The constraint will be applied immediately after datatype(size) in column definition.
Have a look at the following command:
create table patient(pid int(4) primary key, pname varchar(20) not null, age int(2), department
varchar(15), dateofadm date, charges double(7,2),gender char(1));
Table Level
The table level constraint will be written at the end of create table command.
The constraint specification will be written immediately after the column definition followed
by comma. For ex,
create table patient(pid int(4),pname varchar(20),age int(2),department varchar(15),dateofadm
date,charges double(7,2),gender char(1), primary key(pid));
Alter table
The constraints will be applies through alter table
Alter table command followed by add constraint is used to apply constraint.
Have a look at the following example
alter table patient add primary key(pid);