1. Trang chủ
  2. » Công Nghệ Thông Tin

Secure PHP Development- P159 pdf

5 135 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 95,9 KB

Nội dung

Following is the second line: create table customers ( The preceding line tells the SQL server you are trying to create a table called customers. Following is the third line: name CHAR(40) NOT NULL, The preceding line creates a column named name that contains 40 characters of data per item. Additionally, it specifies that this column can never be null, or with- out data. Following is the fourth line: address CHAR(80), The preceding line creates a column called address that holds 80 characters of data per item. Following is the fifth line: telephone CHAR(13)); The preceding line creates a column called telephone that holds 13 characters. This is set up as a character field instead of a numeric field because you would rarely, if ever, need to add telephone numbers or perform mathematical functions with them. You can just treat the telephone number as you would any other char- acter data. Moreover, this makes it easier to deal with the dashes people put in tele- phone numbers. Listing tables in a database To see a list of tables in a database, you can run the following command: show tables; This command is run from the MySQL command-line client. Make sure you run this command after you have run the use command to change to the database. For example: mysql> use store; mysql> show tables; 766 Part VII: Appendixes 33 549669 AppC.qxd 4/4/03 9:28 AM Page 766 The preceding commands display the following: mysql> show tables; + + | Tables_in_store | + + | customers | + + 1 row in set (0.00 sec) Viewing table descriptions To find out information about a table, run the following command: desc table_name; For example: mysql> use store; mysql> desc customers; The preceding commands display the following: + + + + + + + | Field | Type | Null | Key | Default | Extra | + + + + + + + | name | char(40) | | | | | | address | char(80) | YES | | NULL | | | telephone | char(13) | YES | | NULL | | | id | int(11) | | PRI | NULL | auto_increment | + + + + + + + 4 rows in set (0.00 sec) Inserting data into a database To insert data in a table, you need to know the field names and their types, and then use the INSERT statement. Use the desc table_name command to find this information, and then you can run the following: insert into table_name (comma separated field list) values(properly quoted and comma separated value list); Appendix C: MySQL Primer 767 33 549669 AppC.qxd 4/4/03 9:28 AM Page 767 For example, to add data to the customers table in the store database, you can run the following: mysql> insert into customers (name,address,telephone) values(‘Mohammed Kabir’,’2904 Wead Way, Sacramento, CA 95833’, ‘555-5555’); Query OK, 1 row affected (0.33 sec) Note that the values should be properly quoted if the values contain any characters that could confuse the statement. Such characters include the comma (“,”), a space (“ “), a quote (“””), etc. If you must use a quote character of any type in the data, be sure to use the opposite quote to surround the values. For example, if you use an apostrophe (which is a single-quote), sur- round the data with a double-quote. The preceding command inserts a row in the customers table. Notice that we do not use the id field in the insert statement because it is set to auto_increment and therefore is automatically inserted as the next available value. Note that you do not have to ever explicitly deal with setting an auto_increment field, nor do you have to worry about multiple clients causing an error — the MySQL developers have taken care of almost any contingencies. Querying data from a database To find data in a table, you need to use SELECT statements, as shown here: select field_list FROM table_name WHERE conditions; This statement enables you to find field_list data from a table called table_name, where the data meets the given conditions. For example: mysql> select name from customers where id = 1; + + | name | + + | Mohammed Kabir | + + 1 row in set (0.01 sec) 768 Part VII: Appendixes 33 549669 AppC.qxd 4/4/03 9:28 AM Page 768 In the preceding example, the record matching ID = 1 condition is returned from the customers table. Another example is as follows: mysql> select * from customers; + + + + + | name | address | telephone | id | + + + + + | Mohammed Kabir | 2904 Wead Way, Sacramento, CA 95833 | 555-5555 | 1 | | Sheila Kabir | 2904 Wead Way, Sacramento, CA 95833 | 555-5555 | 2 | + + + + + 2 rows in set (0.01 sec) Here, the field_list is set to the asterisk (*), to indicate all data. Because this statement did not use any conditions, all records were returned. (The “Sheila” data was not explicitly entered in this chapter, but is shown to further this example.) You can also limit the number of records returned in a query: mysql> select * from customers limit 3; + + + + + | name | address | telephone | id | + + + + + | Mohammed Kabir | 2904 Wead Way, Sacramento, CA 95833 | 555-5555 | 1 | | Sheila Kabir | 2904 Wead Way, Sacramento, CA 95833 | 555-5555 | 2 | | Joe Public | 6000 J St, Sacramento, CA 95822 | 555-5555 | 3 | + + + + + 3 rows in set (0.00 sec) Appendix C: MySQL Primer 769 33 549669 AppC.qxd 4/4/03 9:28 AM Page 769 Here, only three records were returned from the query results because of the given LIMIT condition. (Again, the extra data is implicitly added for this example.) Limit actually takes an optional second argument, an offset for the record to start with. For example, if you specify LIMIT 10,3 in a SELECT statement, the data returned will start with the tenth record of the return set and return only three records from there. This option can be used to step through data an arbitrary num- ber of records at a time — supposing that you uniquely order the data with each SELECT. Updating data in a database To update a record in a table within a database, you need to use the UPDATE state- ment, as shown in the following example: UPDATE table_name SET field=value list WHERE condition; This statement will update records in a table that matches the given condition. Only the given fields in the field=value list will be updated with new values. For example: mysql> update customers set telephone = ‘555-444’ where ID = 1; Query OK, 1 row affected (0.32 sec) Rows matched: 1 Changed: 1 Warnings: 0 Note the use of the WHERE clause. This clause specifies how to select the records to update and is usually a field (or two) and data to match. Here, the record #1 is updated with a new telephone number. Forgetting to use a condition will update all records! Removing data from a database To remove records from a table, you can use the DELETE statement, as shown in the following example: DELETE from table_name WHERE condition; This statement will delete all rows that match the given condition, as shown here: mysql> delete from customers where ID > 1; Query OK, 4 rows affected (0.01 sec) Here, all records with an ID greater than 1 are deleted from the table. Use the DELETE statement very carefully, as you cannot undo a delete operation. If you don’t use a WHERE clause you risk deleting all records! 770 Part VII: Appendixes 33 549669 AppC.qxd 4/4/03 9:28 AM Page 770

Ngày đăng: 07/07/2014, 07:20

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN