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

DBMS Lab 3-4

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

DBMS Lab 3-4

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

Sample queries for creation of database and conditional queries.

CREATE DATABASE list;

CREATE DATABASE IF NOT EXISTS list;

Tables can be created using CREATE TABLE statement and it has the
following syntax.
CREATE TABLE [IF NOT EXISTS] `TableName` (`fieldname` dataType [optional parameters]);

Below is a MySQL example to create a table in database:


CREATE TABLE IF NOT EXISTS `MyFlixDB`.`Members` (
`membership_number` INT AUTOINCREMENT ,0
`full_names` VARCHAR(150) NOT NULL ,
`gender` VARCHAR(6) ,
`date_of_birth` DATE ,
`physical_address` VARCHAR(255) ,
`postal_address` VARCHAR(255) ,
`contact_number` VARCHAR(75) ,
`email` VARCHAR(255) ,
PRIMARY KEY (`membership_number`) );
Numeric Data types
Numeric data types are used to store numeric values. It is very important to make sure range of your
data is between lower and upper boundaries of numeric data types.

-128 to 127 normal


TINYINT( )
0 to 255 UNSIGNED.
-32768 to 32767 normal
SMALLINT( )
0 to 65535 UNSIGNED.
MEDIUMINT( -8388608 to 8388607 normal
) 0 to 16777215 UNSIGNED.
-2147483648 to 2147483647 normal
INT( )
0 to 4294967295 UNSIGNED.
-9223372036854775808 to 9223372036854775807 normal
BIGINT( )
0 to 18446744073709551615 UNSIGNED.
FLOAT A small approximate number with a floating decimal point.
DOUBLE( , ) A large number with a floating decimal point.
A DOUBLE stored as a string , allowing for a fixed decimal point. Choice
DECIMAL( , )
for storing currency values.

Text Data Types


As data type category name implies these are used to store text values. Always make sure you length of
your textual data do not exceed maximum lengths.

CHAR( ) A fixed section from 0 to 255 characters long.


VARCHAR( ) A variable section from 0 to 255 characters long.
TINYTEXT A string with a maximum length of 255 characters.
TEXT A string with a maximum length of 65535 characters.
BLOB A string with a maximum length of 65535 characters.
MEDIUMTEXT A string with a maximum length of 16777215 characters.
MEDIUMBLOB A string with a maximum length of 16777215 characters.
LONGTEXT A string with a maximum length of 4294967295 characters.
LONGBLOB A string with a maximum length of 4294967295 characters.

Date / Time

DATE YYYY-MM-DD
DATETIME YYYY-MM-DD HH:MM:SS
TIMESTAMP YYYYMMDDHHMMSS
TIME HH:MM:SS

Apart from above there are some other data types in MySQL.

ENUM To store text value chosen from a list of predefined text values
This is also used for storing text values chosen from a list of predefined text
SET
have multiple values.
BOOL Synonym for TINYINT(1), used to store Boolean values
BINARY Similar to CHAR, difference is texts are stored in binary format.
VARBINARY Similar to VARCHAR, difference is texts are stored in binary format.
Table 1: members table

membershi full_name gende date_of physical postal contct email


p_ number s r _ birth _ _ _
address addres numbe
s r
1 Janet Femal 21-07- First Private 0759 [email protected]
Jones e 1980 Street Bag 253 m
Plot No 542
4
2 Janet Femal 23-06- Melrose NULL NULL [email protected]
Smith e 1980 123
Jones
3 Robert Male 12-07- 3rd NULL 12345 [email protected]
Phil 1989 Street 34
4 Gloria Femal 14-02- 2nd NULL NULL NULL
Williams e 1984 Street 23

Table 2: movies table

movie_id title director year_released category_id


