MySQL Create Table
A table is a collection of related data that contains a set of vertical columns and horizontal rows. It requires a name of table which is unique in a database, fields name and their data types. A table columns are defined at the time of table creation, but we can also alter it later and on every data insertion a new row is created.
Syntax of creating a table
CREATE TABLE tablename (column name, column datatype);
Example
The given command creates a STUDENTS table with column names id, first_name, last_name, age, class and make 'id' as primary key.
mysql->CREATE TABLE STUDENTS (id INT(11) NOT NULL AUTO_INCREMENT, first_name NOT NULL VARCHAR(20),
last_name NOT NULL VARCHAR(20), age INT(11), class NOT NULL VARCHAR(10), PRIMARY KEY(id));
baseurl.'/images/mysqlcreatetbl.jpg'; ?> alt="Create Table">
- In the above create table query, id and age are fix length numeric values that's why defined as INT datatype and first_name , last_name, class (like - 1A, 2B etc) are of variable length that's why defined as VARCHAR datatype.
- Each row should contain values of id, first_name, last_name and class, so these are defined as NOT NULL.
- The 'id' field is defined as PRIMARY key.
NOTE: Column name should be unique in a table.
Show Tables
SHOW TABLES command is used to find out the existing tables of a database.
baseurl.'/images/showtables.jpg'; ?> alt="Show Table">Describe Table
DESCRIBE TABLE command is used to show the table structure.
baseurl.'/images/describe_table.jpg'; ?> alt="Describe Table">