0% found this document useful (0 votes)
20 views30 pages

Mysql

Uploaded by

rashmee7305
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)
20 views30 pages

Mysql

Uploaded by

rashmee7305
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/ 30

MYSQL

BASIC POINTS
• FIELDS OR COLUMNS OR ATTRIBUTS
• TUPLES →Rows/Records
• CARDINALITY→ Number of Rows/tuples/Records
All Group functions ignore NULL values
• DEGREE→ Number of Columns/Attributes/Fields
• CARTIAN PRODUCT→
1. Sum of degree(Columns/Fields)
2. Multiply of cardinality (Rows/Tuples)
Learn Bytes
KEYS

PRIMARY FOREIGN ALTERNATE


KEY KEY KEY

COMPOSIT CANDIDATE
KEY KEY
WHAT IS DATABASE?
• DBMS stands for Database Management
System. It is a software that is used to create,
manipulate and retrieve data in databases. A
DBMS stores data in a hierarchical or
navigational form. It uses the file system to
store data. So, there is no relationship
between the tables.
DATABASE SOFTWARE NAME
Oracle RDBMS Redis
IBM DB2 CouchDB
Altibase Neo4j
Microsoft SQL Server OrientDB
SAP Sybase ASE Couchbase
Teradata Toad
ADABAS phpMyAdmin
MySQL SQL Developer
FileMaker Seqel PRO
Microsoft Access Robomongo
Informix DbVisualizer
SQLite Hadoop HDFS
PostgresSQL Cloudera
AmazonRDS MariaDB
MongoDB Informix Dynamic Server
4D (4th Dimension)
What is SQL?

SQL stands for Structured Query Language


