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

Database Related Questions

Db

Uploaded by

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

Database Related Questions

Db

Uploaded by

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

Database Related Questions

& Answers

1.What is a database?

A database is basically a collection of structured data in such a way that it can easily be
retrieved, managed and accessed in various ways. One of the simplest forms of databases
is a text database. Relational databases are the most popular database system which
includes the following:

 MySQL
 Oracle Database
 SQL server
 Sybase
 Informix
 IBM db2
 NO SQL

Among all these databases, MySQL is one of the easiest databases to work with. Let me
walk you through about this in detail.

2 .What is MySQLdb?

MySQLdb is an open-source freely available relational database management system that


uses Structured Query Language. Now one of the most important question here is “What is
SQL?”
SQL (Structured Query Language) is a standard language for relational databases that allow
users to do various operations on data like, Manipulating, Creating, Dropping, etc. In a
nutshell, SQL allows you to do anything with the data.
Let’s move ahead and dive deep into Python database connection wherein you will learn
how to connect with the database.
3. How does Python connect to a database?
It is very simple to connect Python with the database. Refer the below image which
illustrates a Python connection with the database where how a connection request is sent to
MySQL connector Python, gets accepted from the database and cursor is executed with
result data.

4 .Database Operations[CRUD]:

There are numerous operations a programmer can perform using databases and SQL in
order to have sound knowledge of database programming and MySQL.
I have demonstrated the CRUD operations below

o Create– It is an SQL statement used to create a record in the table or can say it is
used for creating a table.
o Read- It is used for fetching useful information from the database.
o Update- This particular SQL statement is used for updating the records in the table
or updating the table.
o Delete- As the name itself justifies this command is used for deleting the table.
5. What is the difference between MySQL and SQL?

SQL is known as the standard query language. It is used to interact with the database like
MySQL. MySQL is a database that stores various types of data and keeps it safe.

A PHP script is required to store and retrieve the values inside the database.

6. What is the difference between the database and the table?

There is a major difference between a database and a table. The differences are as follows:

o Tables are a way to represent the division of data in a database while the database is
a collection of tables and data.

o Tables are used to group the data in relation to each other and create a dataset. This
dataset will be used in the database. The data stored in the table in any form is a part
of the database, but the reverse is not true.

o A database is a collection of organized data and features used to access them,


whereas the table is a collection of rows and columns used to store the data.

7 .Why do we use the MySQL database server?

First of all, the MYSQL server is free to use for developers and small enterprises.

MySQL server is open source.

MySQL's community is tremendous and supportive; hence any help regarding MySQL is
resolved as soon as possible.

MySQL has very stable versions available, as MySQL has been in the market for a long
time. All bugs arising in the previous builds have been continuously removed, and a very
stable version is provided after every update.

8 .What are the different tables present in MySQL?

There are many tables that remain present by default. But, MyISAM is the default database
engine used in MySQL. There are five types of tables that are present:

o MyISAM
o Heap

o Merge

o INNO DB

o ISAM

9 .How to install MySQL?

Installing MySQL on our system allows us to safely create, drop, and test web applications
without affecting our live website's data. There are many ways to use MySQL on our system,
but the best way is to install it manually. The manual installation allows us to learn more
about the system and provides more control over the database. Manual installation of
MySQL has several benefits:

o Backing up, reinstalling, or moving databases from one location to another can be
achieved in a second.

o It provides more control to how and when MySQL server starts and closes.

o We can install MySQL anywhere, like in a portable USB drive.

10 .How to check the MySQL version?

We can check the MySQL version on Linux using the below command:
mysql -v

If we use the MySQL in windows, opening the MySQL command-line tool displayed the
version information without using any flags.

11 .How to add columns in MySQL?

A column is a series of cells in a table that stores one value for each row in a table. We can
add columns in an existing table using the ALTER TABLE statement as follows:
ALTER TABLE table_name

ADD COLUMN column_name column_definition [FIRST|AFTER existing_column];

12 .How to delete a table in MySQL?


Answer:-

We can delete a table in MySQL using the Drop Table statement. This statement removes
the complete data of a table, including structure and definition from the database
permanently. Therefore, it is required to be careful while deleting a table. After using the
statement, we cannot recover the table in MySQL. The statement is as follows:

