Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Mekelle Institute of Laboratory manual for Technology(MIT) Database Management Mekelle,Tigray ,Ethiopia

Systems(DBMSs) Lab manual : 3

Basic MySQL Commands In our second lab exercise we have seen basic introduction to MySQL and we have started to use some MySQL commands .This lab exercise is the continuation of the second exercise and it will give you more details about the most important MySQL statements or commands.

Please note the use of following MySQL Commands:


a) b) c) d) e) f) mysql>CREATE DATABASE CSEdb; mysql>USE CSEdb; mysql>SHOW DATABASES; mysql>DROP DATABASE CSEdb; mysql>SHOW DATABASES; Is your database available in the list of databases? mysql>CREATE BATABASE CSEdb;

1. CREATING TABLES Syntax: Create table table_name (column 1 data type1 [constraint 1] column n data type n [constraint n]) ENGINE= Engine type;

Example 1: CREATE TABLE PARENT (PARENT_ID INT NOT NULL, PARENT_NAME VARCHAR (30) NOT NULL, PRIMARY KEY (PARENT_ID)) ENGINE=INNODB; See the description of table PARENT.

Example 2: CREATE TABLE CHILD (CHILD_ID INT NOT NULL, CHILD_NAME VARCHAR (30) NOT NULL, PARENT_ID INT,INDEX par_id(PARENT_ID), FOREIGN KEY (PARENT_ID) REFERENCES PARENT (PARENT_ID) ON UPDATE CASCADE DELETE CASCADE) ENGINE=INNODB; See the description of table CHILD.

2. DROPPING TABLES Syntax: Drop table table_name; Example: DROP TABLE PARENT; After it is dropped your table no more exists. So create the table PARENT again using the syntax from example 1above. 3. THE ALTER COMMAND a) USING ALTER to change table name Syntax: ALTER TABLE existing_table_name TO new_table_name; Example: mysql>ALTER TABLE PARENT TO PARENTS; mysql> SHOW TABLES; mysql>ALTER TABLE PARENTS TO PARENT; We can also use the RENAME Command to change the name of an existing table into a new one. Syntax: RENAME TABLE existing_table_name TO (AS)new_table_name; Example: mysql>RENAME TABLE CHILD TO CHILDREN; mysql> describe CHILDREN; mysql> describe CHILD; mysql> SHOW TABLES; mysql>RENAME TABLE CHILDREN AS CHILD; b) USING ALTER to ADD a column

Syntax: ALTER TABLE existing_table_name ADD new_column_name data type; Example: mysql>ALTER TABLE PARENT ADD age int; c) USING ALTER to Change the data type of existing column Syntax: ALTER TABLE existing_table_name MODIFY column_name new_data type; Example: mysql>ALTER TABLE PARENT MODIFY name char (30);

d) USING ALTER to Change the name of an existing column Syntax: ALTER TABLE existing_table_name CHANGE old_column_name new_column_name data type; Example: mysql>ALTER TABLE PARENT CHANGE name parent_name varchar (30); e) USING ALTER to drop an existing column Syntax: ALTER TABLE existing_table_name DROP [COLUMN]column_name ; Example: mysql>ALTER TABLE PARENT DROP age; mysql>ALTER TABLE PARENT DROP COLUNM age;

4. INSERTING DATA INTO ALLTHE COLUMNS OF THE CREATED TABLES Syntax: INSERT INTO table_name VALUES (V1, V2 Vn); Where table_name is the name of the table on which the values are to be inserted in and V1, V2 Vn are values of the corresponding columns of the table. Note that string values must be placed inside single quote. Example 1: INSERT INTO PARENT VALUES (100,Ato Belay Hadgu); INSERT INTO PARENT VALUES (101,Ato Hadgu Belay); INSERT INTO PARENT VALUES (102,Ato Gebru Asrat);

Example 2: INSERT INTO CHILD VALUES (300,Alem, 100); INSERT INTO CHILD VALUES (301,Lema, 101); INSERT INTO CHILD VALUES (302,Kessete, 102);

5. INSERTING DATA INTO SPECIFIC COLUMNS OF THE CREATED TABLES Syntax: INSERT INTO table_name (Column 1, Column 2 Column x) VALUES (V1, V2 Vx); Where table_name is the name of the table on which the values are to be inserted in, Column 1, Column 2 Column x are the specific columns and V1, V2 Vx are values of the corresponding columns. Note that string values must be placed inside single quote.

Example 2: INSERT INTO CHILD (CHILD_ID, CHILD_NAME) VALUES (305,Hagos);

6. RETRIEVING VALUES FROM A TABLE a) Retrieving all values Syntax: SELECT * FROM table_name; Example: SELECT * FROM PARENT; SELECT * FROM CHILD;

b) Retrieving specific values Syntax: SELECT column 1, column 2, , column n FROM table_name; Example: SELECT CHILD_NAME, PARENT_ID FROM CHILD;

c) Retrieving all values using where condition Syntax: SELECT * FROM table_name WHERE condition; Example: SELECT * FROM PARENT WHERE PARENT_ID < 500;

d) Retrieving specific values using where condition Syntax: SELECT column 1, column 2,, column n FROM table_name WHERE condition; Example: SELECT CHILD_NAME, PARENT_ID FROM CHILD WHERE PARENT_ID < 400; 7. DELETING RECORDS Syntax: DELETE FROM table_name WHERE condition; Example: DELETE FROM PARENT WHERE PARENT_ID = 400; Note: The where condition is very important. Otherwise all your data in the table will be deleted. 8. UPDATING RECORDS Syntax: UPDATE table_name SET column_name = new_value WHERE condition; Example: UPDATE CHILD CHILD_NAME=Lemma; SET PARENT_ID = 400 WHERE

Note: The where condition is very important. Otherwise all your data in the table will be updated. Exercise: CREATE the following tables. Be careful while selecting the data types, constraints, primary key(s) and foreign key(s).After creating populate them with appropriate values. 1. BANK (BANK_NAME, BRANCH_NAME , ASSETS ) 2. CUSTOMER(CUST_ID,CUST_NAME,BANK_NAME ,BRANCH_ NAME,BALANCE )

You might also like