CS Module II MySQL Notes
CS Module II MySQL Notes
2. Network DBMS (NDBMS):- Supports many - to many relations, this may results
in complex structures.
MySQL is basically a relational database management system, which works with tables
and then defines the relations between the tables. It is an open source software that
provides multiuser access so you can know that your many users can use this particular
DBMS and retrieve the data . It also supports a multi storage engine and works on
many platforms.
Common terminologies used is SQL
● Row(Record): Record is a collection of values or fields of a specific entity. Eg.
An employee, Salary account, etc.
● column(Field): A field refers to an area within a record which is reserved for a
specific piece of data. Eg. Employee ID.
● Table: Table is the collection of records of specific types. E.g. Employee table is
a collection of records related to all the employees.
● Query- It is a request for data or information from a database table or
combination of tables.
Relationships
A relational database contains tables that relate to another table by using a relationship.
The tables are connected by a common field. The relationships are defined as:
● One to Many: this is the most common type of a table relationship. For every
record in Table A, there are multiple records in Table B.
Example: There is a one to many relationship between the Customers table and Orders
table. A customer may have many orders in the Order table.
● Many to Many: For every record in Table A, there are multiple records in Table
B, and vice versa. In this case, a third table called a Join Table is created to
which will contain only unique values
Example: Many to Many relationship between Orders and Products with the table
ProductsOrders functioning as the Join Table. The table ProductsOrders holds all the
details about each order and what products it contains. Its primary key is a combination
of the primary key of the Orders and Products table.
● One to One – the rarest type of a table relationship. It is used for security and
organization to avoid empty fields that only apply to some records in a table.
My SQL Basics
Rules for Database names ,table name and column name :-
1)It can be at most 64 characters long.
2)It should not be a keyword (SELECT,SHOW etc.)
3)It should be unique.
Data types:-
Character data
1) Character data:- Character data can be stored either as fixed length or variable
length .While defining a character column you have to specify the maximum size of any
string to be stored in the column.For example if you want to store strings upto 30
character then you should either use char(30) or varchar(30). The maximum length for
the char column is 255 bytes whereas for varchar it is 65,535.
If you assign a value to a CHAR or VARCHAR column that exceeds the column’s
maximum length ,then the value will not be accepted.
Values in CHAR and VARCHAR are sorted and compared in case-insensitive
fashion,unless the binary attribute was specified when the table was created.
Numeric data
2)Numeric data - Numeric means numbers and they can be positive, negative or zero
,with decimal points without decimal points . If the data is without decimal point then it is
integer and if it is with decimal point then it is real(float).
The table displays the allowable storage in bytes, the maximum and minimum values of
SIGNED and UNSIGNED integer data types.
Type Stora Minimum Minim Maximum Maximum
ge Value SIGNED um Value Signed Value
(byte Value Unsigned
s) Unsign
ed
TINYINT-
CREATE TABLE StudentGroup(id tinyint AUTO_INCREMENT,Name varchar(255) NOT
NULL,Surname varchar(255) NOT NULL,Grade tinyint(4) UNSIGNED,PRIMARY KEY
(id));
INSERT INTO StudentGroup (id, Name, Surname, Grade)VALUES (1, 'John', 'Smith',5);
SMALLINT-
CREATE TABLE SmallintTable(ID smallint AUTO_INCREMENT,value_1 smallint
SIGNED,value_2 smallint(6) UNSIGNED,PRIMARY KEY (id));
INSERT INTO SmallintTable (ID, value_1, value_2)VALUES (1, -768, 0);
MEDIUMINT-
CREATE TABLE MediumIntTable(ID mediumint AUTO_INCREMENT,value_1
mediumint,value_2 mediumint(6) UNSIGNED,PRIMARY KEY (ID));
INSERT INTO MediumIntTable (ID, value_1, value_2) VALUES (5, -1234567, 1234567);
BIGINT-
create a table
CREATE TABLE BigIntTable(ID bigint AUTO_INCREMENT,value_1 bigint,value_2
bigint(20) UNSIGNED,PRIMARY KEY (ID));
-- insert data
INSERT INTO BigIntTable (ID, value_1, value_2)VALUES (2, -72036854775808,
9223372036854775808);
ZEROFILL-If you need to replace spaces with zeros, you can use the ZEROFILL
attribute for the MySQL INT UNSIGNED column. For example, for the INT(3) ZEROFILL
column with the value 1, the result you retrieve will be 001.
3)TEMPORAL DATA (Date and time)-
(1)CREATE:- This statement is used to create a database ,table etc .with a given name
. Remember, the1 database name should be unique. CREATE statements with
constraints like KEY,CHECK,DEFAULT,ALTER and DROP statements.
It defines a each column of the table uniquely. Each column has minimum three
attributes: name, datatype and size( column width).Each column definition is
separated by comma and at last ends with semicolon.
Rules for creating table:- 1)Name should be maximum 30 characters long and can
have alphabets from A-Z and a-z and digits from 0-9.
(A) INSERTING VALUES INTO THE TABLE:- Once a table is created, to insert the
values row by row, we use a command INSERT INTO using the syntax
Which allows inserting the data of selected columns from table 1 to table 2. For
example to create a table emp_new with only two columns from emp with columns
(EMP_NAME,TEL_NO,ADDRESS, JDATE,DESIGNATION) and having 10 records we
write a command as
Which will create a table EMP_ NEW with two columns EMP_NAME and TEL_NO and
will insert the name and tel nos of 10 persons from table EMP.
(5) SHOW:- This statement is used to see what we are looking for. Example SHOW
DATABASES: , SHOW TABLES;
(6) USE:- This statement is used to open an existing database from SQL server.
Pattern matching
Pattern Meaning
7)What is the command to display all the records ending with the alphabet E?
13) What is the difference between the Update and Modify command?
FORMAT()
7
Returns a number formatted to specified number of decimal places
INSERT()
8
Inserts a substring at the specified position up to the specified number of
characters
INSTR()
9
Returns the index of the first occurrence of substring
LCASE()
10
Synonym for LOWER()
DAY(date)
The DAY() is a synonym for the DAYOFMONTH() function
SELECT DAYNAME('1998-02-05');
Output- 2
MONTHNAME(date) :-Returns the full name of the month for a date.
mysql> SELECT MONTHNAME('1998-02-05');
Output:- | February
YEAR(date)-Returns the year for date, in the range 1000 to 9999, or 0 for the .zero.
date.
SELECT YEAR('98-02-03');
Output- 1998
TIME(expr):-Extracts the time part of the time or datetime expression expr and returns it
as a string.
SELECT TIME('2003-12-31 01:02:03');
Output -| 01:02:03 |
SELECT CURDATE();
Output-| 1997-12-15
NUMERIC FUNCTION-
SELECT ABS(-2);
Output :- 2
SELECT MOD(34,10);
Output :- 4
Output:-
POWER(3,3) | 27
SQRT(X) :-This function returns the non-negative square root of X. Consider the
following example −
SQL>SELECT SQRT(49);
Output:
| SQRT(49) |
|7 |