Notes On MYSQL
Notes On MYSQL
Table: Collection of rows and columns that contain useful data and information
Database: Collection of logically related data along with its description is called database
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.
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.
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.
It is a non procedural language that is used to create, manipulate and process the databases.
Features
- Data definition
- It contains commands that are used to create the tables, databases, indexes etc.
- Eg. Create Table <table name> , alter table
- Data manipulation
- Contains commands that are used to manipulate database objects.
- Eg. Select, Insert, Delete, Update
- This is used for controlling the access to the data, Various commands like GRANT REVOKE are
available in DCL.
SQL COMMANDS
Constraints
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
-CHECK CONSTRAINT: Sometimes , we may require values to be within a certain range or they
satisfy with conditions.
DML COMMANDS:
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
PROJECTION : it is the capability of SQL to return only specific attributes in the relation.
Distinct keyword is used to restrict the duplicate rows from the results of a select statement.
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
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:
DELETE COMMAND
REMOVING A COLUMN
DROP A TABLE
- 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()
For INSERT, UPDATE, DELETE queries, you must run commit() with connection object.
4) DESC Campus;
7)DESC Campus
Import mysq.connector as a
If (mycon.is_connected()):
Print(“Okay”)
Else:
Print(“not okay”)
Cursor=mycon.cursor()
D1=”query”
Cursor.execte(d1)
Records=cursor.fetchone
Print(record)
Countofrec=cursor.rowcount
Print(total thingy is”,countofrec)