Introduction to SQL
Introduction to SQL
Structured Query Language (SQL) is the standard language used to manage and manipulate
databases. It is used to interact with relational database management systems (RDBMS),
allowing users to perform various operations like querying data, updating records, managing
schema (structure of the database), and controlling access to data. SQL works by executing
commands, known as SQL statements, which can retrieve, insert, update, delete, or define data
within a database.
Relational Databases: SQL primarily operates on relational databases, where data is organized
into tables with rows and columns.
Tables: A table is a collection of related data entries and consists of columns and rows.
Rows and Columns: Rows represent records, and columns represent attributes or fields of data.
Queries: SQL is often used to query a database, meaning to request specific information from it.
SQL statements are organized into categories based on the kind of operations they perform. The
main types of SQL statements include:
This category primarily deals with querying data from the database. It allows users to retrieve
information stored in a table. The most common DQL statement is:
SELECT: Used to retrieve data from a database. It can filter, sort, and manipulate data based on
conditions.
DML is used to manipulate data stored in the tables. It involves inserting, updating, and deleting
data.
DCL statements manage access to data within the database, such as granting or revoking user
privileges.
TCL manages the changes made by DML statements. It allows users to control transactions to
ensure the integrity of data.
Steps:
1. Open the MySQL Command-Line
To begin, open the terminal (on Linux/Mac) or Command Prompt (on Windows) and connect to
your MySQL server.
Replace username with your MySQL username (e.g., Tyagi or another user with database
creation privileges). You'll be prompted to enter your password.
After connecting, create a new database using the CREATE DATABASE statement:
(In this I have already connected my SQL so I created a new database within mysql directly)
3.) To view if Databases are created or not use the command Show Databases;
How to create a table, Alter table?
The following example creates a table called "Workers" that contains five columns: WID,
LastName, FirstName and Place:
(Remember Varchar is used for Strings like Ansh tyagi , Umang here these both are in string
format to enter the number or digit we use Int function after using Varchar we can add word limit
like for a column how many words could be entered)
How to Alter a table ?
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing
table.
The above one is for changing the name of the table directly.
How to Insert Query and Update Query in MySQL ?
Inserting and updating data in a MySQL database is done using the INSERT INTO and UPDATE
SQL statements, respectively.
The INSERT INTO statement is used to add new records (rows) to a table.
Steps:
In the above table you can see that 2 is missing but from the images above you can see that there
was a row in which the first name of the person was Robert. So here delete function deleted the
whole row and its entries and thus we can see 3 directly after 1.
The following SQL statement deletes all rows in the "Customers" table, without deleting the
table:
This will delete all the rows in the table without impact the column
Select is the query, Customers is the name of the table in the above script.
In the above script Select is the query to choose the column , from directs to table_name and
where is also the query after that you write the condition like the name , no. etc.
In this above table the entry 5 fulfilled the condition of the resident of UAE.
How to use Order By Clause?
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
By default, it uses Ascending order only but you can change that by using ASC AND DESC.
In this above figure I used both ASC and DESC command at the same time to get the output you
can use them separately as well as your needs.
How to use Drop Table and Truncate Table?
The DROP TABLE statement is used to drop an existing table in a database.
The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table
itself.
The truncate deleted the data inside the table not the table itself that’s the difference between
Drop table and Truncate table. All the properties of the table remain safe except the data inside it.
Truncate won’t work in the online compiler it will only work in the MySQL app only.
SQL Questions
Question: Create the Students table with column ID INT PRIMARY KEY, Name VARCHAR
(50), Subject VARCHAR (50), Marks DECIMAL (5, 2).
To add the column, we will be using the alter function and adding the Enrollment date column.
We will use the Rename function in order to change the name of the table directly.
In the above code we Inserted the entries using Insert query then entering the column names in
which we entered the values then executed the script.
The Entity-Relationship Model (ER Model) is a conceptual framework used in database design
to visually represent the structure of a database. It provides a high-level view of how different
data elements (entities) relate to one another, making it easier to organize and plan a relational
database before its implementation.
1. Entity: An entity represents a real-world object or concept that has a distinct existence in
the domain being modeled. Each entity is typically a table in the database.
o Simple Attribute: Cannot be divided into smaller parts (e.g., Age, ID).
o Composite Attribute: Can be subdivided into smaller parts (e.g., Full Name can
be divided into First Name and Last Name).
o Derived Attribute: Can be calculated from other attributes (e.g., Age can be
derived from Date of Birth).
Example: A "Student" entity might have attributes such as StudentID, Name, Age, and
EnrollmentDate.
3. Relationship: A relationship describes how two or more entities are associated with
each other. It defines the logical connection between entities and can be of various types:
o One-to-One (1:1): One entity is associated with exactly one instance of another
entity.
Example: Each student has one student ID card, and each student ID card
belongs to only one student.
o Many-to-Many (M): Multiple instances of one entity are associated with multiple
instances of another entity.
Example: Students enroll in many courses, and courses can have many
students.
4. Cardinality: Cardinality defines the number of instances of one entity that can or must
be associated with each instance of another entity. The common types of cardinality are:
5. Primary Key: A primary key is an attribute (or set of attributes) that uniquely identifies
each entity in a table. Every entity instance must have a unique value for its primary key.
Example: In a Student entity, the primary key might be StudentID, as it uniquely identifies each
student.
6. Foreign Key: A foreign key is an attribute in one entity that refers to the primary key of
another entity. It is used to represent relationships between entities in a database.
Example: In a Course table, the ProfessorID could be a foreign key referring to the Professor
table's ProfessorID.
ER Diagram Symbols:
Foreign Key: Often represented by a line connecting two entities with cardinality
indications (1, M).
ER MODEL FOR
Use this code to create the tables then we will build the relation between them using the
keys.
Entities:
1. Customer:
2. Product:
3. Order:
4. Order_Item:
First, we created the list of tables which we need to establish the connection then Inserted the
data in each table.
Entities:
Product: Each product can be included in multiple orders, and it is tracked in the
inventory.
The above shows the role of each element and below shows the relation between each element
using a common key.
C. Student Enrollment System:
ER Diagram Overview
Entities:
Course: Each course can have multiple students and is assigned to a specific department.
How to Use: