0% found this document useful (0 votes)
59 views

Notes On MYSQL

The document provides an overview of concepts related to MySQL including that a table contains rows and columns of data, a relation is another name for a table, and a database is a collection of logically related data along with descriptions; it also describes SQL commands for manipulating databases, different data types in SQL, and how to connect to a MySQL database from Python using the MySQL connector library.

Uploaded by

Namhaa Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Notes On MYSQL

The document provides an overview of concepts related to MySQL including that a table contains rows and columns of data, a relation is another name for a table, and a database is a collection of logically related data along with descriptions; it also describes SQL commands for manipulating databases, different data types in SQL, and how to connect to a MySQL database from Python using the MySQL connector library.

Uploaded by

Namhaa Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Notes on MYSQL

Table: Collection of rows and columns that contain useful data and information

Relation: A table in mysql is called relation

Database: Collection of logically related data along with its description is called database

Tuple: A row in a relation

Attribute: A column in a relation

Degree: Number of attributes in a relation

Cardinality: Number of tuples in a relation

Primary Key: Key that uniquely identifies the tuples in a relation , It cannot be duplicated or Null

Foreign key: It is a key that is defined as a primary key in another relation. It is used to enforce
referential integrity in RDBMS.

Candidate Key: Set of all attributes which can serve as P.K

Alternate key: All the candidate keys other then the P.K of a relation are Alternate keys

DBA: Data Base Administrator is a person that is responsible for defining the data base schema

Relational Algebra: It is a set operation such as select, project, union, cartesian product etc.

Select Operation: Yields set of rows depending upon the condition.

Project Operation: Yields set of columns as result which is specified.

Union Operation: Two relations are said to be union compatible if the degree and column is same.

Cartesian Product: Cartesian product of two relation gives resultant relation whose no of column are
sum of degrees of two relations and no of rows are product of cardinality of 2 relations.

SQL: Structured Query Language

It is a non procedural language that is used to create, manipulate and process the databases.

Features

-Easy to learn and use -large volume of databases handled easily


Processing Capabilities of SQL

DATA DEFINITION LANGUAGE (ddl)

- Data definition
- It contains commands that are used to create the tables, databases, indexes etc.
- Eg. Create Table <table name> , alter table

Data Manipulation Language (dml)

- Data manipulation
- Contains commands that are used to manipulate database objects.
- Eg. Select, Insert, Delete, Update

Data Control Language(DCL)

- This is used for controlling the access to the data, Various commands like GRANT REVOKE are
available in DCL.

Transaction Control Language

- Commands to control he transactions in a data base system.

DATA TYPES OF SQL

NUMBER- stores numeric value in a field.

CHAR- stores character type data in column

VARCHAR- Stores variable length alphanumeric data

DATE- stores dates in columns

SQL COMMANDS

Create Table Command

It is used to create a table in MYSQL. It is a DDL type of command.

Eg CREATE TABLE <table name>

Constraints

The conditions that can be enforced on the field of relations.

They are used to ensure integrity of a relation , hence called integrity constraints

-NOT NULL CONSTRAINT: It ensures that a column cannot contain a NULL value
-UNIQUE CONSTRAINT: A candidate key is a combination of uniquely identified attributes which
identifies tuple of a relation

-PRIMARY KEY: It ensures : 1) Unique Identification in each tuple 2)No attribute part of the Primary
key constraint contains a NULL value

-FOREIGN KEY: Establishes a relationship with a primary key in another relation

-CHECK CONSTRAINT: Sometimes , we may require values to be within a certain range or they
satisfy with conditions.

DATA MANIPULATION IN SQL

DML COMMANDS:

SELECT – Used for making queries

INSERT – Used to add new records in a relation

UPDATE – Used to modify existing data in a table

DELETE – Used to delete records

ALTER – Used to change the structure of the database

OPERATORS IN SQL:

1) Arithmetic : +, -, *, /
2) Relational: =, <, >
3) Logical: OR, AND, NOT
4) Range check: Low and High
5) List check: in
6) Pattern check: like, not like

