INTRODUCTION TO DATABASE & SQL
BY ISMAILA IBRAHIM ADAMU DEPT. OF INFORMATION TECHNOLOGY SULE LAMIDO UNIVERSITY KAFIN HAUSA
CREATING A NEW DATABSE
Creating a New Database
To create a new database, all you have to do is use the CREATE DATABASE command.
Type the following to create a new database called mydatabase :
CREATE DATABASE mydatabase;
CREATING A TABLE
Creating a New Table
As you know, tables are where you actually store your data.
To start with, you'll create a very simple table, STUDENT , containing three fields: StudentID (the
primary key), name (the name of student), and dept (department of the student).
CREATE TABLE student(
id INT UNSIGEND AUTO_INCREMENT,
name VARCHAR(30),
dept VARCHAR(30),
PRIMARY KEY (StudentID)
);
CREATING A NEW TABLE
You've created a table with the following three fields:
id is the primary key. It uniquely identifies each row of the table.You created the id field
as INT UNSIGNED , which means it can hold integer values up to 4,294,967,295.You also
specified the keyword AUTO_INCREMENT . This ensures that, whenever a new row is
added to the table, the id field automatically gets a new unique value (starting with 1).
This means you don't have to specify this field's value when inserting data
name will store the name of each fruit. It's created as VARCHAR(30) , which means it can
hold strings of up to 30 characters in length.
dept was created in the same way as name , and will be used to store the name of each
student.
ADDING DATA TO A TABLE
To add a new row to a table, you use the SQL INSERT statement. In its basic form, an
INSERT statement looks like this:
INSERT INTO table VALUES ( value1 , value2 , ... );
This inserts values into each of the fields of the table, in the order that the fields were
created.
Alternatively, you can create a row with only some fields populated. The remaining fields
will contain NULL (if allowed), or in the case of special fields such as an
AUTO_INCREMENT field, the field value will be calculated automatically.
To insert a row of partial data, use:
INSERT INTO table ( field1 , field2 , ... ) VALUES ( value1, value2 , ... );
ADDING DATA TO A TABLE
Now try adding some fruit to your table. you can add three rows to the fruit table by
inserting data into just the name and color fields (the id field will be filled automatically):
INSERT INTO student ( name, dept ) VALUES ( ‘mujahid’, ‘IT’ );
INSERT INTO student ( name, dept ) VALUES ( ‘Maryam’, ‘CSC’ );
INSERT INTO student ( name, dept ) VALUES ( ‘musa’, ‘SWE’ );
READING DATA FROM A TABLE
To read data in SQL, you create a query using the SELECT statement.
name of the table you
want to read data
from
SELECT field1, field2, ... FROM table WHERE condition;
name of the fields you
want to read from the
table e.g age
You can filter the
results using the
condition. e.g. age = 15
READING DATA FROM A TABLE
If you want to read all fields from a table, you can use * instead of including all field
names.
SELECT * FROM fruit;
This SQL statement returns all records in the fruit table.
studentID Name Dept
1 Mujahid IT
2 Maryam CSC
3 Musa SWE
READING DATA FROM A TABLE
Now if we want to retrieve a specific set of records with a given condition we can
add WHERE conditions to our query:
SELECT * FROM student where studentID = ‘2';
This query will return all records in the student table with the studentID ‘2’
studentID Name Dept
2 Maryam CSC
DELETING DATA FROM A TABLE
Deleting works in a similar way to updating.
To delete rows, you use the DELETE statement. If you add a WHERE clause, you can
choose which row or rows to delete; otherwise all the data in the table are deleted
(though the table itself remains)