PYTHON MySQL
Front End SQL Data Base
Data Type Description
CHAR(n) Fixed-length string of n characters
VARCHAR(n) Variable-length string up to n
INT Integer (whole number)
FLOAT Decimal number with fractional part
DATE Stores date in YYYY-MM-DD format
Constraint Purpose
NOT NULL Field cannot be left empty
UNIQUE All values must be different
PRIMARY KEY Uniquely identifies each row (NOT NULL + UNIQUE)
DEFAULT A default value specified for the column if no value is provided
FOREIGN KEY The column which refers to value of an attribute defined as primary key in
another table
Creating a new data base
SQL
Select Existing Database
SQL USE dbname;
Display the list of existing database
SQL SHOW DATABASES;
Delete Existing databse
SQL DROP DATABASE school;
Creating a new Table
SQL
Show list of existing Table
SQL
Display Structure/information of Table
SQL SQL
Delete entire table from database
SQL
ALTER TABLE SQL ALTER TABLE TABLE NAME Operation
Operation
ALTER TABLE student 6 Add Primary ALTER TABLE student
1 Add Column
ADD email VARCHAR(50); Key/ Unique ADD PRIMARY KEY(id);
2 7
Remove/ ALTER TABLE student ALTER TABLE student
Add Unique key
Delete Column DROP COLUMN email; ADD UNIQUE (id);
ALTER TABLE student 8
3 Change Add foreign key ALTER TABLE exam ADD FOREIGN
Column name CHANGE COLUMN
old->new to a relation KEY(admno) REFERENCES student (id);
sid admno int;
4 Change Column ALTER TABLE student 9
Remove ALTER TABLE student
Data Type MODIFY COLUMN
Primary Key DROP PRIMARY KEY;
mobileno varchar(15);
Modify ALTER TABLE student ALTER TABLE student
5 constraint of a
10 Modify constraint
column: NOT MODIFY COLUMN mobileno of a column: MODIFY COLUMN mobileno
NULL varchar(15) NOT NULL; DEFAULT varchar(15) DEFAULT ‘999999999’;
Insert Data into Table
To insert data into table, “insert” query is used,
let consider the table structure, Table name: “student”, Colums: Rollno, Name, Class, Subject, Pr, Th
Method 1: Insert Data into Table using positional arguments
Query
In above query, value1 stores to first column, value2 in second column and so on.
Example
Insert Data into Table
To insert data into table, “insert” query is used,
let consider the table structure, Table name: “student”, Colums: Rollno, Name, Class, Subject, Pr, Th
Method 2: Insert Data into Table using Labelled arguments
Query
In above query data stored according to sequence of column definition,
value1 stores to first column, value2 in second column and so on.
Example-1
Above example save data as same as positional argument, as here the column of sequence is same but, if we
change the sequence, or we need to skip any column, we use labelled argument method
Example-2
Insert Data into Table
To insert data into table, “insert” query is used,
let consider the table structure, Table name: “student”, Colums: Rollno, Name, Class, Subject, Pr, Th
Method 3: bulk data allocation method
Query
In bulk data writing, we extending more data tuple one after another by separating commas
Example
The same we can use with labelled data insertion query method
Modify/Update/Change Data of Table
To modify existing data of table, “update” query is used,
let consider the table structure, Table name: “student”, Colums: Rollno, Name, Class, Subject, Pr, Th
Query
Example-1
Example-2
Example-3
Delete Data of Table
To Delete existing data of table, “Delete” query is used,
let consider the table structure, Table name: “student”, Colums: Rollno, Name, Class, Subject, Pr, Th
Query
Delete Single Data row
Example-1
Delete Multiple Data row
Example-2