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

SQL

The document provides a comprehensive overview of SQL commands, including Data Definition Language (DDL) and Data Manipulation Language (DML) commands for creating, altering, and managing databases and tables. It also covers aggregate functions, foreign key operations, and database connectivity using pymysql in Python. Key operations such as inserting, updating, and deleting records, as well as transaction management, are highlighted.
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)
11 views

SQL

The document provides a comprehensive overview of SQL commands, including Data Definition Language (DDL) and Data Manipulation Language (DML) commands for creating, altering, and managing databases and tables. It also covers aggregate functions, foreign key operations, and database connectivity using pymysql in Python. Key operations such as inserting, updating, and deleting records, as well as transaction management, are highlighted.
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/ 6

‭ onstraints:‬

C
‭Unique‬
‭Primary key‬
‭Fees decimal‬‭default‬‭1000‬
‭Check‬‭fees >= 1000‬
‭Not null‬
‭DDL Commands‬

‭CREATE DATABASE <database name>;‬

‭USE <database name>;‬

‭ REATE TABLE student(rollno integer primary key, name char(20), stream‬


C
‭char(30));‬

‭DROP DATABASE stu;‬

‭SHOW TABLES;‬

‭DESC stu;‬

‭PDATE‬‭
U table_name‬
SET‬‭
‭ column1‬‭
=‬‭
value1‬
,‬‭
‭ column2‬‭
=‬‭
value2‬
, ...‬

WHERE‬‭
‭ condition‬
;‬

I‭ ) Adding column:‬
‭ALTER TABLE<TABLE NAME> ADD (<COLUMN_NAME><DATA‬
‭TYPE>[SIZE]);‬

I‭ I) Adding column with default value‬


‭ALTER TABLE<TABLE NAME> ADD (<COLUMN_NAME><DATA‬
‭TYPE>[SIZE] DEFAULT <VALUE>);‬

I‭ II) Modifying definition of an attribute (changing the size or datatype)‬


‭ALTER TABLE<TABLE NAME> MODIFY <COLUMN_NAME><DATA‬
‭TYPE>[SIZE];‬

i‭v) Renaming a column‬


‭ALTER TABLE<TABLE NAME> CHANGE‬
‭<OLD_COLUMN_NAME>to<NEW_COLUMN_NAME><DATA TYPE>;‬

‭ ). Removing a column ALTER TABLE<TABLE NAME> DROP‬


V
‭<COLUMN_NAME>;‬

‭ i) Adding and changing Primary key constraints‬


v
‭ALTER TABLE<TABLE NAME> ADD PRIMARY KEY‬
‭(<COLUMN_NAME><DATA TYPE>[SIZE]);‬
‭ ii) Remove the primary key column ALTER TABLE<TABLE NAME> DROP‬
v
‭PRIMARY KEY;‬

‭ iii) Drop table DROP TABLE<TABLE NAME>;‬


v
‭DML Commands‬

‭Insert into table name [<column list>] values (<value 1,value2….);‬

‭ elect <column name or *> from table where (columnname) >= 45 order by (column‬
S
‭name);‬
‭Where in (hello, no);‬

‭ ggregate functions:‬
A
‭avg – to compute the average value‬
‭min – to find the minimum value‬
‭max – to find the maximum value‬
‭Sum – to find the total value‬
‭Stddev – to find the standard deviation‬
‭Count(column) – to count non-null values in a column‬
‭Count(*) – to count the total number of rows in a table‬

‭Select Department, count(*) from employee group by department;‬

‭ OREIGN KEY‬
F
‭SELECT M1.M_Id, M1.M_Name, M2.M_Qty, M2.M_Supplier FROM MobileMaster‬
‭M1, MobileStock M2 WHERE M1.M_Id=M2.M_Id AND M2.M_Qty>=300;‬

‭DELETE FROM student WHERE stream = “Humanities’;‬


‭CONNECTIVITY‬
‭Connectivity‬
‭import pymysql.cursors‬
‭con = pymysql.connect(host = "localhost", user = "root", password = "Tech#2021")‬
‭a = con.cursor()‬

‭a.commit()‬
‭End current transaction and make all changes performed in the transaction permanent‬

‭a.rollback()‬
‭Undoes every command till after commit (since those are permanent now)‬

‭a.rowcount()‬
‭Returns the number of rows updated, inserted, deleted… ?‬

‭a.fetchone()‬
‭Retrieves the next row of a query result set and returns a single sequence‬
‭[reading data one by one]‬

‭a.fetchall()‬
‭Fetches all rows from the last executed statement in a tuple‬

‭a.fetchmany(n)‬

‭a.close()‬
‭Closes the connection‬

You might also like