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

Course 8 SQL

Uploaded by

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

Course 8 SQL

Uploaded by

fifa03fifa3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

INFORMATIQUE SECOND YEAR ESMT

Course 08: SQL Language


1- Introduction :
The SQL (Structured Query Language) is a language that allows communication with a
database. This language is used by web developers to communicate with their website
database.

• SQL is a data definition language (LDD), it allows you to create, modify or delete
tables in a DB.

• SQL is a language for data manipulation (LDM), it allows you to select, insert, adjust
or delete data in a DB table.

• SQL is a query language. It allows you to perform queries on several tables and
calculate results.

• SQL is an access protection language: It is possible with SQL to define permissions at


the user level of a database. We are talking about DCL (Data Control Language)

2- Manipulation of tables
2.1 creating tables
Tables are created using the CREATE TABLE keyword pair. The simplified table
definition syntax is as follows:
CREATE TABLE Table_name (Column_name1 Data_type1, Column_name2 Data_type2,
..., Column_nameN Data_typeN);
The name given to the table must generally (on most DBMS) start with a letter, and the
maximum number of columns per table is 254.
 Data types

1
INFORMATIQUE SECOND YEAR ESMT

For each column that you create, you must specify the data type that the field will
contain. This can be one of the following types:

Data types Syntax Description

CHAR(n) Fixed length character chain n (n<16383)


Alphanumeric type
VARCHAR(n) Character chain of n characters maximum (n<16383)

BIT ( True or False) 0 or 1

BYTE Integer between 0 and 255 (1 Byte= 8 bit)

SMALLINT Signed integer of 2 Bytes [-32768, 32768 [

Signed integer of 4 Bytes: [ -2 147 483 648 , 2 147


INT
Numeric type 483 648 [

Floating point number: - 3.40E + 38 to 3.40E + 38 (size=4


REAL
bytes)

FLOAT Floating point number: - 1.79E+308 to 1.79E+308


(size=8 bytes)

Money a monetary value ( size=8 Bytes)

DATE Date in the form 07/16/99 (English form of date :


mm/dd/yy)
Date&time Type
TIME Time in the form 12:54:24.85

TIMESTAMP Date and time

……………etc

EXAMPLE: If we want to create the following table:


Product (Barcode, NameP, PriceU, Color, Quantity, Weight)

2
INFORMATIQUE SECOND YEAR ESMT

CREATE TABLE Product (BarCode CHAR(8) PRIMARY KEY, Price MONEY, Name
VARCHAR(40), Color VARCHAR(15), Quantity SMALLINT, Weight REAL)
2.2 Deleting tables
To delete a “table_name” table, simply use the following syntax:
DROP TABLE table_name
To delete all data from a table without deleting the table itself. We use the following
syntax:
TRUNCATE TABLE table_name
Note: Access does not support the TRUNCATE statement.
2.3 modifying tables
The ALTER TABLE command in SQL allows you to modify an existing table. It is possible
to add a column, delete one or modify an existing column (for example to change only
the type)
Basic syntax:
Generally speaking, the command is used as follows:
ALTER TABLE table_name instruction
The keyword “instruction” here is used to designate an additional command depending
on the action you wish to perform: add, delete or modify a column.
 Add a column
Adding a column to a table is relatively simple and can be done using a query that looks
like this:
ALTER TABLE name_table
ADD COLUMN name_column Data_type
Example :
To add the “volume” field to the “product” table of the previous example we write:
ALTER TABLE Product ADD COLUMN Volume REAL
And to add the manufacturing date, we write:
ALTER TABLE Product ADD COLUMN dateFab TIMESTAMP

3
INFORMATIQUE SECOND YEAR ESMT

 Delete a column
A syntax also allows you to delete a column for a table:
ALTER TABLE nom_table
DROP COLUMN nom_colonne
 Edit a column
To modify a column, such as changing the type of a column or changing the maximum
size of the column:
ALTER TABLE table_name
ALTER COLUMN column_name new_data_type
Note : If you want to rename a field in SQL, you must delete the field, then recreate it (i.e.
use DROP COLUMN then ADD COLUMN).
Example: To Change the Quantity type to « byte » (between 0 and 255) we write:
ALTER TABLE Product
ALTER COLUMN Quantity Byte

3- Data manipulation: Addition, Modification, Deletion


3-1. Adding data
Using SQL, we can also add information to a table with the INSERT INTO command.
To do this, you must indicate the table in which you want to integrate a row as well as the
list of fields for which you specify a value, and finally the list of corresponding values.
Example :
INSERT INTO product (BarCode,PriceU, NameP,Color, Quantity,weight,volume,Fabdate)
VALUES ('0117',100, 'Chair', 'White', 20,4.3,6.5,'14/11/2022 10: 00:00')
Or
INSERT INTO product VALUES ('0117',100, 'Chair', 'White', 20,4.3,6.5,'14/11/2022
10:00:00')
The result of the previous query will be:

4
INFORMATIQUE SECOND YEAR ESMT

3-2. Data modification


You can modify certain fields of existing records using the UPDATE keyword: this
instruction allows you to update several fields of several records in a table, based on the
expressions provided to it.
To do this, you must indicate the name of the table to update as well as the new values
to assign to the fields. The SET keyword will allow this assignment.
If we want to modify only a limited set of records, we must use the WHERE clause. If no
clause is indicated, the modification will be made on all records in the table!
For example, to modify the quantity of the product which has the barcode “0118”:
UPDATE Product SET Quantity = 300 WHERE Barcode = ‘0118’
The modification of the quantity of the product will only affect the record which has the
barcode 0118! Whereas if you want the update to be global, there is no need to put a
where clause.
3-2. Data deletion
It may be necessary to delete one or more records from a table; the DELETE FROM
instruction exists for this. We must then provide at least the name of the table on which
the deletion will be carried out.
If there is only the name of the table entered, then all records in this table will be
deleted.
To limit the deletion, we reuse the WHERE clause.
The following command clears all records from the product table:
DELETE FROM product
Whereas this one only deletes one record:
DELETE FROM product WHERE barcode = ‘0116’
4. Information Research:
The most common use of SQL is to read data from the database. This is done using the
SELECT command, which returns records in a result table. This command can select one or
more columns from a table and display them.
Syntax: SELECT field_name FROM table_name

You might also like