* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download MYSQL Introduction MySQL A database that is available for no
Open Database Connectivity wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Ingres (database) wikipedia , lookup
Clusterpoint wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
MYSQL Introduction MySQL A database that is available for no charge (open source) but with a commercial license for those who want to use MySQL as part of a new software product and wants to sell the new product. The developer would need to purchase a commercial license. The fee is very reasonable. Advantages: It’s fast. The main goal of the folks who developed MySQL was speed. Consequently, the software was designed from the beginning with speed in mind. It’s inexpensive. MySQL is free under the open source GPL license, and the fee for a commercial license is very reasonable. It’s easy to use. You can build and interact with a MySQL database by using a few simple statements in the SQL language, which is the standard language for communicating with RDBMSs. It can run on many operating systems. MySQL runs on a wide variety of operating systems — Windows, Linux, Mac OS, most varieties of Unix (including Solaris, AIX, and DEC Unix), FreeBSD, OS/2, Irix, and others. Technical support is widely available. A large base of users provides free support via mailing lists. The MySQL developers also participate in the e-mail lists. You can also purchase technical support from MySQL ABfor a very small fee. It’s secure. MySQL’s flexible system of authorization allows some or all database privileges (for example, the privilege to create a database or delete data) to specific users or groups of users. Passwords are encrypted. It supports large databases. MySQL handles databases up to 50 million rows or more. The default file size limit for a table is 4GB, but you can increase this (if your operating system can handle it) to a theoreticallimit of 8 million terabytes (TB). It’s customizable. The open source GPL license allows programmers to modify the MySQL software to fit their own specific environments. Load WampServer and get to phpMyAdmin link. You will see an interface for creating MySQL database and table. Note that Home icon is very valuable. What happens when clicked? Creating database and table – or choosing them Create database or choose an existing one. To create, you type database name and create. (Existing ones will be shown on the left column. You click to choose.) Then create a table or choose an existing one. (Existing one will be shown on the left column below a database when the database is chosen. You click to choose.) When creating a table you give table name and specify number of fields. Then choose go. You would see an interface for creating table structure. You specify field names, and type at least. Next save the table. 1 Working with tables When the table is selected, then towards the top, you see several menus: Browse, structure, SQL, Search, Insert, Export, Import, Operations and drop. You use these as need be. Browse – for presenting records Structure – for table structure. With this, you can set primary key, add a field (see menu at the bottom), change or drop a field. (See heading action for these last two). SQL - for querying records etc and updating Insert - for adding records Operations- for performing actions such sorting. Drop – for deleting the table. When you select a database, you see several menus towards the top. Included are privileges and drop. Drop is for deleting a database. Privileges are for controlling access to the database. For this you will add new user and grant privileges. To add new user, you go through add new user. There you specify USER NAME, HOST AND PASSWORDS. (You use these later when linking php application and the database, hence the table.) Then on database user, grant privileges on the database and tick the privileges. If all are granted then check all. This will be for the user only. Several database users could be there with varied privileges. Export and import of tables Export Export – to store in different place Go through phpMyAdmin Select database, then table and click export Under export, ensure SQL in text is selected then go. Choose save a file and select the location then save (if you cannot select location, save using default one). A file by name, of the table with extension .sql will be saved in the location you chose when saving. In case you used the default saving location, you now need to save or copy to medium of your choice – say flash disk. Import Now to get the file ready for import – the one you saved – if in flash disk, ensure you insert it to your computer Go through phpMyAdmin Select database where the table will be then select import and browse to get to the .sql file you had saved then open and go. Field types There are several field types. Included are: CHAR(length) Fixed-length character string. VARCHAR(length) Variable-length character string. The longest string that can be stored is length, which must be between 1 and 255. TEXT Variable-length character string with a maximum length of 64KB of text. 2 INT(length) Integer with a range from –2147483648 to +2147483647. The number that can be displayed is limited by length. For example, if length is 4, only numbers from –999 to 9999 can be displayed, even though higher numbers are stored. INT(length) UNSIGNED Integer with a range from 0 to 4294967295. length is the size of the number that can be displayed. For example, if length is 4, only numbers up to 9999 can be displayed, even though higher numbers are stored. Generally you will use the following field types as you created tables: INT TEXT FLOAT Privileges Privileges are permissions. Permission Description ALL- All permissions ALTER- Can alter the structure of tables CREATE -Can create new databases or tables DELETE -Can delete rows in tables DROP -Can drop databases or tables FILE -Can read and write files on the server GRANT -Can change the permissions on a MySQL account INSERT- Can insert new rows into tables SELECT -Can read data from tables SHUTDOWN -Can shut down the MySQL server UPDATE- Can change data in a table USAGE - No permissions at all Granting ALL is not a good idea because it includes permissions for administrative operations, such as shutting down the MySQL server. SQL SQL (Structured Query Language) is the computer language that you use to communicate with MySQL. SQL is almost English. The queries include: o CREATE, DROP, ALTER, SHOW, INSERT, LOAD, SELECT, UPDATE, and DELETE. To show databases - SHOW DATABASES You can delete any database with this SQL query: o DROP DATABASE databasename To see the tables that have been added to a database, use this SQL query: o SHOW TABLES You can remove any table with this query: o DROP TABLE tablename Adding one row at a time You use the INSERT query to add a row to a database. This query tells MySQL which table to add the row to and what the values are for the fields in the row. The general form of the query is: INSERT INTO tablename ( ,....,columnname) 3 VALUES (value, value,....,value) columnname, columnname The following rules apply to the INSERT query: o Values must be listed in the same order in which the column names are listed. The first value in the value list is inserted into the column that’s named first in the column list; the second value in the value list is inserted into the column that’s named second in the column list; and so on. o A partial column list is allowed. You don’t need to list all the columns. Columns that are not listed are given their default value or left blank if no default value is defined. o A column list is not required. If you’re entering values for all the columns, you don’t need to list the columns at all. If no columns arelisted, MySQL will look for values for all the columns, in the order in which they appear in the table. o o The column list and value list must be the same length. If the list of columns is longer or shorter than the list of values, you get an error message like this: Column count doesn’t match value count. The following INSERT query adds a row to the Member table: INSERT INTO Member (loginName,createDate,password,lastName, street,city,state,zip,email,phone,fax) VALUES (“bigguy”,”2001-Dec-2”,”secret”,”Smith”, “1234 Happy St”,”Las Vegas”,”NV”,”88888”, “[email protected]”,”(555) 555-5555”,””) Retrieving information To retrieve specific information, list the columns containing the information you want. For example: o SELECT columnname,columnname,columnname,... FROM tablename o This query retrieves the values from all the rows for the indicated column(s). For instance, the following query retrieves all the last names and first names stored in the Member table: o SELECT lastName,firstName FROM Member o You can perform mathematical operations on columns when you select them. For example, you can use the following SELECT query to add two columns together: o SELECT col1+col2 FROM tablename The simplest, basic SELECT query is o SELECT * FROM tablename You can sort in descending order by adding the word DESC before the column name. For example: SELECT * FROM Member ORDER BY DESC lastName This query retrieves all the last names stored in the table named Member. Of course, more complicated queries (such as the following) are less English-like: SELECT lastName,firstName FROM Member WHERE state=”CA” AND city=”Fresno” ORDER BY lastName o This query retrieves all the last names and first names of members who live in Fresno and then puts them in alphabetical order by last name. Or you could use the following query: o SELECT price,price*1.08 FROM Pet 4 The result is the price and the price with the sales tax of 8 percent added on. You can combine expressions ANDs and ORs. In some cases, you need to use parentheses to clarify the selection criteria. For instance, you can use the following query to answer your boss’s urgent need to find all the people in the Member Directory whose names begin with B, who live in Santa Barbara, and who have an 8 in either their phone or fax number: SELECT lastName,firstName FROM Member WHERE lastName LIKE “B%” AND city = “Santa Barbara” AND (phone LIKE “%8%” OR fax LIKE “%8%”) Notice the parentheses in the last line. You would not get the results asked for without the parentheses. Without the parentheses, each connector would be processed in order from the first to the last, resulting in a list that includes all members whose names begin with B and who live in Santa Barbara and whose phone numbers have an 8 in them and all members whose fax numbers have an 8 in them, whether they live in Santa Barbara or not and whether their name begins with a B or not. When the last OR is processed, members are selected whose characteristics match the expression before the OR or the expression after the OR. LIMIT specifies how many rows can be returned. The form for LIMIT is LIMIT startnumber, numberofrows The first row that you want to retrieve is startnumber, and the number of rows that you want to retrieve is numberofrows. If startnumber is not specified, 1 is assumed. To select only the first three members who live in Texas, use this query: o SELECT * FROM Member WHERE state=”TX” LIMIT 3 Some SELECT queries will find identical records, but in this example, you only want to see one — not all — of the identical records. To prevent the query from returning all the identical records, add the word DISTINCT immediately after SELECT. Updating information Changing information in an existing row is updating the information. For instance, you might need to change the address of a member because she has moved, or you might need to add a fax number that a member left blank when he originally entered his information. The UPDATE query is very straightforward: UPDATE tablename SET column=value,column=value,... WHERE clause In the SET clause, you list the columns to be updated and the new values to be inserted. List all the columns that you want to change in one query. Without a WHERE clause, the values of the column(s) would be changed in all the rows. But with the WHERE clause, you can specify which rows to update. For instance, to update an address in the Member table, use this query: UPDATE Member SET street=”3333 Giant St”, phone=”555-555-5555” WHERE loginName=”bigguy” Removing information Keep the information in your database up to date by deleting obsolete information. You can remove a row from a table with the DELETE query: DELETE FROM tablename WHERE clause Be extremely careful when using DELETE. If you use a DELETE query without a WHERE clause, it will delete all the data in the table. The data cannot be recovered. Creating relationships between tables 5 Some tables in a database are related to one another. Most often, a row in one table is related to several rows in another table. A column is needed to connect the related rows in different tables. In many cases, you include a column in one table to hold data that matches data in the primary key column of another table. A common application that needs a database with two related tables is a customer order application. For example, one table contains the customer information, such as name, address, phone, and so on. Each customer can have from zero to many orders. You could store the order information in the table with the customer information, but a completely new row would be created each time that the customer placed an order, and each new row would contain the entire customer’s information. It would be much more efficient to store the orders in a separate table. The Order table would have a column that contains the primary key from a row in the Customer table so that the order is related to the correct row of the Customer table. How would you copy from one table to another? Database containing tables should be selected. The command is: use database_name You could then refer to tables with the use of dots. Examples assuming the tables come from the same database: SELECT columnnamelist FROM table1,table2 WHERE table1.col2 = table2.col2 SELECT * FROM Pet,Color WHERE Pet.petName = Color.petName How would you update? Could update command and the use of dot and comparison do? (SEARCH THE NET AND REASON.) Primary key This is unique identifier of records. It must not have a null value. Values should not be repeated. To make a field primary While in structure menu with the right table open, click on the primary key icon against the field. To change structure of table By using the ALTER query, you can change the name of the table; add, drop, or rename a column; or change the data type or other attributes of the column. You can also change the table structure with the use of change icon against the field when in structure menu. Then follow the process. You can also delete a field with the use of drop icon against the field when in structure menu. 6 To add a field, be on structure menu then check towards the bottom for items (or displays: add 1 field(s) ... at End of Table ... etc at then Go and act accordingly. 7