SQL lets you access and manipulate databases
SQL became a standard of the American
National Standards Institute (ANSI) in 1986,
and of the International Organization for
Standardization (ISO) in 1987
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
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and
views
SQL Constraints
• SQL Constraints are rules used to limit the type of
data that can go into a table, to maintain the
accuracy and integrity of the data inside table.
• Constraints can be divided into the following two
types,
• Column level constraints: Limits only column
data.
• Table level constraints: Limits whole table data.
Type of Constraints
• The following constraints are commonly used in SQL:
• NOT NULL - Ensures that a column cannot have a NULL
value
• UNIQUE - Ensures that all values in a column are different
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE.
Uniquely identifies each row in a table
• FOREIGN KEY - Uniquely identifies a row/record in another
table
• CHECK - Ensures that all values in a column satisfies a
specific condition
• DEFAULT - Sets a default value for a column when no value
is specified
NOT NULL Constraint
• By default, a column can hold NULL values.
• The NOT NULL constraint enforces a column to NOT accept
NULL values.
• This enforces a field to always contain a value, which means
that you cannot insert a new record, or update a record
without adding a value to this field.
CREATE TABLE
CREATE TABLE Persons ( ID int NOT NULL,LastName
varchar(25) NOT NULL, FirstName varchar(25) NOT NULL,
Age int);
NOT NULL on ALTER TABLE
• ALTER TABLE Persons MODIFY Age int NOT NULL;
UNIQUE Constraint
• The UNIQUE constraint ensures that all values in a column are
different.
• Both the UNIQUE and PRIMARY KEY constraints provide a
guarantee for uniqueness for a column or set of columns.
• A PRIMARY KEY constraint automatically has a UNIQUE constraint.
UNIQUE on CREATE TABLE
• CREATE TABLE Persons (ID int NOT NULL,LastName
varchar(25) NOT NULL, FirstName varchar(25), Age int,
UNIQUE (ID));
UNIQUE Constraint on ALTER TABLE
• ALTER TABLE Persons ADD UNIQUE (ID);
DROP a UNIQUE Constraint
• ALTER TABLE Persons DROP INDEX UC_Person;
PRIMARY KEY Constraint
• The PRIMARY KEY constraint uniquely identifies each record in a table.
• Primary keys must contain UNIQUE values, and cannot contain NULL values.
• A table can have only ONE primary key; and in the table, this primary key can
consist of single or multiple columns (fields).
PRIMARY KEY on CREATE TABLE
• CREATE TABLE Persons (ID int NOT NULL, LastName varchar(255) NOT NULL,
FirstName varchar(255), Age int, PRIMARY KEY (ID) );
PRIMARY KEY constraint on multiple columns
CREATE TABLE Persons (ID int NOT NULL, LastName varchar(25) NOT NULL,
FirstName varchar(25), Age int,
CONSTRAINT PK_Person PRIMARY KEY (ID,LastName) );
PRIMARY KEY on ALTER TABLE
• ALTER TABLE Persons ADD PRIMARY KEY (ID);
DROP a PRIMARY KEY
• ALTER TABLE Persons DROP PRIMARY KEY;
CHECK Constraint
• The CHECK constraint is used to limit the value range that can be placed in a column.
• If you define a CHECK constraint on a single column it allows only certain values for this column.
• If you define a CHECK constraint on a table it can limit the values in certain columns based on
values in other columns in the row.
CHECK on CREATE TABLE
• CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName
varchar(255), Age int, CHECK (Age>=18));
• CREATE TABLE Persons (ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(25),
Age int, City varchar(25), CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes‘));
CHECK on ALTER TABLE
• To create a CHECK constraint on the "Age" column when the table is already created, use the
following SQL:
• ALTER TABLE Persons ADD CHECK (Age>=18);
DROP a CHECK Constraint
• ALTER TABLE Persons DROP CONSTRAINT CHK_PersonAge
DEFAULT Constraint
• The DEFAULT constraint is used to provide a default value for a column.
• The default value will be added to all new records IF no other value is
specified.
DEFAULT on CREATE TABLE
• CREATE TABLE Persons (ID int NOT NULL, LastName
varchar(255) NOT NULL,
FirstName varchar(255), Age int, City
varchar(255) DEFAULT 'Sandnes’);
DEFAULT on ALTER TABLE
• ALTER TABLE Persons
ALTER City SET DEFAULT 'Sandnes';
DROP a DEFAULT Constraint

• ALTER TABLE Persons


ALTER City DROP DEFAULT;
FOREIGN KEY Constraint
• A FOREIGN KEY is a key used to link two tables together.
• A FOREIGN KEY is a field (or collection of fields) in one table that refers to the
PRIMARY KEY in another table.
• The table containing the foreign key is called the child table, and the table
containing the candidate key is called the referenced or parent table.
FOREIGN KEY on CREATE TABLE
• CREATE TABLE Orders (OrderID int NOT NULL,OrderNumber int NOT NULL,
PersonID int, PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID));
FOREIGN KEY on ALTER TABLE
• ALTER TABLE Orders ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
DROP a FOREIGN KEY Constraint
• ALTER TABLE Orders DROP FOREIGN KEY FK_PersonOrder;
Types Of SQL Commands : Introduction

• SQL Categorizes its commands on the basis of


functionalities performed by them. There are
five types of SQL Commands which can be
classified as:DDL(Data Definition Language).
• DML(Data Manipulation Language).
• DQL(Data Query Language).
• DCL(Data Control Language).
• TCL(Transaction Control Language).
Remember : Multiple Row Functions also called Aggregate
functions/Group functions.
Types of Single Row Function
1. Mathematical functions : POW(), SQRT(), ROUND(),
TRUNCATE() etc.

2. String functions : LOWER(),UPPER(),LENGTH(),TRIM(),


ASCII(), SUBSTR()/MID(),INSTR() etc.

3. Date & Time functions : CURDATE(), CURTIME(), NOW(),


SYSDATE(), DAYNAME(), MONTHNAME(), DAYOFWEEK(),
YEAR(), MOTH(), DAY(), HOUR(), MINUTE(), SECOND() etc.
Multiple Row/Group/Aggregate function
• Sum()
• Avg()
• Max()
• Min()
• Count()
➢All Group functions ignore
NULL values

You might also like