|
SQL
SQL CREATE TABLETables 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, you will need to create tables in the database yourself. Many database tools allow you to create tables without writing SQL, but given that the table is the container of all the data, it is important to include the CREATE TABLE syntax in this tutorial. Before we dive into the SQL syntax for CREATE TABLE, it is a good idea to understand what goes into a table. Tables are divided into rows and columns. Each row represents one piece of data ('record'), and each column can be thought of as representing a component of that piece of data. So, for example, if we have a table for recording customer information, then the columns may include information such as First Name, Last Name, Address, City, Country, Birth Date, and so on. As a result, when we specify a table, we include the column headers and the data types for these columns. So what are data types? Typically, data comes in a variety of forms. It could be an integer (such as 1), a real number (such as 0.55), a string (such as 'sql'), a date (such as '2000-01-25'), or even in binary format. When we specify a table, we need to specify the data type associated with each column. For example, we will specify that 'First Name' is of type CHAR(50) - meaning it is a string with a maximum length of 50 characters). The reason that one needs to specify a maximum length is to increase database access speed by making all records the same size (so increased speed is achieved at the expense of a larger storage size). One thing to note is that different relational databases allow for different data types, so it is wise to consult with a database-specific reference first. The SQL syntax for CREATE TABLE is CREATE TABLE table_name For example : CREATE TABLE customer SQL DROP TABLE Sometimes we may decide that we need to remove - that is, delete - a table in the database for some reason. In fact, it would be problematic if we cannot do so because this would create a maintenance nightmare. Fortunately, SQL allows us to do it by using the DROP TABLE command. The syntax for DROP TABLE is DROP TABLE table_name So, if we wanted to drop the table called customer, we can use : DROP TABLE customer
SQL TRUNCATE TABLESometimes we may wish to remove all the data in a table. One way of doing this is with DROP TABLE. But what if we wish to simply get rid of the data but not the table itself? For this, we can use the TRUNCATE TABLE command. The syntax for TRUNCATE TABLE is: TRUNCATE TABLE table_name So, if we wanted to truncate the table called customer, we can use : TRUNCATE TABLE customer
SQL INSERT INTOThe easiest way to insert data into a table is one row (record) at a time. The syntax for inserting data into a table one row at a time is as follows: INSERT INTO table_name (column1, column2, ...) Assume we have a table that has the following structure:
If we wish to insert one additional row into the table representing the sales data for Los Angeles, we can use the following SQL script: INSERT INTO Store_Information (Store_Name, Num_Sales)
SQL UPDATEOnce we have the information in the table, we might find that there is a need to modify the data. To do so, we can use the UPDATE command. The syntax for this is: UPDATE table_name The conditional WHERE clause is optional but is normally required as without it, every row will have that column's value set to new_value. It is easiest to use an example. Say we currently have a table as below:
We find that the sales for Los Angeles is actually 500 rather than 300, and we need to update that particular entry. To do so, we can use the following SQL: UPDATE Store_Information The resulting table would look like this:
In this case, there is only one row that satisfies the condition in the WHERE clause. If there are two rows that satisfy the condition, both rows will be modified. It is also possible to UPDATE multiple columns at the same time. The syntax in this case would look like the following: UPDATE TABLE table_name
SQL DELETE FROMSometimes rather than updating, we wish to remove records (rows) from the table. To do this we can use the DELETE FROM command. The syntax for this is DELETE FROM table_name It is easiest to use an example. Say we currently have a table as below:
and we decide not to keep any information on Los Angeles in this table. We can use the following SQL: DELETE FROM Store_Information
SQL SELECTThe SQL SELECT command is used for retrieving data from the tables in a database. Immediately, we see two keywords: we need to SELECT information FROM a table. (Note that a table is a container (file) that resides in the database where the data is stored). Hence we have the most commonly used SQL statement: SELECT column_name FROM table_name [WHERE condition] To illustrate the above example, assume that we have the following table:
To retrieve a list of all the stores names in table Store_Information we can use the following SQL statement: SELECT Store_Name FROM Store_Information
Next, we might want to conditionally select - that is, filter - the data from a table. For example, we may want to only retrieve stores with sales above 500 units. To do this, we include the WHERE keyword. The syntax is as follows: SELECT column_name For example, to select all stores with sales above 500 units in Table Store_Information,
we can use the SQL statement: SELECT Store_Name
One can also request more than one column to be returned. The order will be as wriiten in the SELECT statement: SELECT Num_Sales, Store_Name
Now consider the case that there are two tables in the database. One holds person names plus an id - and also the id of the car they drive. A second table - that can be referred to as a 'look-up' table - holds the car name for each car id. Database tables usually include at least one column that contains the 'key' - a unique identifier; two people may have the same name, but will have different IDs.
If we wish to get the name of person with id=2, and the name of the car they drive, we can use the following SELECT statement: SELECT name, car_name
If two tables both have a column with the same name, the table name can be used as a prefix plus '.' to differentiate them. For example: car.id
|