Creating a Table using MySql queries:

In this we will learn how we can create tables in the database using sql queries. As we know it becomes quite complicated to make the tables manually in the database. To ease the process we can follow the steps to make tables using my sql queries.

Syntax:

CREATE TABLE table_name (
    column1 column1 datatype column1 constraint,
    column2 column2 datatype column2 constraint,
    column3 column3 datatype column3 constraint,
   ....
);

Constraints:

  • NOT NULL CONSTRAINT – Ensures that a column cannot have a null value.
  • DEFAULT CONSTRAINT – Provides a default value for a column when none is specified.
  • UNIQUE CONSTRAINT – Ensures that all value in a column are different.
  • CHECK CONSTRAINT – Make sure all values in a column satisfy certian criteria.
  • PRIMARY_KEY CONSTRAINT – Used to uniquely identify a row in a table.
  • FOREIGN_KEY CONSTRAINT – Used to ensure referential integrity of the data.

Keys:

  • A primary key is used to uniquely identify each row in a table.
  • A primary key can consists of one or more columns on a table.
  • When multiple columns are used as primary key, it is called as Composite key.
  • A foreign key is a column (or columns) that references a column (most often primary key) of other table.
  • The purpose of foreign key is to referential integrity of the data.

As shown in the above picture, Cust_ID is the foreign key for order table whereas it is primary key for in the Customer table that means the value of Cust_ID will not change in either of the table.

For example we will create a table named as customer_table inside the ‘test’ database:

Joydeep M
Latest posts by Joydeep M (see all)
Tagged : / /
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x