0% found this document useful (0 votes)
28 views16 pages

Mysql

This document provides an overview of JavaScript and MySQL, detailing JavaScript syntax, data types, and MySQL features, including its history and main functionalities. It covers SQL commands for database management such as CREATE, SELECT, INSERT, UPDATE, and DELETE, along with examples for creating and manipulating databases and tables. Additionally, it highlights MySQL's usage in web applications and its compatibility with various platforms.

Uploaded by

div lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views16 pages

Mysql

This document provides an overview of JavaScript and MySQL, detailing JavaScript syntax, data types, and MySQL features, including its history and main functionalities. It covers SQL commands for database management such as CREATE, SELECT, INSERT, UPDATE, and DELETE, along with examples for creating and manipulating databases and tables. Additionally, it highlights MySQL's usage in web applications and its compatibility with various platforms.

Uploaded by

div lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

UNIT III – JAVASCRIPT AND MYSQL

Javascript : Advantages of Javascript – Javascript Syntax – Data Type – Variable – Array –

Operators and Expressions – Loops – Functions – Dialog Box – MySQL – The Show Databases

and Table – The USE Command – Create Database and Tables – Describe Table – Select,

Insert, Update and Delete Statement.


MYSQL
Introduction to MY SQL
MySQL is an open-source, fast reliable, and flexible relational database management system, typicallyused
with PHP. This chapter is an introductory chapter about MySQL, what is MySQL, and the main features of
MySQL are described here.

What is MySQL?
 MySQL is a database system used for developing web-based software applications.
 MySQL used for both small and large applications.
 MySQL is a relational database management system (RDBMS).
 MySQL is fast, reliable, and flexible and easy to use.
 MySQL supports standard SQL (Structured Query Language).
 MySQL is free to download and use.
 MySQL was developed by Michael Widenius and David Axmark in 1994.
 MySQL is presently developed, distributed, and supported by Oracle Corporation.
 MySQL Written in C, C++.

Main Features of MySQL


 MySQL server design is multi-layered with independent modules.
 MySQL is fully multithreaded by using kernel threads. It can handle multiple CPUs if they are
available.
 MySQL provides transactional and non-transactional storage engines.
 MySQL has a high-speed thread-based memory allocation system.
 MySQL supports in-memory heap table.
 MySQL handles large databases.
 MySQL Server works in client/server or embedded systems.
 MySQL Works on many different platforms.
Who Uses MySQL
 Some of the most famous websites like Facebook, Wikipedia, Google (not for search), YouTube, and
Flickr.
 Content Management Systems (CMS) like WordPress, Drupal, Joomla, hob etc.
 A large number of web developers worldwide are using MySQL to develop web applications.

History:
 Development of MySQL by Michael Widenius & David Axmark beginning in 1994.
 First internal release on 23 May 1995.
 Windows version was released on 8 January 1998 for Windows 95 and NT.
 Version 3.23: beta from June 2000, production release January 2001.
 Version 4.0: beta from August 2002, production release March 2003 (unions).
 Version 4.01: beta from August 2003, Jyoti adopts MySQL for database tracking.
 Version 4.1: beta from June 2004, production release October 2004.
 Version 5.0: beta from March 2005, production release October 2005.
 Sun Microsystems acquired MySQL AB on 26 February 2008.
 Version 5.1: production release 27 November 2008.
SQL CREATE Database
The SQL CREATE DATABASE statement is used to create a new SQL database.

Syntax
The basic syntax of this CREATE DATABASE statement is as follows −
CREATE DATABASE DatabaseName;

Always the database name should be unique within the RDBMS.

Example
If you want to create a new database <testDB>, then the CREATE DATABASE statement would be
as shown below −
SQL> CREATE DATABASE testDB;
Make sure you have the admin privilege before creating any database. Once a database is created, you
can check it in the list of databases as follows −
SQL> SHOW DATABASES;
+ +
| Database |
+ +
| information_schema |
| AMROOD |
| TUTORIALSPOINT |
| mysql |
| orig |
| test |
| testDB |
+ +
7 rows in set (0.00 sec)
SQL SELECT Database
When you have multiple databases in your SQL Schema, then before starting your operation, you
would need to select a database where all the operations would be performed.
The SQL USE statement is used to select any existing database in the SQL schema.

Syntax
The basic syntax of the USE statement is as shown below −
USE DatabaseName;
Always the database name should be unique within the RDBMS.

