SQL
SQL
SQL
DDL DML
1.These commands allow us to 1.These commands are used to
perform tasks related to Data manipulate the data ie. records or
definition ie. related to the rows in a table.
structure of the database.
2.Examples of these commands 2. Examples of these commands
are : CREATE, ALTER , DROP are : INSERT , UPDATE , DELETE ,
,RENAME , USE. SELECT .
Data type of Attribute
CHAR(x) VARCHAR(x)
1.It provides a fixed length memory 1.It DOES NOT provide a fixed length
storage. memory storage.
2.This takes X characters of storage , 2. This will take the actual number of
even if you enter less than x characters. characters entered in the column.
3.If a value is less than than its length x, 3.No blanks are added if the length is
then blanks are added. shorter than the maximum length.
Constraints
Constraints are the certain types of restrictions on the data values that an attribute can
have. Table below lists some of the commonly used constraints in SQL. They are used
to ensure correctness of data. However, it is not mandatory to define constraints for
each attribute of a table.
Constraint Description
NOT NULL Ensures that a column cannot have NULL values where NULL means missing/
unknown/not applicable value.
UN I QUE Ensures that all the values in a column are distinct/unique
DEFAULT A default value specified for the column if no value is provided
PR I MARY KEY The column which can uniquely identify each row/record in a table.
FORE I GN KEY The column which refers to value of an attribute defined as primary key in another
table
CREATE Database
To create a database called Student Attendance , we will type the
following command at mysql prompt
mysql> CREATE DATABASE Student , Attendance ;
Query OK , 1 r ow affected ( 0 . 02 s ec )
A DBMS can manage multiple databases on one computer. Therefore,
we need to select the database that we want to use. To know the
names of existing databases, we use the statement SHOW DATABASES.
From the listed databases, we can select the database to be used. Once
the database is selected, we can proceed with creating tables or
querying data.
.
CREATE Database
The show tables statement that lists names of all the tables within a database.
my s q l > SHOW TABLES ;
Emp t y s e t ( 0 . 06 s ec )
Database Commands in MySql
Getting a list of available databases
mysql> SHOW DATABASES;
Creating a database
mysql> CREATE database myschool;
Deleting a database
mysql> DROP database <databasename>;
To remove table
mysql> drop table <tablename>;
After database creation we can open the database using USE
command
mysql> USE myschool;
Altering Table
The SQL ALTER TABLE command is used to add, delete or modify columns
in an existing table. You should also use the ALTER TABLE command to add
and drop various constraints on an existing table.
Syntax
The basic syntax of an ALTER TABLE command to add a New Column in
an existing table is as follows.
ALTER TABLE table_name ADD column_name datatype;
The basic syntax of an ALTER TABLE command to DROP COLUMN in an
existing table is as follows.
ALTER TABLE table_name DROP COLUMN column_name;
The basic syntax of an ALTER TABLE command to change the DATA TYPE
of a column in a table is as follows.
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
MySQL Order by – e.g.Suppose we are having student table with following data.
ALTER TABLE
table_name DROP
PRIMARY KEY;
AGGREGATE FUNCTIONS
An aggregate function performs a calculation on multiple values and returns a
single value. For example, you can use the AVG() aggregate function that takes
multiple numbers and returns the average value of the numbers.Following is the list
of aggregate functions supported by mysql.
Name Purpose
Query result will be unique occurrences of class values along with counting of students(records)
of each class(sub group).
MySQL GROUP BY with aggregate functions
we are having student table with following data.
Query result will be unique occurrences of class values along with average
marks of each class(sub group).
MySQL GROUP BY with aggregate functions (with where and order by clause)
we are having student table with following data.
Query result will be unique occurrences of class values where class<10 along with
average marks of each class(sub group) and descending ofer of marks.
Renaming of columns(ALIAS)
In case we want to rename any column while
displaying the output, it can be done by using the
alias 'AS'. The following query selects Employee
name as Name in the output for all the employees:
mysql> SELECT EName as Name FROM EMPLOYEE;
Note: If an aliased column name has space as in the
case of DATE OF BIRTH, it should be enclosed in
quotes as’DATE OF BIRTH’.
Calculating the total
THE END