Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Microsoft SQL Server wikipedia , lookup
Concurrency control wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Ingres (database) wikipedia , lookup
Functional Database Model wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Clusterpoint wikipedia , lookup
Csc202/csc322 Data Processing and Database Systems Introduction to MySQL Aim of this practical In this practical we are going to familiarise ourselves with MySQL, implement a small database in MySQL and then perform some simple queries on it. At the end of the practical you will be able to develop your own database in MySQL. MySQL MySQL is a widely used relational database management system, where data is stored in a series of related tables. MySQL has been developed on the client/server model and written in C++. MySQL is an interactive program that allows you to connect to a MySQL server, run queries and view results (it can also be used in batch mode). There is an abundance of information on the web relating to MySQL. As a starting point please refer to www.mysql.com and visit the Developer Zone. The SQL part of MySQL refers to “Structured Query Language” which you will be familiar with from your lectures and it is the most common language used to access databases. Getting Started From the Start menu choose Programs followed by Accessories followed by Command Prompt. You will have been given a username and a password. At the command prompt type the following command: mysql –h csweb2.cs.qub.ac.uk –u your username -p You will be prompted for a password. Type it in capital letters as it has been given to you. Once you have the mysql> prompt we recommend that you change your password for your own security. Type the following: SET PASSWORD = PASSWORD(‘newpassword’); And press Return. Try to remember this new password as you will need it the next time you log on. Your database name is the same as your username. The final step before you can type your own SQL statements is to connect to the database. Type the following command CONNECT database name; The database The database we will develop is based on the ER diagram below: Programmers Work_on M Work N Projects An N:M relationship becomes a separate relation so for the above ER diagram the relational schema is as follows: PROGRAMMER (STAFF_NO, NAME, OFFICE_NO, SALARY, JOINING_DATE) PROJECT (TITLE, START_DATE, DURATION, BUDGET) WORK_ON (STAFF_NO, TITLE) 1 Csc202/csc322 Data Processing and Database Systems Each relation in the schema will be a table in the database. Creating a Table Use the SHOW statement to find out what databases currently exist on the server: mysql> SHOW DATABASES; +----------+ | Database | +----------+ | student1 | +----------+ The name of the database is probably different on your machine. There are currently no tables in your database. Type SHOW TABLES; to verify that fact. In SQL the CREATE TABLE command is used to set up a new table (i.e. to implement a relation). For example, to create the table programmer with the attributes staff_no of type integer, name of type varchar, office_no of type integer, salary of type integer and joining_date of type date and staff_no as the primary key, type the following command: CREATE TABLE programmer(staff_no INTEGER, name VARCHAR(20), office_no INTEGER, salary INTEGER, joining_date DATE, PRIMARY KEY(staff_no)); When typing a long statement like this one you will have to press return a few times. This is not a problem as MySQL determines where your statement ends by looking for the terminating semicolon, not by looking for the end of the input line. (In other words, MySQL accepts free-format input: it collects input lines but does not execute them until it sees the semicolon.) This is what simple multiple-line statement would look like. mysql> SELECT * -> FROM programmer; Now that you have created a table, SHOW TABLES should produce some output: mysql> SHOW TABLES; +---------------------+ | Tables_in_student1 | +---------------------+ | programmer | +---------------------+ To verify that your table was created the way you expected, use a DESCRIBE statement: mysql> DESCRIBE programmer; 2 Csc202/csc322 Data Processing and Database Systems +--------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+-----------------+ | staff_no | integer | | PRI | NULL | 0 | | name | varchar(20) | YES | | NULL | | | office_no | integer | YES | | NULL | | | salary | integer | YES | | NULL | | | joining_date | date | YES | | NULL | +--------------+--------------+------+-----+---------+-------+ You can use DESCRIBE any time, for example, if you forget the names of the columns in your table or what types they have. Populate your table with data There are two ways to insert data into a table: 1. Type the following command to insert one row of data: INSERT INTO programmer VALUES (123, ‘Little,Mary’, 12,49000,’1980-6-5’); note- date format is ‘YYYY-MM-DD’ 2. You can also copy data from a file. Take a copy of the files programmer.txt and project.txt from S:\library\level2\csc202 into your home directory. Now type the following command LOAD DATA LOCAL INFILE 'I:\/path/programmer.txt' INTO TABLE programmer LINES TERMINATED BY '\r\n'; To look at your table with the data type the following command: SELECT * FROM PROGRAMMER; The update command is used to change an existing value. Type the following command: UPDATE programmer SET salary = 52000 WHERE name = ‘McIntyre,Jack’; Use the select command to check that the update has been carried out correctly. See Appendix 1 for information on using batch files for more efficient processing of your SQL statements. See Appendix 2 for information on how to print from MySql. Create your own table and do some queries. The data for the project table is in the file project.txt. Create a project table with the following fields: title of type varchar, start_date of type date, duration of type varchar and budget of type integer. Use the LOAD DATA LOCAL INFILE command to copy the data into the project table. Now perform the following queries: (i) List everything in the project table. SELECT * FROM PROJECT; (ii) What is the total project budget within the company? SELECT SUM(budget) FROM PROJECT; 3 Csc202/csc322 Data Processing and Database Systems (iii) Now here is a query requiring the use of the programmer table. List the names and office numbers of all pairs of programmers who share an office. SELECT p1.name, p2.name, p1.office_no FROM programmer AS p1, programmer AS p2 WHERE p1.office_no = p2.office.no AND p1.staff_no < p2.staff_no; (iv) This query illustrates how you can compare a table to itself. p1 and p2 are both copies of the programmer table. Remove all projects with a budget of less than 3000. DELETE FROM project WHERE budget < 3000; List the project table to ensure the deletion has occurred. Create your own data and corresponding table As yet the Work_on table does not exist. Use Notepad to create a file with the following data: 001 003 003 004 Payroll Newdirectory Filmstudio Payroll (Note: use tabs between each piece of data in a row. Beware of extra blank lines and spaces. When you have typed the last piece of data, in this case Payroll, press return once.) Create the table in MySQL and copy the data into it. (v) (vi) List everything in the WORK_ON table. Finally, retrieve the names of programmers who work on at least one project with a budget over 4000. To exit from MySQL type exit at the command prompt. 4 Csc202/csc322 Data Processing and Database Systems Appendix 1 You can run mysql in batch mode. To do this, put the commands you want to run in a file, then type the following : mysql> source filename; There are good reasons for using a script You can generate new queries from existing ones that are similar by copying and editing script files. If you run a query repeatedly (say, every day or every week), making it a script allows you to avoid retyping it each time you execute it. Appendix 2 In order to print out your results for this practical there are two commands that you need to be aware of typing tee filename at the MySQL prompt appends everything that you do from now on into the file that you have indicated. This file can then be opened in Notepad, tidied up and then printed. typing notee at the MySQL prompt will stop the writing to the output file. 5