Post on 28-Nov-2014
description
hasen@microcis.net July 16, 2013 Hassen poreya Trainer, Cresco Solution
Afghanistan Workforce Development Program
Database MySQL, Modification Commands
INSERT Command
In its simplest form, it is used to add one or more tuples to a relation.
Tuples are the rows
Relations are the tables.
Attribute values should be listed in the same order as the attributes were specified in the CREATE TABLE command.
INSERT Command
INSERT INTO table name VALUES
(value1, value2, ...);
Values are separated by comma ,
Strings, Dates are enclosed by single quotation marks ‘’
Values have to be sorted according to the table structure (the columns)
INSERT Command
INSERT INTO employee VALUES
(1,'John',‘ Smith', 'Berlin', '1979-
09-04', 1000.40);
INSERT INTO project VALUES (1,
'Database Design', 'Kabul');
INSERT INTO works in VALUES(1,1,40);
INSERT Command
INSERT INTO employee VALUES
(NULL, 'Anna', 'Conner', 'London',
'1960-11-10‘,1.40);
What will happen if we put null value in the id field?
INSERT Command
If we used AUTO INCREMENT for the definition of a column, the database system will automatically compute a unique value for Id.
The value is computed by incrementing the maximum value for Id already in the database.
DELETE Command
Removes tuples from a relation.
Includes a WHERE clause to select the tuples to be deleted.
A missing WHERE clause species that all tuples in the relation are to be deleted; the table then becomes an empty table.
The number of tuples deleted depends on the number of tuples in the relation that satisfy the WHERE clause.
DELETE Command
DELETE FROM table name WHERE condition;
DELETE FROM employee WHERE id=1;
DELETE FROM project WHERE
location=‘kabul’;
INSERT Command: Integrity Constraints
Integrity Constraints:
One of the tasks of a database system is to make (automatically) sure that the stored data is in a consistent state.
When we model the Mini-World we also model what a consistent state means for us by defining integrity constraints.
In our company each employee is assigned a unique id as the PRIMARY KEY
Integrity Constraints: PRIMARY KEY
Primary Key
Database system automatically rejects all new records with a key that is already stored in the database as below:
Integrity Constraints: UNIQUE
UNIQUE
Similar to primary key: has to be unique
We do not want to use name as a primary key to identify records, but still we want to express that it has to be unique.
Integrity Constraints: NOT NULL
NOT NULL
Sometimes we want to make sure that a value is always assigned to a column, so we use NOT NULL
Primary keys are automatically defined as NOT NULL (we couldn't identify an employee if there is no Id stored)
Modification Command: UPDATE
It is used to modify attribute values of one or more selected tuples.
A WHERE clause selects the tuples to be modified.
An additional SET clause species the attributes to be modified and their new values.
Modification Command: SELECT
Extract data stored in some columns of some table.
General syntax is:
SELECT column name1, column name2, ... FROM
table name;
SELECT * FROM table name;
The asterisk (*) retrieves all attributes form the specified table.
SELECT Command: WHERE
Basic form of the SQL SELECT statement is called a mapping or a SELECT-FROM-WHERE- block.
SELECT < attributelist > FROM < tablelist > WHERE
< condition >
< condition > is a conditional (Boolean) expression that identifies the tuples to be retrieved by the query.