0% found this document useful (0 votes)
31 views

Create Table Statement: Structured Query Language

SQL is a language used to store, manipulate, and retrieve data from relational databases. Some key SQL statements include CREATE TABLE to create a new table, ALTER TABLE to modify an existing table by adding or dropping columns, INSERT INTO to add new rows of data to a table, SELECT to retrieve data from one or more tables, GROUP BY to group query results by one or more columns, and ORDER BY to sort the query results in ascending or descending order.

Uploaded by

tarrant High
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Create Table Statement: Structured Query Language

SQL is a language used to store, manipulate, and retrieve data from relational databases. Some key SQL statements include CREATE TABLE to create a new table, ALTER TABLE to modify an existing table by adding or dropping columns, INSERT INTO to add new rows of data to a table, SELECT to retrieve data from one or more tables, GROUP BY to group query results by one or more columns, and ORDER BY to sort the query results in ascending or descending order.

Uploaded by

tarrant High
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Structured Query Language

SQL (Structured Query Language) is a computer language aimed to store, manipulate, and
retrieve data stored in relational databases.

Create Table Statement


Tables are the basic structure where data is stored in the database. Given that in most cases, there
is no way for the database vendor to know ahead of time what your data storage needs are,
chances are that you will need to create tables in the database yourself. Many database tools
allow you to create tables without writing SQL, but given that tables are the container of all the
data, it is important to include the CREATE TABLE syntax in this tutorial.

The SQL syntax for CREATE TABLE is

CREATE TABLE "table_name"


("column 1" "data_type_for_column_1",
"column 2" "data_type_for_column_2",
... )

So, if we are to create the customer table specified above, we would type in:

CREATE TABLE Customer


(Customer_ID char (8) NOT NULL UNIQUE,
First_Name char (25),
Last_Name char (25),
Address char (50),
City char (20),
Country char (25),
Birth_Date date,
Primary key (Customer_ID));

Once a table is created in the database, there are many occasions where one may wish to change
the structure of the table. Typical cases include the following:

- Add a column
- Drop a column
The SQL syntax for ALTER TABLE is:

ALTER TABLE "table_name"


[alter specification]

Alter Table
This allows persons to make changes to a table in a database, in relation to: Add, drop or change
information.

1. Drop Column:
ALTER Table student
DROP Address;
2. Add Column

ALTER Table student

ADD Age char(2)

3. DELETE ROW
DELETE FROM student
WHERE STUID=115;

The syntax for inserting data into a table one row at a time is as follows:

INSERT INTO "table_name" ("column1", "column2", ...)


VALUES ("value1", "value2", ...)

Insert Into
INSERT INTO student (STUID, FName, LName, Gender, Tel, Test1, Test2)

VALUES (115, “Jake”, “Ritz”, “Male”, 8756555, 99, 82);

The GROUP BY keyword is used when we are selecting multiple columns from a table (or
tables) and at least one arithmetic operator appears in the SELECT statement. When that
happens, we need to GROUP BY all the other selected columns, i.e., all columns except the
one(s) operated on by the arithmetic operator.

Group By
This is normally used in the case of calculated fields.

SELECT FName,sum(Average)

FROM student
GROUP BY FName;

So far, we have seen how to get data out of a table using SELECT and WHERE commands.
Often, however, we need to list the output in a particular order. This could be in ascending order,
in descending order, or could be based on either numerical value or text value. In such cases, we
can use the ORDER BY keyword to achieve our goal.

The syntax for an ORDER BY statement is as follows:

SELECT "column_name"
FROM "table_name"
ORDER BY "column_name" [ASC, DESC]

Order By
SELECT LName, FName, Gender

FROM student

ORDER BY "LName [ASC, DESC]" ;

OR

SELECT * FROM student

ORDER BY "LName, Average [ASC, DESC]" ;

OR

SELECT * FROM student

ORDER BY LName ASC;

ASC means that the results will be shown in ascending order, and DESC means that the results
will be shown in descending order. If neither is specified, the default is ASC.

You might also like