sql
sql
DDL stands for Data Definition Language and refers to SQL commands used
to create, modify, and delete database structures such as tables, indexes,
and views. DML stands for Data Manipulation Language and refers to SQL
commands used to insert, update, and delete data within a database. Now,
let’s learn about the DDL and DML commands in depth.
Snega is a talented programmer who has been eager to learn more about
the various aspects of Python programming. One day, she comes across a
tutorial about databases and SQL queries. She is particularly interested in
learning about the basics of databases, such as Data Definition Language
(DDL) and Data Manipulation Language (DML). She eagerly starts reading
the tutorial. Lets help her in the journey.
Explore the difference between DDL and DML commands in the below table.
Understand how DDL commands shape database structures, while DML
commands manipulate data within the database.
DDL DML
Used to define database objects like Used to manipulate data within the
tables, indexes, views, etc. database.
Changes made using DDL affect the Changes made using DML affect the
structure of the database. data stored in the database.
DDL statements are typically used during DML statements are used during
the design and setup phase of a database. normal operation of a database.
Domain is a set of values from which an attribute can take value in each row. For
example, roll no field can have only integer values and so its domain is a set of
integer values
PRIMARY
KEY—
UNIQUE
KEY ,LIKE
EMPID
ONE
TABLE
CAN HAVE
ONLY ONE
PRIMARY
KEY
IF WE
ADD
MORE
THAN
ONE KEY
THAT IS CALLED composite KEY.
Green marked columns can be unique key
no common keys above so
DDL---DATA DEFINITION LANGUAGE
STRUCTURE OF A TABLE IS CALLED
SCHEMA
COMMANDS USED ARE :
Show , use & desc
SHOW DATABASES;
SHOW TABLES;
show databases;
use information_schema;
show tables;
desc APPLICABLE_ROLES ;
desc ENGINES ;
-- insert
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept TEXT NOT NULL
);
INSERT INTO EMPLOYEE VALUES (0001, 'Clark', 'Sales');
INSERT INTO EMPLOYEE VALUES (0002, 'Dave', 'Accounting');
INSERT INTO EMPLOYEE VALUES (0003, 'Ava', 'Sales');
-- fetch
SELECT * FROM EMPLOYEE WHERE dept = 'Sales';
Date –‘yyy-mm-dd’
Logical- t/f
Difference between Char & varchar
Can be a combination of alphabets ,digits ,special symbols ,spaces
etc.
Ex: Char(20)..if we are using only 10 letters ..rest of the memory
gets wasted
Varchar(20)—return rest of the space . if 10 characters are used .
Ruchi have ---5 character length and I have given
varchar(20)..rest of the space is free.it will use only space of 5
characters .no memory is wasted.
Example
The following SQL deletes the "Email" column from the "Customers" table:
Example
ALTER TABLE Customers
DROP COLUMN Email;
SQL Server:
Ans: 6,20
Ans:8,15
The degree of the Cartesian product is the sum of the degrees of the two tables. The cardinality
of the Cartesian product is the product of the cardinalities of the two tables
Ans: C
Ans:
Ans:
Q
Ans:
Q
Ans:
Q
Ans:
Ans 1)
Q Define the term Domain with respect to RDBMS. Give one example to support your answer.
Ans:
Domain is a set of values from which an attribute can take value in each row. For example, roll no field
can have only integer values and so its domain is a set of integer values
Note: Any other correct logic may be marked ½ mark for correct definition ½ mark for correct example
Write any two aggregate functions in SQL with an appropriate example. (2)
OR
Write two commands each of DDL and DML commands in SQL.
(b) The code given below inserts the following record in the table Fun_City:
Ticket_Id – integer
Name – string
Ticket_Price – integer
No_Of_Tickets – integer
Total_Amount – integer
Note the following to establish connectivity between Python and MySQL:
• Username is root
• Password is Ticket
• The table exists in a MySQL database named Amusement.
• The details (Ticket_Id, Name, Ticket_Price, No_of_Tickets) are to be accepted by the user.
Write the following missing statements to complete the code:
Statement 1 – To form the cursor object
Statement 2 – Write a command to execute the query
Statement 3 – Write a command to save data permanently in the database.
import mysql.connector as mysql
def sql_data():
con=mysql.connect(host="localhost",user="root",password="Ticket",
database="Amusement")
mycursor=_________________ #Statement 1
Ticket_No=int(input("Enter Ticket Number :: "))
Name=input("Enter Name :: ")
Ticket_Price= input("Enter Ticket Price :: ")
No_Of_Tickets=int(input("Enter No of tickets :: "))
Total_Amount= Ticket_Price * No_Of_Tickets
query="Insert into Fun_City values({},'{}',{},{})".format(Ticket_No, Name,
Ticket_Price, No_Of_Tickets, Total_Amount)
____________________ #Statement 2
______________________ #Statement 3
print("Data Added successfully")