TABLE
TABLE in SQL refers to an organized structure for storing data. It is composed of rows and columns where each row represents a record and each column represents a field of that record. The intersecting point of a row and a column in a table represents a cell. Information is stored efficiently and accessibly in an SQL table, making it an integral part of RDBMS (Relational Database Management Systems).
Example
CREATE TABLE Employee ( ID INT NOT NULL, Name VARCHAR(255), Age INT, Salary FLOAT, PRIMARY KEY (ID));
Output
Query OK, 0 rows affected (0.01 sec)
Explanation
This SQL statement creates a new table named ‘Employee’. The table includes columns for ID, Name, Age, and Salary. The ID column is designated as the primary key.
Example
CREATE TABLE Employees ( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, SALARY REAL DEFAULT 30000.0, JOIN_DATE DATE NOT NULL);
Output
‘CREATE TABLE’ command is successfully executed
Explanation
This SQL statement creates a new table in a database. The table, named ‘Employees’, has four fields: ‘ID’, ‘NAME’, ‘SALARY’, and ‘JOIN_DATE’. The ‘ID’ field is of type INT and is the primary key, the ‘NAME’ field is of type TEXT, the ‘SALARY’ field is of type REAL with a default value of 30000.0, and the ‘JOIN_DATE’ field is of type DATE.
Example
CREATE TABLE Employees ( ID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), Position VARCHAR(50) );
INSERT INTO Employees (ID, FirstName, LastName, Position)VALUES (1, 'John', 'Doe', 'Manager');
Output
Command(s) completed successfully.
Explanation
The above example creates a table called Employees
with columns ID
, FirstName
, LastName
, and Position
. It then inserts a record into the table, with the ID of 1, first name of ‘John’, last name of ‘Doe’, and position of ‘Manager’.
Example
CREATE TABLE Employees ( Employee_Id number(10), First_Name varchar2(100), Last_Name varchar2(100), Hire_Date date, Phone_Number varchar2(20),);
Output
Table EMPLOYEES created.
Explanation
The above SQL statement is an example of how to create a new table in Oracle called Employees. The table is composed of five columns, Employee_Id
, First_Name
, Last_Name
, Hire_Date
and Phone_Number
. Each column has a specific data type and size - number(10)
for Employee_Id, varchar2(100)
for First_Name and Last_Name, date
for Hire_Date and varchar2(20)
for Phone_Number.
Example
CREATE TABLE Students ( ID INTEGER PRIMARY KEY, Name TEXT NOT NULL, Age INTEGER NOT NULL, Grade INTEGER NOT NULL);
INSERT INTO Students (ID, Name, Age, Grade)VALUES (1, 'John Doe', 15, 10), (2, 'Jane Doe', 16, 11);
SELECT * FROM Students;
Output
ID | Name | Age | Grade1 | John Doe | 15 | 102 | Jane Doe | 16 | 11
Explanation
The CREATE TABLE
statement is used to create a new table called Students
with four columns: ID
, Name
, Age
, and Grade
. ID
is assigned as the primary key. The NOT NULL
constraint ensures that each of these columns must always have a value.
The INSERT INTO
statement is used to insert two new rows of values into the Students
table.
Finally, the SELECT * FROM Students
statement selects all rows and columns from the Students
table, displaying all the data contained within the table.