Example
You can check the available databases as shown below −
SQL> SHOW DATABASES;
+ +
| Database |
+ +
| information_schema |
| AMROOD |
| TUTORIALSPOINT |
| mysql |
| orig |
| test |
+ +
6 rows in set (0.00 sec)

Now, if you want to work with the AMROOD database, then you can execute the following SQL
command and start working with the AMROOD database.
SQL> USE AMROOD;

DROP or DELETE Database


The SQL DROP DATABASE statement is used to drop an existing database in SQL schema.

Syntax
The basic syntax of DROP DATABASE statement is as follows −
DROP DATABASE DatabaseName;
Always the database name should be unique within the RDBMS.

Example
If you want to delete an existing database <testDB>, then the DROP DATABASE statement would be
as shown below −
SQL> DROP DATABASE testDB;
NOTE − Be careful before using this operation because by deleting an existing database would result
in loss of complete information stored in the database.
Make sure you have the admin privilege before dropping any database. Once a database is dropped,
you can check it in the list of the databases as shown below −

SQL> SHOW DATABASES;


+ +
| Database |
+ +
| information_schema |
| AMROOD |
| TUTORIALSPOINT |
| mysql |
| orig |
| test |
+ +
6 rows in set (0.00 sec)

SQL Commands:
The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT,
UPDATE, DELETE and DROP.
These commands can be classified into groups based on their nature:

DDL - Data Definition Language:


 CREATE Creates a new table, a view of a table, or other object in database.
 ALTER Modifies an existing database object, such as a table.
 DROP deletes an entire table, a view of a table or other object in the database.

DML - Data Manipulation Language:


 INSERT Creates a record.
 UPDATE Modifies records
 DELETE Deletes records

DCL - Data Control Language:


 GRANT Gives a privilege to user
 REVOKE Takes back privileges granted from user

DQL - Data Query Language:


 SELECT retrieves certain records from one or more tables.
CREATE TABLE
Creating a basic table involves naming the table and defining its columns and each column's data type.
The SQL CREATE TABLE statement is used to create a new table.

Syntax
The basic syntax of the CREATE TABLE statement is as follows −
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);

CREATE TABLE is the keyword telling the database system what you want to do. In this case, you
want to create a new table. The unique name or identifier for the table follows the CREATE TABLE
statement.
Then in brackets comes the list defining each column in the table and what sort of data type it is. The
syntax becomes clearer with the following example.
A copy of an existing table can be created using a combination of the CREATE TABLE statement and
the SELECT statement. You can check the complete details at Create Table Using another Table.

Example
The following code block is an example, which creates a CUSTOMERS table with an ID as a primary
key and NOT NULL are the constraints showing that these fields cannot be NULL while creating
records in this table −

SQL> CREATE TABLE CUSTOMERS(


ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,AGE
INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
Describe Table
You can verify if your table has been created successfully by looking at the message displayed by the
SQL server, otherwise you can use the DESC command as follows −
SQL> DESC CUSTOMERS;
+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| ID | int(11) | NO | PRI | | |
| NAME | varchar(20) | NO | | | |
| AGE | int(11) | NO | | | |
| ADDRESS | char(25) | YES | | NULL | |
| SALARY | decimal(18,2) | YES | | NULL | |
+ + + + + + +
5 rows in set (0.00 sec)

Now, you have CUSTOMERS table available in your database which you can use to store the required
information related to customers.

Insert query
The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.

Syntax
There are two basic syntaxes of the INSERT INTO statement which are shown below.
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
Here, column1, column2, column3,...columnN are the names of the columns in the table into which
you want to insert the data.
You may not need to specify the column(s) name in the SQL query if you are adding values for all the
columns of the table. But make sure the order of the values is in the same order as the columns in the
table.
The SQL INSERT INTO syntax will be as follows −
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);

Example
The following statements would create six records in the CUSTOMERS table.
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (3, 'kaushik', 23, 'Kota', 2000.00 );

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)


VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 );
SERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
INSERT (5,
VALUES INTO'Hardik',
CUSTOMERS27,
(ID,NAME,AGE,ADDRESS,SALARY)
'Bhopal', 8500.00 );
VALUES (6, 'Komal', 22, 'MP', 4500.00 );
You can create a record in the CUSTOMERS table by using the second syntax as shown below.
INSERT INTO CUSTOMERS
VALUES (7, 'Muffy', 24, 'Indore', 10000.00 );
All the above statements would produce the following records in the CUSTOMERS table as shown
below.

