Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

INTRODUCTION TO MySQL

DATABASE
Database is a collection of data, which contains information relevant to the enterprise. A database is
a separate application that stores a collection of data. Each database has one or more distinct APIs
for creating, accessing, managing, searching and replicating the data it holds.

DBMS (Data Base Management System) is a collection of inter-related data and set of programs to
access those data. A DBMS refers to software that is responsible for storing, maintaining and utilizing
databases. A database management system (DBMS) is a computer software application that
interacts with end-users, other applications, and the database itself to capture and analyze data.

A Relational Database Management System (RDBMS) is software that:

 Enables you to implement a database with tables, columns and indexes.


 Guarantees the Referential Integrity between rows of various tables.
 Updates the indexes automatically.
 Interprets an SQL query and combines information from various tables.
RDBMS Terminology:
Before we proceed to explain MySQL database system, let's revise few definitions related to
database.

 Database: A database is a collection of tables, with related data.


 Table: A table is a matrix with data. A table in a database looks like a simple spreadsheet.
 Column (Attributes): One column (data element) contains data of one and the same kind, for
example the column postcode.
 Row (Record): A row (= tuple, entry or record) is a group of related data, for example the
data of one subscription.
 Primary Key: A primary key is unique. A key value cannot occur twice in one table. With a
key, you can find at most one row.
 Foreign Key: A foreign key is the linking pin between two tables.
 Index: An index in a database resembles an index at the back of a book.

SQL stands for Structured Query Language


SQL is a standard language for accessing and manipulating databases.

What Can SQL do?

 SQL can execute queries against a database


 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database

MySQL Database:
MySQL is a fast, easy-to-use RDBMS being used for many small and big businesses. MySQL is
developed, marketed, and supported by MySQL AB, which is a Swedish company. MySQL is
becoming so popular because of many good reasons:

 MySQL is released under an open-source license. So you have nothing to pay to use it.

 MySQL is a very powerful program in its own right. It handles a large subset of the
functionality of the most expensive and powerful database packages.

 MySQL uses a standard form of the well-known SQL data language.

 MySQL works on many operating systems and with many languages including PHP, PERL, C,
C++, JAVA, etc.

 MySQL works very quickly and works well even with large data sets.

 MySQL is very friendly to PHP, the most appreciated language for web development.

 MySQL supports large databases, up to 50 million rows or more in a table. The default file
size limit for a table is 4GB, but you can increase this (if your operating system can handle it)
to a theoretical limit of 8 million terabytes (TB).

 MySQL is customizable.
SQL GENERAL DATA TYPES
Each column in a database table is required to have a name and a data type.
The following table lists the general data types in SQL:
Data type Description
CHARACTER(n) A fixed-length string between 1 and 255
characters in length (for example CHAR(5)),
right-padded with spaces to the specified
length when stored. Defining a length is not
required, but the default is 1.
VARCHAR(n) Character string. Variable length. Maximum
length n. A variable-length string between 1
and 255 characters in length. For example,
VARCHAR(25). You must define a length when
creating a VARCHAR field.

INTEGER Integer numerical (no decimal). Precision 10


DECIMAL(p,s) Exact numerical, precision p, scale s.
Example: decimal(5,2) is a number that has 3
digits before the decimal and 2 digits after the
decimal
FLOAT(p) Approximate numerical, mantissa precision p.
A floating number in base 10 exponential
notation. The size argument for this type
consists of a single number specifying the
minimum precision

INTRODUCTION TO DDL AND DML COMMANDS IN MYSQL

Types of SQL Commands:


 Data Definition Language (DDL)
DDL or Data Definition Language actually consists of the SQL commands that can be used to define
the database schema. It simply deals with descriptions of the database schema and is used to create
and modify the structure of database objects in database.
Some of the most fundamental DDL commands discussed during following hours include the
following:

 CREATE – is used to create the database or its objects (like table, index, function, views, store
procedure and triggers).
 DROP – is used to delete objects from the database.
 ALTER-is used to alter the structure of the database.
 RENAME –is used to rename an object existing in the database

CREATE TABLE [table name] (


column_1 datatype,
column_2 datatype,
Create a table
column_3 datatype
);

DROP TABLE [table name];


Delete a table

ALTER TABLE [table name] drop column [column


Alter table by deleting the existing column
name];
Alter table by adding a new column to ALTER TABLE [table name] add column [new column
existing table. name] data_type;
Alter table by modifying the data type of
ALTER TABLE [table name] MODIFY C_N new_data_type;
existing column
Alter table by renaming it ALTER TABLE [table name ]RENAME TO [new table name];
To rename the table Rename table [table name1] to [table name2];

 Data Manipulation Language


The SQL commands that deals with the manipulation of data present in database belong to DML or
Data Manipulation Language and this includes most of the SQL statements.
Syntax of DML Commands:

Select * from [table name];


Select – to retrieve data from a database. Select [column name1],[column name2],…. from [table
name];
INSERT INTO table-name
Inserting data into table
VALUES (value1, value2, value3, ...);
Updating the information in an existing
Update [table name] set C_N= value where condition;
table
Deleting the table Delete from [table name];

 Data Control Language


DCL includes commands such as GRANT and REVOKE which mainly deals with the rights, permissions
and other controls of the database system.
Examples of DCL commands:
GRANT-gives user’s access privileges to database.
REVOKE- withdraws user’s access privileges given by using the GRANT command.

MySQL - DESCRIBE Statement


As the name suggests, DESCRIBE is used to describe something. Since in database we have tables,
that’s why we use DESCRIBE or DESC (both are same) command to describe the structure of a table.
Syntax:
DESCRIBE [table name];
OR
DESC [table name]

PROGRAM DEMONSTRATION ON MYSQL-STUDENT DATABASE

Grno Name class Section


21687 Pranav 8 A
18745 Asha 10 E
19816 Bibin 11 A
22213 Kiran 10 H
18855 Rohit 5 I

Database Name: student_db.


Table Name: student_tb.
grno - integer(5) class - integer(2)
section - varchar(1) name - varchar(10)
1) create database student_db;
2) use student_db;
3) create table student_tb (grno integer(5),name varchar(10),class integer(2),section
varchar(1));
4) insert into student_tb values (21687,’ Pranav’,8,’A’);
5) insert into student_tb values(18745,’ Asha’,10,’E’);
6) insert into student_tb values(19816,’ Bibin’,11,’A’);
7) insert into student_tb values(22213,’ Kiran’,10,’H’);
8) insert into student_tb values(18855,’ Lohit’,5,’I’);
9) select * from student_tb;
10) Desc student_tb;

You might also like