SQL COMMAND
Some of the SQL command or the SQL command to know is:
- CREATE DATABASE, to create a database.
- DROP DATABASE to remove a database.
- CREATE TABLE, to create a table.
- Alter TABLE, to modify a table.
- DROP TABLE to remove a table.
- SELECT , to display data from the database.
- UPDATE, to modify the data from the database.
- INSERT INTO, to add data in the database.
- DELETE, to delete data from the database.
While the SQL command to have advanced quite a lot. Command-level information is useful to process data in a manner that is more complicated and complex.
CREATE DATABASE COMMAND
Syntax:
CREATE DATABASE database_name |
After creating a database with the command as above, does not automatically your database contains the table-table. Your database in a state of empty. You must create a table-like tables that will be described in the next section.
DROP DATABASE COMMAND
If you want to delete a database, use the following command:
DROP DATABASE database_name |
If you use the above command, all database and table that is in it will be erased.
Note: for Access, you may not use the DROP DATABASE command. If you use this command, your application will be garbled. If you want to delete the database from Access, you can delete the mdb file that was created by Access.
CREATE TABLE COMMAND
CREATE TABLE table_name(column_name1 data_type,column_name2 data_type,....... ) |
For example:
CREATE TABLE Person (LastName varchar,FirstName varchar,Address varchar,Age int) |
You can also limit the size of each field utnuk produce a database file that is smaller:
CREATE TABLE Person (LastName varchar(30),FirstName varchar,Address varchar,Age int(3) ) |
For the data type, you can see again the first part of the material in this lecture.
ALTER TABLE COMMAND
The syntax is:
To add a new field: ALTER TABLE table_name ADD column_name datatype (size)To remove an existing field: ALTER TABLE table_name DROP COLUMN column_name |
DROP TABLE COMMAND
DROP TABLE table_name |
Note: Please note that if you delete a table, then all existing file in it will be erased.
SELECT COMMAND
To display data from a database, you can use the SELECT command. Basic form of this command is:
SELECT column_name_1, column_name_2, dstFROM table_name |
If you want to display all data in a table without selecting one of the field, then you can use the following command:
SELECT * FROM table_name |
SELECT DISTINCT STATEMENT
Note the sample table orders the following:
| Company | OrderNumber |
| Sega | 3412 |
| W3Schools | 2312 |
| Trio | 4678 |
| W3Schools | 6798 |
SELECT Company FROM Orders |
then the result will be like this:
| Company |
| Sega |
| W3Schools |
| Trio |
| W3Schools |
SELECT DISTINCT Company FROM Orders |
The results will be like this:
| Company |
| Sega |
| W3Schools |
| Trio |
Note that the SELECT command, the data "W3Schools" 2 times will be shown that there is appropriate data, whereas with the command SELECT DISTINCT, then the data "W3Schools" is only shown 1 times. So the command SELECT DISTINCT will not produce data that is identical or twin.
WHERE CLAUSE
Basic syntax of the command is WHERE:
SELECT column FROM tableWHERE column operator value |
| Operator | Description |
| = | Equal |
| <> or != | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
| BETWEEN | Between an inclusive range |
| LIKE | Search for a pattern |
Note the PERSONS table below:
| LastName | FirstName | Address | City | Year |
| Hansen | Ola | Timoteivn 10 | Sandnes | 1951 |
| Svendson | Tove | Borgvn 23 | Sandnes | 1978 |
| Svendson | Stale | Kaivn 18 | Sandnes | 1980 |
| Pettersen | Kari | Storgt 20 | | 1960 |
If you use the SELECT command with a WHERE attributes such as the following:
SELECT * FROM |
The results will be displayed like this:
| LastName | FirstName | Address | City | Year |
| Hansen | Ola | Timoteivn 10 | Sandnes | 1951 |
| Svendson | Tove | Borgvn 23 | Sandnes | 1978 |
| Svendson | Stale | Kaivn 18 | Sandnes | 1980 |
Note that the displayed data is coming from the city "Sandnes" in accordance with the conditions provided in the command above. Pay attention to any posts in quotes ( ") is used, the following is true:
For a text value: The correct syntax: SELECT * FROM Persons WHERE FirstName='Tove'The wrong Syntax: SELECT * FROM Persons WHERE FirstName=ToveValue to a number: The correct syntax: SELECT * FROM Persons WHERE Year>1965The wrong Syntax: SELECT * FROM Persons WHERE Year>'1965' |
LIKE CLAUSE
The syntax is:
SELECT column FROM table_nameWHERE column LIKE conditions |
Note the following example:
SELECT * FROM PersonsWHERE FirstName LIKE 'O%' |
SELECT * FROM PersonsWHERE FirstName LIKE '%a' |
While the above example will generate all the data that firstname is ending with the letter "a".
SELECT * FROM PersonsWHERE FirstName LIKE '%la%' |
While the above this will generate all the data that it has a firstname letter "la" in the middle.
ORDER BY STATEMENT
Syntax is as follows:
SELECT column_1, column_2, dst FROM table_nameORDER BY column_1, column_2, dst ASC/DESC |
SELECT column_1, column_2, dst FROM tableWHERE column_2 LIKE conditions ORDER BY column_1 ASC |
Note the sample table orders the following:
| Company | OrderNumber |
| Sega | 3412 |
| ABC Shop | 5678 |
| W3Schools | 2312 |
| W3Schools | 6798 |
Suppose you have a command like this:
SELECT Company, OrderNumber FROM OrdersORDER BY Company |
| Company | OrderNumber |
| ABC Shop | 5678 |
| Sega | 3412 |
| W3Schools | 6798 |
| W3Schools | 2312 |
You can also combine several conditions at once. Note the following example:
SELECT Company, OrderNumber FROM OrdersORDER BY Company DESC, OrderNumber ASC |
| Company | OrderNumber |
| W3Schools | 2312 |
| W3Schools | 6798 |
| Sega | 3412 |
| ABC Shop | 5678 |
Note that the results indicate that the company will be in the field in descending order, and in the field akan ordernumber in Ascending order.
AND dan OR STATEMENT
Note the sample table PERSONS following:
| LastName | FirstName | Address | City |
| Hansen | Ola | Timoteivn 10 | Sandnes |
| Svendson | Tove | Borgvn 23 | Sandnes |
| Svendson | Stephen | Kaivn 18 | Sandnes |
SELECT * FROM PersonsWHERE FirstName='Tove'AND LastName='Svendson' |
The results will be like this:
| LastName | FirstName | Address | City |
| Svendson | Tove | Borgvn 23 | Sandnes |
SELECT * FROM PersonsWHERE firstname='Tove'OR lastname='Svendson' |
But the results will be displayed is like this:
| LastName | FirstName | Address | City |
| Svendson | Tove | Borgvn 23 | Sandnes |
| Svendson | Stephen | Kaivn 18 | Sandnes |
SELECT * FROM Persons WHERE(FirstName='Tove' OR FirstName='Stephen')AND LastName='Svendson' |
| LastName | FirstName | Address | City |
| Svendson | Tove | Borgvn 23 | Sandnes |
| Svendson | Stephen | Kaivn 18 | Sandnes |
UPDATE COMMAND
The syntax is:
UPDATE table_nameSET column_name = new_valueWHERE column_name = old_value |
| LastName | FirstName | Address | City |
| Nilsen | Fred | Kirkegt 56 | |
| Rasmussen | Ani | Storgt 67 | |
Suppose you have a command like this:
UPDATE Person SET FirstName = 'Nina'WHERE LastName = 'Rasmussen' |
| LastName | FirstName | Address | City |
| Nilsen | Fred | Kirkegt 56 | |
| Rasmussen | Nina | Storgt 67 | |
Note that the previous record was "Ani" was changed to "Nina" after the command is executed.
INSERT INTO COMMAND
The syntax is:
INSERT INTO table_name (column1, column2, dst)VALUES (column1_value, column2_value, dst) |
| LastName | FirstName | Address | City |
| Pettersen | Kari | Storgt 20 | |
To add the new data, you can use the INSERT INTO. Following example:
INSERT INTO Persons (LastName, FirstName, Address, City)VALUES ('Hetland', 'Camilla', 'Hagabakka 24', 'Sandnes') |
Or the command like this:
INSERT INTO Persons VALUES ('Hetland', 'Camilla', 'Hagabakka 24', 'Sandnes') |
Note that because the contents of a text field, then the value that is given the quotation marks
( '') which signifies or text string. The result is as follows:
| LastName | FirstName | Address | City |
| Pettersen | Kari | Storgt 20 | |
| Hetland | Camilla | Hagabakka 24 | Sandnes |
Note the following table PERSONS:
| LastName | FirstName | Address | City |
| Pettersen | Kari | Storgt 20 | |
| Hetland | Camilla | Hagabakka 24 | Sandnes |
Suppose you have a command like this:
INSERT INTO Persons (LastName, Address)VALUES ('Rasmussen', 'Storgt 67') |
| LastName | FirstName | Address | City |
| Pettersen | Kari | Storgt 20 | |
| Hetland | Camilla | Hagabakka 24 | Sandnes |
| Rasmussen | | Storgt 67 | |
DELETE COMMAND
This can not only remove the content of a particular field only. The entire field in a series of rows will be deleted if it met specified conditions.
The syntax is:
DELETE FROM table_nameWHERE column_name = value |
Note the sample table PERSONS following:
| LastName | FirstName | Address | City |
| Nilsen | Fred | Kirkegt 56 | |
| Rasmussen | Nina | Stien 12 | |
DELETE FROM Person WHERE LastName = 'Rasmussen' |
The results will be like this:
| LastName | FirstName | Address | City |
| Nilsen | Fred | Kirkegt 56 | |
If you want to delete all the rows that have, you can use the following:
DELETE FROM table_nameatauDELETE * FROM table_name |
Then all data in the table that will be lost altogether.
0 comments
Post a Comment