Create Table in DBMS

Creating a Database Table

In this article, we will learn about Create Table in DBMS.We can create table using CREATE tag. CREATE is the DDL(data definition language) commands used for the creation of the tables in a database
Find the Greatest of the Two Numbers in Java

CREATE TABLE

A database is nothing but the structured organization of data. For organizing the data in a database we need to create database tables as per the required structure

CREATE is  the DDL(data definition language) commands used for the creation of the tables in a database

Syntax:

CREATE TABLE table_name(
column1 datatype,
column2 datatype,
................
columnN datatype,
PRIMARY KEY(one or more columns)
);

CREATE TABLE

  • Create command is used to create a table in the database with the structure specified by the user
  • This structure includes the number of columns to be present in the table and the data type of the column, size of data, etc

Basic Syntax for CREATE

CREATE TABLE table_name
(
column1 datatype(size),
column2 datatype(size),
column3 datatype(size),
.....
columnN datatype(size),
PRIMARY KEY( one or more columns )
);

Example

create table emp( 
empno number(4,0), 
ename varchar2(10), 
job varchar2(9), 
mgr number(4,0), 
hiredate date, 
sal number(7,2), 
deptno number(2,0) 
PRIMARY KEY (ID)
);
  • Table name:emp
  • Column names: In the above table that we have created have 7 columns namely  empno, ename, job, mgr,hiredate, sal,deptno
  • Data types: What type of data should be entered for each column value. for example, we have used number data type for the column empno , which means you must enter numerical values only while inserting data for emp column
  • Size: Specifies the length of the value that is inserted, for example, we have used size 10  for ename as varchar2(10), which means the maximum number of characters that can be entered for the ename column is 10.

DESC command

  • Describe  command shows the structure of the table that we have created
  • It displays the column names, data types of column names, size of data , any constraints imposed the table, default values for each column, whether null values allowed or not for each column, etc

Example

desc emp;
O/P
Field Type Null Key Default Extra
EMPNO number(4,0) N0 PRI
ENAME varchar2(10) YES NULL
JOB varchar2(9) NO NULL
MGR number(4,0) NO NULL
HIREDATE date NO NULL
SAL number(7,2) NO NULL
DEPTNO number(2,0) NO NULL

Creating a new table from an existing table

  • We can create a new table with exactly the same structure of any of the existing tables
  • Here an empty table with exactly the same structure of the existing table is created but data and constraints of the table are not copied

Example

create table
sample as (select * from emp);
O/P
table sample crated successfully
Not both sample and emp tables will have the same structure i.e same number of columns, column names,  datatype, sizes, etc  .you can also cross-check by using desc command

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription