Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🗄️ Unit 4 – Database Concepts & SQL  ·  Lesson 16

MySQL: Data Types, Constraints, CREATE

What is SQL?

SQL (Structured Query Language, often said "sequel") is the standard language to talk to relational databases. With SQL we create tables, put data in, change it and ask questions (queries) like "show the students of Jaipur with marks above 80". MySQL is a free, open-source RDBMS that understands SQL.

YouMySQL serverDatabaseSELECT Name FROM STUDENT WHERE City = "Jaipur";find matching rowsAarav, Kabirresult table
You send SQL; MySQL returns a result

To practise, install MySQL Community Server (with MySQL Shell or Workbench) from dev.mysql.com, or use a portable package like XAMPP. Every SQL statement ends with a semicolon ;.

Categories of SQL commands

🏗️
DDL – Data DefinitionDefine structure: CREATE, ALTER, DROP.
✏️
DML – Data ManipulationChange data: INSERT, UPDATE, DELETE.
🔎
DQL – Data QueryRead data: SELECT.
🛡️
DCL / TCLPermissions (GRANT, REVOKE) and transactions (COMMIT, ROLLBACK).
SQL command groups

Creating and using a database

SQL
CREATE DATABASE school;
SHOW DATABASES;
USE school;
Output
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+

USE school; makes school the current database, so all tables you create go into it.

MySQL data types

Data typeStoresExample value
CHAR(n)Fixed-length text, n up to 255'M', '302012'
VARCHAR(n)Variable-length text'Aarav Sharma'
INTWhole numbers101, -25
FLOATApproximate decimal numbers3.14
DECIMAL(p, s)Exact decimals: p digits, s after pointDECIMAL(7,2) → 12345.67
DATEDate in 'YYYY-MM-DD' format'2009-04-15'

CHAR(10)

  • 'Ram' is stored as 'Ram' + 7 spaces
  • Always 10 characters of space
  • Slightly faster for fixed-size data

VARCHAR(10)

  • 'Ram' uses only 3 characters (+ length info)
  • Saves space for varying text
  • Best for names, cities, addresses
CHAR vs VARCHAR

Constraints

Constraints are rules on columns that keep data correct.

ConstraintRule
NOT NULLColumn cannot be left empty
UNIQUENo two rows can have the same value
DEFAULTValue used when none is given
PRIMARY KEYUNIQUE + NOT NULL; identifies each row
FOREIGN KEYValue must exist in the referenced table's primary key
CHECKValue must satisfy a condition (MySQL 8.0.16+)

CREATE TABLE

Syntax
CREATE TABLE table_name (
  column1 datatype constraint,
  column2 datatype constraint,
  ...
);
SQL
CREATE TABLE STUDENT (
  RollNo  INT PRIMARY KEY,
  Name    VARCHAR(30) NOT NULL,
  Gender  CHAR(1),
  DOB     DATE,
  Class   VARCHAR(5) DEFAULT 'XI-A',
  Marks   DECIMAL(5,1) CHECK (Marks BETWEEN 0 AND 100),
  City    VARCHAR(20)
);
DESCRIBE STUDENT;
Output
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| RollNo | int          | NO   | PRI | NULL    |       |
| Name   | varchar(30)  | NO   |     | NULL    |       |
| Gender | char(1)      | YES  |     | NULL    |       |
| DOB    | date         | YES  |     | NULL    |       |
| Class  | varchar(5)   | YES  |     | XI-A    |       |
| Marks  | decimal(5,1) | YES  |     | NULL    |       |
| City   | varchar(20)  | YES  |     | NULL    |       |
+--------+--------------+------+-----+---------+-------+

DESCRIBE (or DESC) shows the structure: Field = column name, Null = can it be empty, Key = PRI for primary key, Default = default value.

Table with a foreign key

SQL
CREATE TABLE GUARDIAN (
  GID      INT PRIMARY KEY,
  GName    VARCHAR(30) NOT NULL,
  Phone    CHAR(10) UNIQUE,
  RollNo   INT,
  FOREIGN KEY (RollNo) REFERENCES STUDENT(RollNo)
);
DESCRIBE GUARDIAN;
Output
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| GID    | int         | NO   | PRI | NULL    |       |
| GName  | varchar(30) | NO   |     | NULL    |       |
| Phone  | char(10)    | YES  | UNI | NULL    |       |
| RollNo | int         | YES  | MUL | NULL    |       |
+--------+-------------+------+-----+---------+-------+
STUDENT🔑 RollNo INTName VARCHAR(30)Gender CHAR(1)DOB DATEClass VARCHAR(5)Marks DECIMAL(5,1)City VARCHAR(20)GUARDIAN🔑 GID INTGName VARCHAR(30)Phone CHAR(10) UNIQUE🔗 RollNo INT
GUARDIAN.RollNo refers to STUDENT.RollNo

SHOW, DESCRIBE and DROP

SQL
SHOW TABLES;
Output
+------------------+
| Tables_in_school |
+------------------+
| GUARDIAN         |
| STUDENT          |
+------------------+

DROP TABLE GUARDIAN; deletes the table and all its data permanently. DROP DATABASE school; deletes the entire database. Always double-check before dropping. A parent table (STUDENT) cannot be dropped while a child table (GUARDIAN) still refers to it.

Summary & practice

  • SQL is the language of relational databases; MySQL is an RDBMS.
  • DDL: CREATE, ALTER, DROP. DML: INSERT, UPDATE, DELETE. DQL: SELECT.
  • Types: CHAR, VARCHAR, INT, FLOAT, DECIMAL, DATE.
  • Constraints: NOT NULL, UNIQUE, DEFAULT, PRIMARY KEY, FOREIGN KEY, CHECK.
✅ Practice

Write CREATE TABLE commands for: (1) BOOK(BookID primary key, Title not null, Author, Price decimal with 2 places, Qty default 1). (2) ISSUE(IssueID primary key, BookID foreign key to BOOK, IssueDate date, ReturnDate date).

Frequently Asked Questions

What is the difference between CHAR and VARCHAR?
CHAR(n) is fixed length: it always uses n characters of space (padding with spaces). VARCHAR(n) is variable length: it uses only as much space as the actual text, up to n characters. Use CHAR for fixed codes like gender or PIN, VARCHAR for names and addresses.
Is SQL case-sensitive?
SQL keywords are not case-sensitive — CREATE and create work the same. Writing keywords in capitals is a convention for readability. Table names may be case-sensitive on Linux servers.
What does DECIMAL(5,1) mean?
A number with up to 5 digits in total, of which 1 is after the decimal point, for example 100.0 or 87.5.
← Class 11 Informatics Practices
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicMySQL: INSERT & SELECT Queries Your progress: 16 / 20 (80%) · Lesson 17 / 20 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free