QUERIES

To retrieve information from a database, we can query the database

PROJECTION : it is the capability of SQL to return only specific attributes in the relation.

ELIMINATING DUPLICATE DATA

Distinct keyword is used to restrict the duplicate rows from the results of a select statement.

CONDITIONS BASED ON RANGE

Between Operator : It defines a range of values that the column value must fall in
CONDITION BASED ON LIST

IN operator is used to select values that match any value in given list.

ORDER BY CLAUSE

- It is used to display the result of a query in a specific order


AGGREGATE OR GROUP FUNCTIONS
1) SUM(): returns the sum of values
2) AVG(): returns the average of values
3) MIN(): returns the minimum values
4) MAX(): returns the maximum values
5) COUNT(): returns the number of rows in a relation
6) Round(): returns rounded off values from rows

GROUP BY CLAUSE:

- The rows of a table can be grouped together based on a common value. It Is used in select
statements

GROUP BY HAVING:

- It is used to apply condition to the GROUP BY clause

DELETE COMMAND

- It deletes records from a table


DROP COMMAND
- It deletes a table from the database
UPDATE COMMAND
- It is used to update the existing data stored in database
CREATE VIEW COMMAND
- Used to create a view of the existing table that contains specific attributes of the table
View table is a virtual table that does not contain data of its own and derives data from other
tables.
ALTER TABLE
- It is used to change the structure of the database.

REMOVING A COLUMN

ALTER TABLE <TABLE NAME>

DROP COLUMN column_name;

DROP A TABLE

DROP TABLE <TABLE NAME>


Computer Networks:

- To connect Database you need a framework that facilitates programming application and
DBMS.
- To connect from Python to MYSQL, You need library called MYSQL connector.
- Must import mysql.connector before writing code of connectivity.
- Steps
- 1) Start Python
- 2) Import Package
- 3) Open connection : mycon= mysql.connector.connect(host=’’, user=’’, passwd=’’,
database=’’)
- i. So example: Import mysql.connector as a
- Mycon=a.connect(………………………………………)
- 4) Create Cursor: cursor=mycon.cursor()
- 5) Execute querys:
- Example- d1=SELECT * FROM MobileMaster
- Cursor.execute(d1)
- 6) Extract data from result set: Fetchall(), Fetchmany(n), fetchone()
- Example : cursor.fetchall()

->Connect(): Establishing Data Connection


->Cursor(): Makes it possible to define a result set and perform logic row by row.
->Execute(): To execute an SQL query.

->A database connection object controls the connection to the database.


->A database cursor is a special control structure that facilitates row by row processing of
records in result set.
->Resultset is a logical set of records that are fetched from database

For INSERT, UPDATE, DELETE queries, you must run commit() with connection object.

Computer Networks in Notebook


Redoing Test sheet on UPDATE, ALTER , DELETE commands

1) CREATE DATABASE University;


2) USE DATABASE University;
3) CREATE TABLE Campus
-(Campus_ID int(4) Primary Key,
-Campusname varchar(20) NOT NULL,
-Campusaddress varchar(50) UNIQUE,
-Campusdiscount float(4,2) CHECK(Campusdiscount>150));

4) DESC Campus;

5)INSERT INTO CAMPUS (Campus_ID, Campusname, CampusAddress, Campusdiscount)VALUES

-(1222,”Nagoya University”,”2-2-1, Komaki,Aichi”, 22.43);

6)ALTER TABLE Campus Modify ( Campus Discount float(10,2));

7)DESC Campus

8)ALTER TABLE Campus CHANGE (Campusaddress CAddress varchar(20));

Import mysq.connector as a

Mycon=a.connect(host=’localhost’, user=”root”, passwd=’’, database=’’)

If (mycon.is_connected()):

Print(“Okay”)

Else:

Print(“not okay”)

Cursor=mycon.cursor()

D1=”query”

Cursor.execte(d1)

Records=cursor.fetchone

For record in records

Print(record)

Countofrec=cursor.rowcount
Print(total thingy is”,countofrec)

You might also like