300 likes | 305 Views
Learn how to install MySQL, set up databases and tables, execute queries, modify and delete records, add foreign keys, and perform other essential tasks. This tutorial provides step-by-step instructions and handy commands.
E N D
By Mr. Jiaming Su MySQL Tutorial CS3754 Classnote #9
CS3754 Classnote #9 Install MySQL • Where to find the software • http://www.mysql.com/products/database/mysql/community_edition.html • Steps of the Installation
CS3754 Classnote #9 New program installed
CS3754 Classnote #9 Starting MySQL
CS3754 Classnote #9 Change Password • Type in the following commends: • delete from user where Host <> 'localhost' and User <> 'root'; • delete from db; • update user set Password=password('NewPd') where User='root'; • flush privileges; • exit
CS3754 Classnote #9 Log in
CS3754 Classnote #9 After you log in
CS3754 Classnote #9 Build a database • Create databases • create database University; • Use a database • use University; • Create tables • create table table_Name (field_1 integer, field_2 char(10), …); • List Tables • show tables;
CS3754 Classnote #9 Results
CS3754 Classnote #9 Results (Cont’1)
CS3754 Classnote #9 Results (Cont’2)
CS3754 Classnote #9 Results (Cont’3)
CS3754 Classnote #9 Results (Cont’4)
CS3754 Classnote #9 Put data into a database • Insert a record to a table • insert into table_1 (field_1, field_2) values (1, 'first');
CS3754 Classnote #9 Show records
CS3754 Classnote #9 Modify Database • Modify one field of records • update table_1 set field_1=5 where field_1=1; e.g., update employee set sex = ‘F’ where ssn=‘665170901’; • Update multiple records in one stroke • update table_1 set field_5=12 where field_1>11; • Deleting Records • delete from table_1 where field_1=3;
CS3754 Classnote #9 Alter Database • Adding a field of a table • alter table table_1 add column field_5 char(20); • Delete a database, table, … • drop database_name; • drop Table_name; • Add or delete a index • add index ind_name (column_name, …); • drop index ind_name;
CS3754 Classnote #9 Add foreign Keys
CS3754 Classnote #9 After adding foreign keys
CS3754 Classnote #9 Query database • select column_name, … from table_name, … where column=condition, … ; • Note: any join query need follow relationships between tables.
CS3754 Classnote #9 An Join Query
CS3754 Classnote #9 Quit Database • Quit • bye