DROP TABLE table_name;

13 .How to add foreign keys in MySQL?

The foreign key is used to link one or more tables together. It matches the primary key field
of another table to link the two tables. It allows us to create a parent-child relationship with
the tables. We can add a foreign key to a table in two ways:
o Using the CREATE TABLE Statement
o Using the ALTER TABLE Statement

Following is the syntax to define a foreign key using CREATE TABLE OR ALTER TABLE
statement:
[CONSTRAINT constraint_name]
FOREIGN KEY [foreign_key_name] (col_name, ...)
REFERENCES parent_tbl_name (col_name,...)

14 .How to import a database in MySQL?

Importing database in MySQL is a process of moving data from one place to another place.
It is a very useful method for backing up essential data or transferring our data between
different locations. For example, we have a contact book database, which is essential to
keep it in a secure place. So we need to export it in a safe place, and whenever it lost from
the original location, we can restore it using import options.

In MySQL, we can import a database in mainly two ways:


o Command Line Tool
o MySQL Workbench

15 .How to change the column name in MySQL?

While creating a table, we have kept one of the column names incorrectly. To change or
rename an existing column name in MySQL, we need to use the ALTER TABLE and
CHANGE commands together. The following are the syntax used to rename a column in
MySQL:
ALTER TABLE table_name
CHANGE COLUMN old_column_name new_column_name column_definition [FIRST|
AFTER existing_column];
Suppose the column's current name is S_ID, but we want to change this with a more
appropriate title as Stud_ID. We will use the below statement to change its name:
ALTER TABLE Student CHANGE COLUMN S_ID Stud_ID varchar(10);

16 .How to insert data in MySQL?

We can insert data in a MySQL table using the INSERT STATEMENT. This statement
allows us to insert single or multiple rows into a table. The following is the basic syntax to
insert a record into a table:
INSERT INTO table_name ( field1, field2,...fieldN )
VALUES ( value1, value2,...valueN );
If we want to insert more than one rows into a table, use the below syntax:
INSERT INTO table(field1, field2,...fieldN)
VALUES
(value1, value 2, ...),
(value1, value2, ...),
...
(value1, value2, ...);

17 .How to join two tables in MySQL?

We can connect two or more tables in MySQL using the JOIN clause. MySQL allows various
types of JOIN clauses. These clauses connect multiple tables and return only those records
that match the same value and property in all tables. The following are the four easy ways to
join two or more tables in MySQL:
o Inner Join
o Left Join
o Right Join
o Cross Join

18 .How to drop the primary key in MySQL?

MySQL primary key is a single or combination of the field used to identify each record in a
table uniquely. A primary key column cannot be null or empty. We can remove or delete a
primary key from the table using the ALTER TABLE statement. The following syntax is used
to drop the primary key:
ALTER TABLE table_name DROP PRIMARY KEY;

19. How to set auto increment in MySQL?

Auto Increment is a constraint that automatically generates a unique number while inserting
a new record into the table. Generally, it is used for the primary key field in a table. In
MySQL, we can set the value for an AUTO_INCREMENT column using the ALTER TABLE
statement as follows:
ALTER TABLE table_name AUTO_INCREMENT = value;

20 .What is the difference between TRUNCATE and DELETE in MySQL?

o TRUNCATE is a DDL command, and DELETE is a DML command.


o It is not possible to use Where command with TRUNCATE QLbut you can use it with
DELETE command.
o TRUNCATE cannot be used with indexed views, whereas DELETE can be used with
indexed views.
o The DELETE command is used to delete data from a table. It only deletes the rows
of data from the table while truncate is a very dangerous command and should be
used carefully because it deletes every row permanently from a table.

21.Explain the difference between a SQL database and a NoSQL database.

22. What is SQLite, and how is it different from other databases?

23. Explain the purpose of the sqlite3 module in Python.

24. What is an ORM, and how does it relate to databases in Python?

25. Explain the role of the PEP 249 specification in Python database programming

26.Explain parameterized queries and why they are important in database


programming.

27. What is the role of a database cursor?

28. What is the difference between a relational database and a non- relational
database?

29. What is the purpose of the commit() and rollback() methods in PostgreSQL
transactions?

30. How do you connect to a PostgreSQL database using Python?

You might also like