EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial Table in MySQL
Secondary Sidebar
MySQL Tutorial
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Partitioning
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

Table in MySQL

By Priya PedamkarPriya Pedamkar

Table-in-MySQL

Introduction to Table in MySQL

Tables are the structured format of saving records in a database as this is easy to access and can be maintained by drawing relations between multiple tables. In the table in MySQL in this there are two kinds of tables, called parent and child tables. There must be a primary key in each table which is a unique identification of each record. In the child table, the primary key is one attribute from the parent table, which may also be called a foreign key as this key establishes a relationship between 2 tables. If there is a common field that exists between 2 tables, we can join both the tables using multiple types of join commands like inner join, outer join, left & right join. This table can consists many kind of datatypes as INT(), SMALLINT(), BIGINT(), CHAR(), VARCHAR(), DATE(), TIME(), TIMESTAMP(), BOOLEAN() etc. Apart from this, we can perform many operations in tables as joining, subquery, etc.

How to Create a Table in MySQL?

There are many kinds of SQL language.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,060 ratings)

This is categorized into:

Table-in-MySQL

  • DDL (Data Definition Language)
  • DML (Data Manipulation Language)
  • DQL (Data Query Language)
  • DCL (Data Control Language)
  • TCL (Transaction Control Language)

Creating a table, altering and dropping table comes under DDL.

Commands Description Sample Query
CREATE It used to create a table or database. CREATE table employee;
ALTER Used to modify or change values in the table. ALTER TABLE table_name

ADD COLUMN col_name;

RENAME Rename the table or database name. ALTER TABLE table_name

RENAME COLUMN col_name TO new_col_name;

DROP This removes records of a table as well as the structure of a table. This can’t be rolled back/undo. DROP TABLE IF EXISTS table_name;
TRUNCATE This empties the records only, leaves the structure for future records. TRUNCATE TABLE employee;

Let’s create a customer table.

Code:

CREATE TABLE (IF NOT EXIST) ‘tablename’
‘Fieldname’ datatype (optional parameter)
PRIMARY KEY (‘Fieldname’)
;
OR
CREATE TABLE (IF NOT EXIST) ‘Tablename’
‘Fieldname’ datatype PRIMARY KEY
;

  • “CREATE TABLE” is responsible for the creation of a new table in the database.
  • “IF NOT EXIST” is not mandatory to give. It basically checks whether the name we want to give to the new table already exists in the database or not.
  • “Datatype” is referred to, which kind of datatype we want to assign the attribute. Eg: numeric, string, date, etc.
  • At the end or at the line also we can derive the constraints like primary key, unique key, not null, etc.

Best practices:

  • We should use upper case for SQL keywords like SELECT, DELETE, DROP, etc.
  • We should always end the SQL query with a semicolon.
  • We should always avoid spaces between the name of the table or field names. Instead, we should use underscore like student_table, customer_order, etc.

Let’s create the below ‘customer’ table in the database:

Cust_id First_name Last_name Contact
1009 Ajinkya Rahane 8746874464
1005 Hardik Pandya 5435555426
1007 Jasprit Bumrah 9875986763
1002 Virat Kohli 7487687648
1010 Ravi Sashtri 9759878404
1006 Krunal Pandya 9874989859
1012 Bhubaneswar Kumar 3547687379
1004 Virendra Shewag 8765876876
1003 Sachin Tendulkar 9878749867
1008 Anil Kumble 9856876755
1001 Rohit Sharma 2986746767
1011 Rahul Dravid 5876874676

Step 1: Creating the table with these fields.

Code:

CREATE TABLE customer (
Cust_id INT(10),
First_name VARCHAR(20) not null,
Last_name VARCHAR(20) not null,
Contact INT(10),
PRIMARY KEY (Cust_id)
);

Step 2: Inserting all records.

Code:

INSERT INTO customer
VALUES
(1009, ‘Ajinkya’,’Rahane’, 8746874464),
(1005, ‘Hardik’, ‘Pandya’, 5435555426),
(1007, ‘Jasprit’, ‘Bumrah’, 9875986763),
(1002, ‘Virat’, ‘Kohli’, 7487687648),
(1010, ‘Ravi’, ‘Sashtri’, 9759878404),
(1006, ‘Krunal’, ‘Pandya’, 9874989859),
(1012, ‘Bhubaneswar’, ‘Kumar’, 3547687379),
(1004, ‘Virendra’, ‘Shewag’, 8765876876),
(1003, ‘Sachin’, ‘Tendulkar’, 9878749867),
(1008, ‘Anil’, ‘Kumble’, 9856876755),
(1001, ‘Rohit’, ‘Sharma’, 2986746767),
(1011, ‘Rahul’, ‘Dravid’, 5876874676);

Output:

Table in MySQL

How to Alter the Table in MySQL?

Alter is basically for doing manipulation in a table like adding a new column, changing the name of an existing column, dropping an existing column, rename table name, and for adding constraints.

Example #1

Adding a new column.

Code:

ALTER TABLE customer
ADD COLUMN email VARCHAR(50);

Output:

Table in MySQL

Example #2

Code:

ALTER TABLE customer
ADD COLUMN age INT(5)
DEFAULT 30;

Output:

ALTER

Example #3

Code:

ALTER TABLE customer
DROP COLUMN email ;

Output:

DROP COLUMN detail

Example #4

Code:

ALTER TABLE customer
RENAME COLUMN Cust_id TO Customer_id;

Output:

ALTER TABLE customer

How to Delete a Table in MySQL?

There are 3 kinds of commands in the deletion purposes like DROP, TRUNCATE and DELETE.

DROP table deletes the whole records as well as the structure/index of the table.

Syntax:

DROP TABLE Table_name;

TRUNCATE table deletes only the records from the table by keeping the structure for further use.

Syntax:

TRUNCATE TABLE Table_name;

DELETE is used with WHERE clause to delete the records from the table.

Syntax:

DELETE FROM customer
WHERE first_name LIKE ‘A%’;

Output:

DELETE is used with WHERE clause

(It deleted all the records where first_name starts with ‘A’)

Conclusion

Those tables in SQL give advantages to keep data in structural form so that we can use those records and can perform many operations easily. Each row can be identified by a unique field for better accessibility to particular records. In the database using those tables, we can establish many relationships with different tables/data by creating some foreign keys. This helps us storing large amounts of data with easy, secure, and faster accessibility.

Recommended Articles

This is a guide to Table in MySQL. Here we discuss how to create a table in MySQL, how to alter the table, and how to delete a table respectively. You can also go through our other suggested articles to learn more–

  1. Distinct Keyword in SQL
  2. SQL Server Constraints
  3. SELECT in MySQL
  4. MySQL Aggregate Function
Popular Course in this category
SQL Training Program (7 Courses, 8+ Projects)
  7 Online Courses |  8 Hands-on Projects |  73+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (14 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more