+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi |
| 3 | kaushik | 1500.00 |
| 4 | Chaitali | 23 | Kota |
2000.00 |
5 | Hardik | 25 | Mumbai |
| 6 | Komal | 6500.00
| 4500.00 |
7 | Muffy 27 | Bhopal | 10000.00 |
|
+ + |
+ + + +

Select Query
The SQL SELECT statement is used to fetch the data from a database table which returns this data in
the form of a result table. These result tables are called result-sets.

Syntax
The basic syntax of the SELECT statement is as follows −
SELECT column1, column2, columnN FROM table_name;
Here, column1, column2... are the fields of a table whose values you want to fetch. If you want to
fetch all the fields available in the field, then you can use the following syntax.
SELECT * FROM table_name;

Example
Consider the CUSTOMERS table having the following records −
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+ + + + + +
The following code is an example, which would fetch the ID, Name and Salary fields of the customers
available in CUSTOMERS table.
SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS;
This would produce the following result −
+ + + +
| ID | NAME | SALARY |
+ + + +
| 1 | Ramesh | 2000.00 |
| 2 | Khilan | 1500.00 |
| 3 | kaushik | 2000.00 |
| 4 | Chaitali | 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+ + + +
If you want to fetch all the fields of the CUSTOMERS table, then you should use the following query.
SQL> SELECT * FROM CUSTOMERS;
This would produce the result as shown below.
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+ + + + + +

Update Query
The SQL UPDATE Query is used to modify the existing records in a table. You can use the WHERE
clause with the UPDATE query to update the selected rows, otherwise all the rows would be affected.

Syntax
The basic syntax of the UPDATE query with a WHERE clause is as follows −
UPDATE table_name
SET column1 = value1, column2 = value2... , columnN = valueN
WHERE [condition];
You can combine N number of conditions using the AND or the OR operators.
Example
Consider the CUSTOMERS table having the following records −
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi |
| 3 | kaushik | 1500.00 |
| 4 | Chaitali | 23 | Kota |
2000.00 |
5 | Hardik | 25 | Mumbai |
| 6 | Komal | 6500.00
| 4500.00 |
7 | Muffy 27 | Bhopal | 10000.00 |
|
+ + |
+ + + +
The following query will update the ADDRESS for a customer whose ID number is 6 in the table.
SQL> UPDATE CUSTOMERS
SET ADDRESS = 'Pune'
WHERE ID = 6;
Now, the CUSTOMERS table would have the following records −
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Pune | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+ + + + + +
If you want to modify all the ADDRESS and the SALARY column values in the CUSTOMERS table,
you do not need to use the WHERE clause as the UPDATE query would be enough as shown in the
following code block.
SQL> UPDATE CUSTOMERS
SET ADDRESS = 'Pune', SALARY = 1000.00;
Now, CUSTOMERS table would have the following records −
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 1 | Ramesh | 32 | Pune | 1000.00 |
| 2 | Khilan | 25 | Pune | 1000.00 |
| 3 | kaushik | 23 | Pune | 1000.00 |
| 4 | Chaitali | 25 | Pune | 1000.00 |
| 5 | Hardik | 27 | Pune | 1000.00 |
| 6 | Komal | 22 | Pune | 1000.00 |
| 7 | Muffy | 24 | Pune | 1000.00 |
+ + + + + +
Delete Query
The SQL DELETE Query is used to delete the existing records from a table.
You can use the WHERE clause with a DELETE query to delete the selected rows, otherwise all the
records would be deleted.

Syntax
The basic syntax of the DELETE query with the WHERE clause is as follows −
DELETE FROM table_name
WHERE [condition];
You can combine N number of conditions using AND or OR operators.

Example
Consider the CUSTOMERS table having the following records −
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+ + + + + +
The following code has a query, which will DELETE a customer, whose ID is 6.
SQL> DELETE FROM CUSTOMERS
WHERE ID = 6;

Now, the CUSTOMERS table would have the following records.


+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+ + + + + +

If you want to DELETE all the records from the CUSTOMERS table, you do not need to use the
WHERE clause and the DELETE query would be as follows −
SQL> DELETE FROM CUSTOMERS;

Now, the CUSTOMERS table would not have any record

You might also like