Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
133 views

SQL Notes

The document defines key terms and concepts related to database management systems (DBMS) and relational databases. It discusses the basic components of a database including tables, rows, columns, relations, and domains. It also covers database schemas, constraints, queries, and other SQL concepts like NULL values, keys, and functions. The document provides examples of applying constraints at the column and table level in MySQL.

Uploaded by

Rohan Kotian
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

SQL Notes

The document defines key terms and concepts related to database management systems (DBMS) and relational databases. It discusses the basic components of a database including tables, rows, columns, relations, and domains. It also covers database schemas, constraints, queries, and other SQL concepts like NULL values, keys, and functions. The document provides examples of applying constraints at the column and table level in MySQL.

Uploaded by

Rohan Kotian
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Unit III

Database Management System(DBMS)

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.

Relational Data Model

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

These will be applied on a single value and returns a single value.

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.

Cartesian Product (X)

 It refers to the records fetched from two tables


 It combines records of two tables
 The result of cartesian product is all pairs of rows from two tables
 It fetches the sum of columns and product of rows from both tables
 It is denoted by X
 Suppose you have two tables, one table contains 5 rows and 3 columns and another table
contains 14 rows and 7 columns then the cartesian product will be 70 rows and 12 columns
 The query will be:

Displaying all the records


select * from student,marks;

Displaying records that matches with exact values


select * from student, marks where student.rollno=marks.rollno;

2
Constraints in MySQL

 Constraints are some set of rules used for data validation.


 They are some restrictions applied on attribute values.
 It enforces the standards and flawless data.
 Its not necessary to define constraints for each attribute of a table.

The commonly used constraints are,


Primary Key: Used to identify the rows uniquely. It cannot be null or duplicated.
Foreign Key: The key column from a table, which references the primary key in another table.
Not Null: Ensures that a column cannot have NULL values.
NULL refers to missing/unknown/not applicable value.
Unique: Ensures that the values in a column are distinct/unique.
Default: It refers to the value specified for the column when the value is not entered.

Applying constraints to a table


1. Column level
2. Table level
3. Alter Table

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);

Add default value


alter table patient modify gender char(2) default 'M';

Remove primary key


alter table patient drop primary key;

You might also like