Skip to main content

MySQL Basics

What is a Database

A database is a container that stores related data in an organized way. In MySQL, a database holds one or more tables. Think of it like: 

Folder analogy :  A database is like a folder. Each table is a file inside that folder. The rows in the table are like the content inside each file. 

Excel analogy : A database is like an Excel workbook. Each table is a separate sheet inside that workbook. Each row in the table is like a row in Excel.

----------------------------------------------------------------------------------------------------------------------

Data Types in SQL :

1. INT : Stores integer type, used for whole numbers

2. VARCHAR(100) : Stores variable length string, upto 100 characters.

3. Date : Stores date values

4. BOOLEAN : Stores TRUE or FALSE

5. ENUM : A string object with a value chosen from a list of permitted values. eg. gender                   ENUM('Male', 'Female', 'Other')

Contraints :

1. AUTO_INCREMENT Automatically generates a unique number for each row. 

2.PRIMARY KEY : Uniquely identifies each row in the table. 

3. NOT NULL : Ensures a column cannot have a NULL value. 

4. UNIQUE : Ensures all values in a column are different. 

5. DEFAULT : Sets a default value for a column if no value is provided. 

----------------------------------------------------------------------------------------------------------------------

1. DDL (Data Definition Language) commands are used to define and manage the structure of database objects like tables, schemas, and indexes.

1.  CREATE A DATABASE

CREATE DATABASE College;

 CREATE A TABLE

CREATE TABLE users 

                ( id INT AUTO_INCREMENT PRIMARY KEY,

                   name VARCHAR(100) NOT NULL, 

                   email VARCHAR(100) UNIQUE NOT NULL, 

                   gender ENUM('Male', 'Female', 'Other'), 

                   date_of_birth DATE);

2. DROP THE DATABASE

DROP DATABASE College;

3. TRUNCATE

TRUNCATE TABLE Students;

It is used to remove all records from table and keep the skeleton of the table

4. RENAME TABLE 

RENAME TABLE Students TO candidates;

This rename an existing table

5. ALTER TABLE

    You can modify an existing table using ALTER TABLE

    Add a Column

        ALTER TABLE candidates ADD_COLUMN is_active BOOLEAN DEFAULT TRUE;

    Drop a Column

        ALTER TABLE candidates DROP COLUMN is_active;

    Modify a Column Type

        ALTER TABLE candidates MODIFY COLUMN name VARCHAR(150);

CommandPurpose
CREATE      ==>          Create objects
ALTER        ==>          Modify structure
DROP           ==>         Delete objects
TRUNCATE==>          Remove all data
RENAME    ==>          Rename objects
----------------------------------------------------------------------------------------------------------------------

2. DML (Data Manipulation Language)

Used to work with data inside tables

Main Commands:

  • INSERT → Add new data
  • UPDATE → Modify existing data
  • DELETE → Remove data
INSERT INTO students (id, name) VALUES (1, 'Rahul');

UPDATE students SET name = 'Amit' WHERE id = 1;

DELETE FROM students WHERE id = 1;
----------------------------------------------------------------------------------------------------------------------

3. DQL (Data Query Language)

Selecting Data from Tables:

Select All Columns

SELECT * FROM Students;

This fetches every columns and every row from the students table.

SELECT name, email FROM Students;

This only fetches name and email columns from all rows.

----------------------------------------------------------------------------------------------------------------------

 4. DCL (Data Control Language)

Used to control access/permissions

Main Commands:

  • GRANT → Give permission
  • REVOKE → Remove permission

GRANT SELECT ON students TO user1;
REVOKE SELECT ON students FROM user1;

----------------------------------------------------------------------------------------------------------------------

5. TCL (Transaction Control Language)

Used to manage transactions

Main Commands:

  • COMMIT → Save changes
  • ROLLBACK → Undo changes
  • SAVEPOINT → Set a checkpoint

COMMIT;
ROLLBACK;
SAVEPOINT sp1;

----------------------------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

What is a Large Language Model?

  What is a Large Language Model? Explained Simply A beginner-friendly guide to understanding the AI technology behind ChatGPT, Claude, and Gemini Introduction: The AI Everyone Is Talking About You have probably heard terms like ChatGPT, Claude, or Gemini being thrown around everywhere in the news, at work, on social media. These are all powered by something called a Large Language Model, or LLM for short. But what exactly is an LLM? How does it work? And why does it seem almost magical at understanding and generating human language? In this blog post, we will break it all down in plain English no PhD required. By the end, you will have a solid understanding of what LLMs are, how they learn, and why they matter.   1. What Is a Language Model? Before we get to "Large," let us start with the basics: what is a language model? A language model is a type of AI that has been trained to understand and generate text. At its core, it learns to predict:...

Machine Learning Project Life Cycle: A Complete End-to-End Guide

  Machine Learning Project Life Cycle: A Complete End-to-End Guide Machine Learning (ML) projects are more than just training algorithms on data. A successful ML solution requires structured planning, quality data, robust engineering, continuous monitoring, and iterative improvements. The Machine Learning Project Life Cycle defines a systematic approach for building scalable, reliable, and production-ready ML systems. This blog explains each stage of the ML project life cycle in detail, including Statement of Work (SOW), data collection, exploratory data analysis (EDA), feature engineering, model selection, training, fine-tuning, deployment monitoring, and feedback loops. 1. Understanding the ML Project Life Cycle Definition The ML Project Life Cycle is a structured framework that guides the development of machine learning systems from problem identification to deployment and continuous improvement. It ensures that every phase of the project is organized, measurable, and aligned wi...

What is Data Science?

The Multidisciplinary Power of Data Science (It's Not Just a Buzzword) If you've spent any time in the tech world lately, you've heard the term Data Science . Some critics dismiss it as a superfluous label — a buzzword meant to salt resumes and catch the eye of tech recruiters. But if we peel back the hype, what is it actually? Data science, despite its hype-laden veneer, is perhaps the best label we have for a cross-disciplinary set of skills that are becoming increasingly important in both industry and academia. It isn't just a single subject you learn in a vacuum; it is a toolkit — a set of skills that allows you to turn raw, messy data into actionable insights. But to truly appreciate what data science is , we first need to understand where it came from. A Brief History: How Data Science Was Born Data science didn't appear overnight. Its roots stretch back decades. In the 1960s and 70s, statisticians were already wrestling with large datasets, ...