Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left

  Apply Now

SQL Tutorial

15 / 68

Creating and Dropping a table

A table is an object which stored data in rows and columns.

The table creation command requires the following details:
Name of the table Name of the fields Definitions for each field

Basic Syntax:

create table table_name (
  column_name1 column_type1 [default <values>]
  ,column_name1 column_type1 [default <values>]
 );

Full Syntax:

https://dev.mysql.com/doc/refman/8.0/en/create-table.html

Example:

use retail_db;
create table deptt(
 depid int not null,
 depname varchar(30) not null default 'New',
 depopendate datetime not null default CURRENT_TIMESTAMP
);

Try creating above table and if succeeds, you will see a message like below one, else you will see relevant error message:
Query OK, 0 rows affected (0.01 sec)

View the table definition using below commands:

describe deptt;

OR

show create table deptt;
show columns from deptt;
show table status like 'deptt';

Try selecting the data from the table. You would see 0 rows because no data has been inserted yet into the table.

select * from deptt;

Dropping a table
Tables can be dropped using 'drop table' command. A table can not be dropped if it is being used by someone else or if the table has child tables enforced by Foreign Keys and child table has records.

drop table deptt;

No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...