sql-fundamentals
sql-fundamentals
Fundamentals
Database:
A database system is computer based record keeping
system that contains the data/ information about any entity in
systematic way. It also provides various effective methods to
retrieve and manipulate the data.
Some Advantages of database are:
It Reduce data Redundancy ( Repetition / Duplication)
It Controls data Inconsistency
It facilitate sharing of data
It enforce standards ( Rules)
Data security as centralized database
Integrity can be maintained
Database Models:
A database model is a collection of concepts that describes the
structure of database and also describe some constraints which
applied on the database system.
Various types of database models are:
Relational Data Model
Network Data Model
Hierarchical Data Model
Relational Data Model:
Relational Data Model is mostly used model in database
management system. In this model the data stored in
tabular form that mean rows and columns. The rows in
table represent the relationship among set of values.
MySQL Datatypes:
Datatypes are used to identify the type of value that an attribute
can hold in database. It also helps in performing the various
operations associated with type of values.
Some common datatypes are:
Datatype Explanation
CHAR Stores Fixed length String type values
VARCHAR Stores Variable length String type values
INT Stores Integer type Values
FLOAT Stores Floating point Values (Precise 23 digits)
DOUBLE Stores Floating point Values (Precise 24-53 digits)
DATE Stores date in YYYY-MM-DD format
TIME Stores time in HH:MM:SS format
CHAR vs VARCHAR:
CHAR VARCHAR
It allowed to store Fixed length It allowed to store Variable length String
String type value type value
When a column declared as When a column declared as VARCHAR(n)
CHAR(n) then column reserve n- then column reserve maximum n-bytes
bytes memory for values. If value is memory for values. If value is shorter
shorter than length n byte, then than length n byte, then no blank bytes
blank bytes are added. In this case are added. In this case the total size
the total size remains n- bytes. remains exactly equal to size of specified
value.
If specified size of value is larger than n-byte( Maximum), that time error
occurred by Database.
Example: Example:
City CHAR(10) City VARCHAR(10)
City=”Jaipur” City=”Jaipur”
Here maximum size is 10 bytes, and Here maximum size is 10 bytes, and size
size of specified value (Jaipur) is 6. of specified value (Jaipur) is 6. The size
But the size of value remains 10 of value will also remain 6 bytes.
bytes.
Example:
Create a table “Student” as per given below specification.
Field Name
Datatype Size Constraint Description
SID INT 3 PRIMARY KEY Student ID
NAME VARCHAR 25 NOT NULL Name of Student
AGE INT 2 AGE > 5 YEAR Age greater than 5
GENDER CHAR 1 DEFAULT ‘M’ Use M / F / T
MARKS FLOAT ----- ------------- ---------------
CREATE TABLE student
(
SID INT(3) PRIMARY KEY ,
NAME VARCHAR(25) NOT NULL ,
AGE INT(2) CHECK (AGE>5) ,
GENDER CHAR(1) DEFAULT ‘M’ ,
MARKS FLOAT
);
DESCRIBE the Structure of TABLE:
Use following commands.
DESC student;
:: Finished ::