In this tutorial we will see the commands which we can use to make changes in our table (as per
To add a column in an existing table:
ALTER TABLE table_name ADD column_name DATATYPE;
To delete a column in an existing table:
ALTER TABLE table_name DROP column_name;
To change the data type of a column in an existing table:
ALTER TABLE table_name MODIFY COLUMN column_name NEW DATA TYPE;
To rename a column in an existing table:
ALTER TABLE table_name CHANGE column_name NEW_COLUMN_NAME;
To update the column as NOT NULL Constraint:
ALTER TABLE table_name MODIFY column_name DATATYPE NOT NULL;
To Sort the data in the Table:
SELECT "column_name" FROM "table-name" [ WHERE "condition"] ORDER BY "column_name" [ ASC/DESC];
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.
CREATE TABLE table_name (
column1 column1 datatype column1 constraint,
column2 column2 datatype column2 constraint,
column3 column3 datatype column3 constraint,
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.
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: