CSK W MySQL NOTES - 1 (INFO)
CSK W MySQL NOTES - 1 (INFO)
MySQL
SQL (Structured Query Language)
The database management system(DBMS) is a special kind of programming language
called a query language to enable users to access data from the database. Structured
Query Language (SQL) is the language used by most relational database systems
(RDBMS). It is a standard language developed and used for accessing and modifying
relational databases
The SQL language was originally developed at the IBM research laboratory in San José,
in connection with a project developing a prototype for a relational database management
system called System R in the early 70s.
SQL is being used by many database management systems. Some of them are:
MySQL
PostgreSQL
Oracle
SQLite
Microsoft SQL Server
MYSQL
Mysql is currently the most popular open-source database software. It is a multi-user,
multiprocessing database management system.
Mysql is one of the best RDBMS being used for developing various web-based software
applications.
Mysql AB was founded by Michael Widenius(Monty), David Axmark and Allan Larsson
in Sweden in the year 1995.
Mysql is an Open Source, Fast and Reliable Relational Database Management System
(RDBMS) . MySQL is free to use, and you can download it from MySQL official
website( http://www.mysql.com/).
MySQL COMMANDS
The database management system(DBMS) is a special kind of programming language
called a query language to enable users to access data from the database. Structured
Query Language (SQL) is the language used by most of the relational database systems
(RDBMS). Mysql is currently the most popular and common open-source database
software for database management. Mysql provides commands for defining, modifying,
controlling and deleting the databases and relations.
CREATE Database :
To create a database, we use the CREATE DATABASE statement.
syntax: CREATE DATABASE databasename;
To create a database called School, we will type the following command at Mysql prompt.
mysql> CREATE DATABASE School;
To create a database called LIBRARY,
mysql> CREATE DATABASE LIBRARY;
A DBMS can manage multiple databases on one computer. Therefore, we need to select
the database that we want to use. Once the database is selected, we can proceed with
creating tables or querying data.
To open/ activate/ use the existing database,
Syntax: mysql> USE databasename;
mysql> USE School;
To get the list of existing databases of Mysql server,
mysql> SHOW DATABASES;
Initially, the created database was empty. It can be checked by using the Show tables
command, which lists the names of all the tables within a database.
To get the list of existing tables in a database,
mysql> SHOW TABLES;
To remove/delete the existing database,
Syntax: mysql> DROP DATABASE databasename;
mysql> DROP DATABASE School;
The storage size of the CHAR datatypes The storage size of the VARCHAR
is equal to n bytes i.e. set length datatype is equal to the actual length
of the entered string in bytes.
CHAR takes 1 byte for each character VARCHAR takes 1 byte for each
character and some extra bytes for
holding length information
Constraints
Constraints are certain types of restrictions on the data values that an attribute can have.
They are used to ensure the accuracy and reliability of data. However, it is not mandatory
to define constraint for each attribute of a table.
By default, each attribute can take NULL values except for the primary key.
Not NULL and Unique constraints, when applied together, will produce a Primary Key
constraint.
Unique constraint allows one Null value for the attribute.
DESCRIBE Command
DESCRIBE is used to view the structure/ Schema of an existing table.
Syntax: mysql> DESCRIBE Tablename;
mysql> DESCRIBE student;
To display employee number, name, job, and salary of all employees from the table EMP
mysql> SELECT Empno, Ename, job, Salary from Emp ;
3. DISTINCT Clause/ Keyword
By default, SQL shows all the data retrieved through the query as output. However, there
can be duplicate values. The SELECT statement, when combined with the DISTINCT
clause, returns records without repetition (distinct records) or eliminates duplicate values
of the specified column.
Syntax: mysql> SELECT DISTINCT attribute FROM Table name;
For example, while retrieving an employee’s job, there can be duplicate values as many
employees are assigned to the same job. To display a unique job type for all the
employees, we use DISTINCT as shown below:
mysql> SELECT DISTINCT Job FROM EMP;
To display the employee name, job, and salary of employees working in department 20,
mysql> SELECT ename, job, salary, deptno from emp
where deptno= 20 ;
To display the employee name, job, and salary of employees other than managers,
mysql> SELECT ename, job, salary from emp
where not job = “manager” ;
Conditions-based operators
Conditions based on a range:-SQL provides a BETWEEN/NOT BETWEEN operator that
defines a range of values that the column value must fall for the condition to become true.
E.g., write an SQL command to display the name and salary of all the employees whose
earnings are between 2000 and 3000
SELECT ENAME, SALARY FROM EMP
WHERE SALARY BETWEEN 2000 AND 3000;
The above command displays Ename and salary of those employees whose salary lies in the
range 2000 to 3000 (both 2000 and 3000 are included in the range).
E.g., write an SQL command to display the name & salary of all the employees whose salary
is less than 2000 or more than 3000
SELECT ENAME, SALARY FROM EMP
WHERE SALARY NOT BETWEEN 2000 AND 3000;
Conditions based on a list: To specify a list of values, the IN/NOT IN operator is used. This
operator selects values that match any value in the given list.
Eg:- Write SQL commands to display the details of employees who are working as CLERKS,
MANAGERS or ANALYSTS.
WHERE SALARY>2000;
DELETE COMMAND: Used to delete all or Selected Rows from the table. The delete
command belongs to the DML category.
SYNTAX: DELETE FROM TABLE NAME WHERE CONDITION;
Write the SQL command to delete the details of CLERKS from table EMP.
DELETE FROM EMP WHERE JOB=’CLERK’;
Write the SQL command to delete all rows from the table EMP.
DELETE FROM EMP ;
DROPPING TABLES: The DROP TABLE command removes a table(both rows and
structure) from the database. This command belongs to the DDL category.
Syntax: DROP TABLE tablename ;
E.g. To drop a table EMP,
DROP TABLE EMP;
Once this command is given, the table name is no longer recognized, and all the data in the
table along with the table structure will be deleted.