1 Pirates of the Caribean 4 Rob Marshall 2011 1
2 Forgetting Sarah Marshal Nicholas Stoller 2008 2
3 X-Men NULL 2008 NULL
4 Code Name Black Edgar Jimz 2010 NULL
5 Daddy’s Little Girls NULL 2007 8
6 Angels and Demons NULL 2007 6
7 Davinci Code NULL 2007 6
9 Honey mooners John Schultz 2005 8
16 67% Guilty NULL 2012 NULL
Let’s say we are only interested in getting only the full_names, gender,
physical_address and email fields only. The following script would help us to
achieve this.
SELECT `full_names`,`gender`,`physical_address`, `email` FROM `members`;

Executing the above script in MySQL workbench produces the following


results.

full_names gender physical_address email


Janet Jones Female First Street Plot No 4 [email protected]
Janet Smith Jones Female Melrose 123 [email protected]
Robert Phil Male 3rd Street 34 [email protected]
Gloria Williams Female 2nd Street 23 NULL

Remember in our above discussion that we mention expressions been used


in SELECT statements. Let’s say we want to get a list of movie from our
database. We want to have the movie title and the name of the movie
director in one field. The name of the movie director should be in brackets.
We also want to get the year that the movie was released. The following
script helps us do that.
SELECT Concat(`title`, ' (', `director`, ')') , `year_released` FROM `movies`;

Executing the above script in MySQL workbench produces the following


result set.

Concat(`title`, ‘ (‘, `director`, ‘)’) year_released


Pirates of the Caribean 4 ( Rob Marshall) 2011
Forgetting Sarah Marshal (Nicholas Stoller) 2008
NULL 2008
Code Name Black (Edgar Jimz) 2010
NULL 2007
NULL 2007
NULL 2007
Honey mooners (John Schultz) 2005
NULL 2012
Suppose we want to get a member’s personal details from members table
given the membership number 1, we would use the following script to
achieve that.
SELECT * FROM `members` WHERE `membership_number` = 1;

Executing the above script on the “myflixdb” would produce the following
results.

membership_number full_names gender date_of_birth physical_address postal_address contc


1 Janet Jones Female 21-07-1980 First Street Plot No 4 Private Bag 0759

WHERE clause combined with – AND LOGICAL


Operator
The WHERE condition in MySQL when used together with the AND logical
operator, is only executed if ALL filter criteria specified are met.

Suppose we want to get a list of all the movies in category 2 that were
released in 2008, we would use the script shown below is achieve that.

SELECT * FROM `movies` WHERE `category_id` = 2 AND `year_released` = 2008;

Executing the above script in MySQL workbench against the “myflixdb”


produces the following results.

movie_id title director year_released category_id


2 Forgetting Sarah Marshal Nicholas Stoller 2008 2

WHERE clause combined with – OR LOGICAL


Operator
The WHERE clause when used together with the OR operator, is only
executed if any or the entire specified filter criteria is met.
The following script gets all the movies in either category 1 or category 2
SELECT * FROM `movies` WHERE `category_id` = 1 OR `category_id` = 2;

Executing the above script in MySQL workbench against the “myflixdb”


produces the following results.

movie_id title director year_released category_id


1 Pirates of the Caribean 4 Rob Marshall 2011 1
2 Forgetting Sarah Marshal Nicholas Stoller 2008 2

WHERE clause combined with – IN Keyword


The WHERE in MySQL clause, when used together with the IN keyword only
affects the rows whose values matches the list of values provided in the IN
keyword. The MySQL IN statement helps to reduce number of OR clauses
you may have to use.
The following MySQL WHERE IN query gives rows where
membership_number is either 1 , 2 or 3

SELECT * FROM `members` WHERE `membership_number` IN (1,2,3);

Executing the above script in MySQL workbench against the “myflixdb”


produces the following results.

membership_number full_names gender date_of_birth physical_address postal_address con


1 Janet Jones Female 21-07-1980 First Street Plot No 4 Private Bag 075
2 Janet Smith Jones Female 23-06-1980 Melrose 123 NULL NU
3 Robert Phil Male 12-07-1989 3rd Street 34 NULL 123

You might also like