Informatics Practices Class 11th Notes Unit 3 Part-1 MySql
Informatics Practices Class 11th Notes Unit 3 Part-1 MySql
Database facilitates Sharing of Data: In a database system all the users handling different related application refer and
share the same data store data stored in central location.
When a database contains data to be used by many users, it is important to keep integrity to check to insure that data
values satisfy to certain specified rules for example and invalid dates should not be allowed to enter as 31/02/96.
Database ensures Security: Data are protected against accidental or intentional disclosure to unauthorized person or
unauthorized modification.
Data model
Data model is a collection of concepts that can be used to describe the structure of database. Structure of database means
data types, relationships and constraints. In other words Data model describes, how data is organized or stored in the
database. Many data models are used for database management system, these are:
Relational data model
In this model data is organized into relation or table (i.e.
row and column) a row in a table represents a
relationship of data to each other and also called a tuple
or record. A column is called attribute or field. Mr. E.F
Codd of the IBM proposed the relational model. This
model is flexible and it easy to link two or more tables.
Network data model
In this model, data is represented by collection of records
and relationship among data is shown by links.
Hierarchical data model
In this model, records are organized as tree. A record at
top level is called root record and this may contain
multiple directly linked children records.
Object oriented data model
In this model, data is represented as objects. The collection of similar types of object is called class.
1
Easy Notes: Database MySql Nitin Ruhela
RDBMS Terms
Relation/Table: A table refers to a two dimensional representation of data arranged in columns (also called fields or
attributes) and rows (also called records or tuples).
The tables in a database are generally related to each other to facilitate efficient management of the database. Interrelated
tables also reduce the chances of errors in the database.
A relation has following properties.
1. For a row, each column must have one value only.
2. All rows are distinct i.e. no two rows are identical
3. The ordering of row in not necessary
4. For any given column of a table, all values are of same kind.
Domain: it is a pool (collection/set) of possible values from which the actual value for a column is derived. Like 1, ravi,
12500, rohan, etc.
Record/tuple/entity: the row or records of tables are generally related to a tuples. For example the following is a tuple
from the student table
101 asmit ravi raj nagar bandra east 9485364512 78
Attributes: the column or fields of table are generally referred to as attributes. Every column in a table must have a
unique name. in the above given student table AdmNo, LibCardNo are two attribute.
Degree: the number of attributes in a relation id called the degree of a relation. A relation having 3 attribute is said to be
of degree 3. In the above given table students the degree is 4.
Cardinality: the number of tuples in a relation is called cardinality of the relation. In table student the cardinality is 3.
Introduction to MySQL
MySQL is Open Source, Fast and Reliable Relational Database Management System (RDBMS) software like Oracle,
Sybase, MS SQL Server etc. It was developed by Michael Widenius and AKA Monty and is alternative to many of the
commercial RDBMS.
The main features of MySQL are-
Open Source & Free of Cost: It is Open Source and available at free of cost.
Portability: It can be installed and run on any types of Hardware and OS like Linux, MS Windows or Mac etc.
Security: It creates secured database protected with password.
Connectivity: It may connect various types of Network client using different protocols and Programming
Languages .
Query Language It uses SQL (Structured Query Language) for handling database.
2
Easy Notes: Database MySql Nitin Ruhela
Data types
The data types are the characteristics of data stored in the tables. Data types allow you to specify the type of information
that can be stored in a particular column of a table. For example, you can specify whether a column should consist of
numeric value or alphanumeric string. Defining a data type for each column in a table helps to prevent incorrect data from
entering in a database. The various data types that a column can hold in MySql are:
MySql uses many different data types, divided into three categories.
Data type Description
Integer or Int You can specify a width up to 11 digits (signed or unsigned). If signed,
the range is from -2147483648 to +2147483647. If unsigned, the range
from 0 to 4294967295.
Tinyint You can specify a width up to 4 digits (signed or unsigned). If signed, the
range is from -128 to +127. If unsigned, the range from 0 to 255.
Smallint You can specify a width up to 5 digits (signed or unsigned). If signed, the
range is from -32768 to +32767. If unsigned, the range from 0 to 65535.
Float(M,D) Stores real numbers up to M digits length(including .) with D decimal
Numeric places. A floating point number that cannot be unsigned. Decimal
precision can go to 24 places for a Float.
Double(M,D) Stores real numbers up to M digits length(including .) with D decimal
places. A floating point number that cannot be unsigned. Decimal
precision can go to 53 places for a Double.
Decimal It can represent number with or without the fractional part (like 17.32,
345). The maximum number of digits may be specified in the size
parameter. The maximum number of digits to the right of the decimal
point is specified in the d parameter
Char(size) A fixed-length string from 1 to 255 characters in length right-padded
with spaces to the specified length when stored (default is 1).
String /text Varchar(size) A variable-length string from 1 to 255 characters in length; for example
VARCHAR(25).
Date and Time date It stores date in YYYY-MM-DD format. Like, '2009-07-02'
time It stores time in HH:MM:SS format. Note: The supported range is from
'-838:59:59' to '838:59:59'
Char, varchar, date and time values should be enclosed in single quotes (‘ ’) or double quotes (“ ”). Like, 'Computer'
"Me and u", '2009-07-02', “2014-12-04” etc.
UNSIGNED is an attribute which can be added to many types. You mark the unsigned with the keyword unsigned. So,
when making a table for an example:
CREATE TABLE `example_table` ( `example_col` tinyint(3) unsigned );
If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to the column.
Numeric data types that permit the UNSIGNED attribute also permit SIGNED. However, these data types are signed by
default, so the SIGNED attribute has no effect.
AUTO_INCREMENT
Mysql allows you to set AUTO_INCREMENT to a column. Doing so will increase the value of that column by 1
automatically, each time a new record is added.
3
Easy Notes: Database MySql Nitin Ruhela
Key:
A column or a combination of columns which can be used to identify one or more rows (tuples) in a table is called a key
of the table. In other words, keys refer to the column (or group of columns) which specify how rows of a relation are
distinct from each other.
Primary Key: The group of one or more columns used to uniquely identify each row of a relation is called its Primary
Key.
Candidate Key: A column or a group of columns which can be used as the primary key of a relation is called a candidate
key because it is one of the candidates available to be the primary key of the relation
Alternate Key: A candidate key of a table which is not made its primary key is called its Alternate Key.
Composite Key: in certain tables, a single attribute cannot be used to identify rows uniquely and combination of two or
more attribute is used as primary. Such keys are called Composite Key.
4
Easy Notes: Database MySql Nitin Ruhela
For the above tables the primary keys may be set as follows:
In the STUDENTS table LibCardNo can also be the primary key (Why?). It means that there are two columns in the table
STUDENTS that are unique for each row and can be set as the primary key of the table. These two columns are candidate
keys as they are the candidates for primary key. Out of these two candidate keys we have chosen AdmNo as the primary
key. The other candidate key, i.e., LibCardNo, becomes the Alternate Key of the table. Had we chose LibCardNo as the
primary key, AdmNo would have been called the alternate key of the table.
Every SQL commands starts with the Keywords/Reserve words and clause. Keywords/Reserve words are terms that
have a special meaning and that gives the additional information which are required to execute the command.
Every SQL statement in MySql ends with a semicolon (;).
SQL commands are not case sensitive i.e. you can write SQL statements both in upper and lower case.
stud
roll name address Marks
5
Easy Notes: Database MySql Nitin Ruhela
Create table result (select roll, name, marks from stud);
Create table result as (select * from stud);
MySQL Constraints
Many times it is not possible to keep a manual check on the data that is going into the tables using INSERT or UPDATE
commands. The data entered may be invalid. MySQL provides some rules, called Constraints, which help us, to some
extent, ensure validity of the data. These constraints are:
6
Easy Notes: Database MySql Nitin Ruhela
Address varchar(30)
);
d. MySql Check Constraint
The latter way is useful if you want to specify a composite primary key (when multiple fields or columns are used as a
primary key, they are called a composite key)
In the above example, the customer_SID column in the Order tableis a foreign key pointing to the SID column in the
Customer Table.
Create table Customer Create table Order
( ID integer (3) Primary Key, ( Order_ID integer (3) Primary Key,
Name varchar(30) , Order_Date date ,
Address varchar(30) Customer_SID integer (3)
); Amount integer
Foreign key (Customer_SID) references Customer(SID)
);
8. Viewing table structure :-To show the details of a particular table which is given by you at the time of table
creation, the following command is used Describe employee;
7
Easy Notes: Database MySql Nitin Ruhela
9. Inserting Data into table :-You can insert record in the table by using the following DML command
Syntax :- Insert into <table name> [<column list>] values <list of values>
Suppose a table named student has been created with the following structure.
10. Inserting records from other table:- You can insert all or selected record(s) in the table from another table
by using select command in place of values. Suppose a table named new_student has been created and records to
be inserted from student table having the same structure of column.
13. Modifying table structure (using alter command):- The alter table command is used to change definition
or structure of existing tables. Usually, In general, MySql alter command is used
8
Easy Notes: Database MySql Nitin Ruhela
To add a column:- for instance to add a new column contact of type integer in table student. For this you can use
add clause of alter table, you may give:
Alter table student add (contact integer (10));
To delete a column:- drop column is used to remove a column from a given table this also removes all data from
the column
Alter table student drop column marks;
To redefine a column (data type, size, default values):-To modify column name of table student to have new
width of 30 characters. For this you can use modify clause of alter table, you may give:
Alter table student modify (name varchar (25));
To Change the column name:- Some time you may need to change the name of one of your column. For this you
can use change clause of alter table command
Alter table student change name first_name carchar(25);
To add an integrity constraint:-
Alter table student add constraint A_no Primary Key (Amin_Nno);
Remove a integrity constraint:-
Alter table student add primary key(roll_number);