Are you

What is Table in SQL database?


Table is a collection of data, organized in terms of rows and columns. In DBMS term, table is known as relation and row as tuple. Table is the simple form of data storage. A table is also considered as a convenient representation of relations.

Employee
EMP_NAMEADDRESSSALARY
JohnKigali15000
ClaudeNyagatare18000
BenoitKigali20000

In the above table, "Employee" is the table name, "EMP_NAME", "ADDRESS" and "SALARY" are the column names. The combination of data of multiple columns forms a row e.g. "John", "Kigali" and 15000 are the data of one row.

SQL CREATE TABLE

SQL CREATE TABLE statement is used to create table in a database. If you want to create a table, you should name the table and define its column and each column's data type.

Syntax:

create table "tablename"
("column1" "data type",
"column2" "data type",
"column3" "data type",
...
"columnN" "data type");

Example:

Let us take an example to create a myFirstDB table with ID as primary key and NOT NULL are the constraint showing that these fields cannot be NULL while creating records in the table.

CREATE TABLE STUDENTS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
PRIMARY KEY (ID)
);

Tip: If you are using phpMyAdmin, you will click SQL menu and write the code above then click go, but if you are using CMD, you will need to start CMD, start mysql in your CMD and then write the code.

If you have successfully created the table, you may check it by looking at the message provided by SQL Server; otherwise, you can use the DESC command as follows:

DESC STUDENTS;

FIELDTYPENULLKEYDEFAULTEXTRA
IDInt(11)NOPRI
NAMEVarchar(20)NO
AGEInt(11)NO
ADDRESSVarchar(25)YESNULL

You now have the STUDENTS table in your database, which you can use to record necessary student information.

Create a Table using another table

The create table command can be used to duplicate or create new table from an existing table. The new table inherits the old table's column signature. We have the option of selecting all columns or a subset of them.

If we create a new table from an existing table, the new table will be filled with the old table's existing values.

Syntax:

CREATE TABLE table_name AS
SELECT column1, column2,...
FROM old_table_name WHERE ..... ;

Example:

The following SQL creates a copy of the employee table.

CREATE TABLE EmployeeCopy AS
SELECT EmployeeID, FirstName, Email
FROM Employee;
Next

Courses