Beginning PHP and MySQL From Novice to Professional phần 8 docx

108 382 0
Beginning PHP and MySQL From Novice to Professional phần 8 docx

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

722 CHAPTER 28 ■ MYSQL STORAGE ENGINES AND DATATYPES Copying a Table It’s a trivial task to create a new table based on an existing one. The following query produces an exact copy of the employees table, naming it employees2: CREATE TABLE employees2 SELECT * FROM employees; An identical table, employees2, will be added to the database. Sometimes you might be interested in creating a table based on just a few columns found in a preexisting table. You can do so by simply specifying the columns within the CREATE SELECT statement: CREATE TABLE employees3 SELECT firstname,lastname FROM employees; Creating a Temporary Table Sometimes it’s useful to create tables that will have a lifetime that is only as long as the current session. For example, you might need to perform several queries on a subset of a particularly large table. Rather than repeatedly run those queries against the entire table, you can create a temporary table for that subset and then run the queries against it instead. This is accomplished by using the TEMPORARY keyword in conjunction with the CREATE TABLE statement: CREATE TEMPORARY TABLE emp_temp SELECT firstname,lastname FROM employees; Temporary tables are created just as any other table would be, except that they’re stored in the operating system’s designated temporary directory, typically /tmp or / usr/tmp on Linux. You can override this default by setting MySQL’s TMPDIR environ- ment variable. ■Note As of MySQL 4.0.2, ownership of the CREATE TEMPORARY TABLE privilege is required in order to create temporary tables. See Chapter 29 for more details about MySQL’s privilege system. Viewing a Database’s Available Tables You can view a list of the tables made available to a database with the SHOW TABLES statement: mysql>SHOW TABLES; Gilmore_862-8C28.fm Page 722 Friday, February 15, 2008 7:17 AM CHAPTER 28 ■ MYSQL STORAGE ENGINES AND DATATYPES 723 + + | Tables_in_company | + + | employees | + + 1 row in set (0.00 sec) Note that this is the standard methodology prior to MySQL version 5.0.0. Although the command is still available for versions 5.0.0 and greater, consider using the commands provided to you by way of the INFORMATION_SCHEMA. See the later section titled “The INFORMATION_SCHEMA” for more information about this new feature. Viewing a Table Structure You can view a table structure using the DESCRIBE statement: mysql>DESCRIBE employees; + + + + + + + | Field | Type | Null | Key | Default | Extra | + + + + + + + | id | tinyint(3) unsigned | | PRI | NULL | auto_increment | | firstname | varchar(25) | | | | | | lastname | varchar(25) | | | | | | email | varchar(45) | | | | | | phone | varchar(10) | | | | | + + + + + + + Alternatively, you can use the SHOW command like so to produce the same result: mysql>SHOW columns IN employees; Note that this is the standard methodology prior to MySQL version 5.0.0. Although the command is still available for versions 5.0.0 and greater, consider using the commands provided to you by way of the INFORMATION_SCHEMA, described in the upcoming section “The INFORMATION_SCHEMA.” Gilmore_862-8C28.fm Page 723 Friday, February 15, 2008 7:17 AM 724 CHAPTER 28 ■ MYSQL STORAGE ENGINES AND DATATYPES Deleting a Table Deleting a table, or dropping it, is accomplished via the DROP TABLE statement. Its syntax follows: DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name, ] For example, you could delete your employees table as follows: DROP TABLE employees; You could also simultaneously drop the employees2 and employees3 tables created in previous examples like so: DROP TABLE employees2 employees3; Altering a Table Structure You’ll find yourself often revising and improving your table structures, particularly in the early stages of development. However, you don’t have to go through the hassle of deleting and re-creating the table every time you’d like to make a change. Rather, you can alter the table’s structure with the ALTER statement. With this statement, you can delete, modify, and add columns as you deem necessary. Like CREATE TABLE, the ALTER TABLE statement offers a vast number of clauses, keywords, and options. It’s left to you to look up the gory details in the MySQL manual. This section offers several examples intended to get you started quickly, beginning with adding a column. Suppose you want to track each employee’s birth date with the employees table: ALTER TABLE employees ADD COLUMN birthdate DATE; The new column is placed at the last position of the table. However, you can also control the positioning of a new column by using an appropriate keyword, including FIRST, AFTER, and LAST. For example, you could place the birthdate column directly after the lastname column, like so: ALTER TABLE employees ADD COLUMN birthdate DATE AFTER lastname; Whoops, you forgot the NOT NULL clause! You can modify the new column: ALTER TABLE employees CHANGE birthdate birthdate DATE NOT NULL; Finally, after all that, you decide that it isn’t necessary to track the employees’ birth dates. Go ahead and delete the column: ALTER TABLE employees DROP birthdate; Gilmore_862-8C28.fm Page 724 Friday, February 15, 2008 7:17 AM CHAPTER 28 ■ MYSQL STORAGE ENGINES AND DATATYPES 725 The INFORMATION_SCHEMA Earlier in this chapter you learned that the SHOW command is used to learn more about the databases found in the server, tables found in a database, and columns comprising a table. In fact, SHOW is used for learning quite a bit about the server’s configuration, including user privileges, supported table engines, executing processes, and more. The problem is, SHOW isn’t a standard database feature; it’s something entirely native to MySQL. Furthermore, it isn’t particularly powerful. For instance, it’s not possible to use the command to learn about a table’s engine type. Nor could one, say, easily find out which columns in a set of given tables are of type VARCHAR. The introduction of the INFORMATION_SCHEMA in version 5.0.2 solves such problems. Supported by the SQL standard, INFORMATION_SCHEMA offers a solution for using typical SELECT queries to learn more about databases and various server settings. Consisting of 27 tables, it’s possible to learn about practically every aspect of your installation. The table names and brief descriptions are listed here: • CHARACTER_SETS: Stores information about the available character sets. • COLLATIONS: Stores information about character set collations. • COLLATION_CHARACTER_SET_APPLICABILITY: A subset of the INFORMATION_SCHEMA.COLLATIONS table, matches character sets to each respective collation. • COLUMNS: Stores information about table columns, such as a column’s name, datatype, and whether it’s nullable. • COLUMN_PRIVILEGES: Stores information about column privileges. Keep in mind that this information is actually retrieved from the mysql.columns_priv table; however, retrieving it from this table offers the opportunity for additional unifor- mity when querying database properties. See Chapter 29 for more information about this topic. • ENGINES: Stores information about available storage engines. • EVENTS: Stores information about scheduled events. Scheduled events are out of the scope of this book; consult the MySQL documentation for more information. • FILES: Stores information about NDB disk data tables. NDB is a storage engine that is out of the scope of this book; consult the MySQL documentation for more information. Gilmore_862-8C28.fm Page 725 Friday, February 15, 2008 7:17 AM 726 CHAPTER 28 ■ MYSQL STORAGE ENGINES AND DATATYPES • GLOBAL_STATUS: Stores information about server status variables. • GLOBAL_VARIABLES: Stores information about server settings. • KEY_COLUMN_USAGE: Stores information about key column constraints. • PARTITIONS: Stores information about table partitions. • PLUGINS: Stores information about plug-ins, a feature new to MySQL 5.1 and out of the scope of this book. Consult the MySQL documentation for more information. • PROCESSLIST: Stores information about currently running threads. • REFERENTIAL_CONSTRAINTS: Stores information about foreign keys. • ROUTINES: Stores information about stored procedures and functions. See Chapter 32 for more about this topic. • SCHEMATA: Stores information about the databases located on the server, such as the database name and default character set. • SCHEMA_PRIVILEGES: Stores information about database privileges. Keep in mind that this information is actually retrieved from the mysql.db table; however, retrieving it from this table offers the opportunity for additional uniformity when querying database properties. See Chapter 29 for more information about this topic. • SESSION_STATUS: Stores information about the current session. • SESSION_VARIABLES: Stores information about the current session’s configuration. • STATISTICS: Stores information about each table index, such as the column name, whether it’s nullable, and whether each row must be unique. • TABLES: Stores information about each table, such as the name, engine, creation time, and average row length. • TABLE_CONSTRAINTS: Stores information about table constraints, such as whether it includes UNIQUE and PRIMARY KEY columns. Gilmore_862-8C28.fm Page 726 Friday, February 15, 2008 7:17 AM CHAPTER 28 ■ MYSQL STORAGE ENGINES AND DATATYPES 727 • TABLE_PRIVILEGES: Stores information about table privileges. Keep in mind that this information is actually retrieved from the mysql.tables_priv table; however, retrieving it from this table offers the opportunity for additional uniformity when querying database properties. See Chapter 29 for more information about this topic. • TRIGGERS: Stores information about each trigger, such as whether it fires according to an insertion, deletion, or modification. See Chapter 33 for more information about this topic. Note that this table wasn’t added to the INFORMATION_SCHEMA until version 5.0.10. • USER_PRIVILEGES: Stores information about global privileges. Keep in mind that this information is actually retrieved from the mysql.user table; however, retrieving it from this table offers the opportunity for additional uniformity when querying database properties. See Chapter 29 for more information about this topic. • VIEWS: Stores information about each view, such as its definition and whether it’s updatable. See Chapter 34 for more information about this topic. To retrieve a list of all table names and corresponding engine types found in the databases residing on the server except for those found in the mysql database, execute the following: mysql>USE INFORMATION_SCHEMA; mysql>SELECT table_name FROM tables WHERE table_schema != 'mysql'; + + + | table_name | engine | + + + | authentication_dynamic | MyISAM | | authentication_static | MyISAM | | products | InnoDB | | selectallproducts | NULL | | users | MEMORY | + + + 5 rows in set (0.09 sec) Gilmore_862-8C28.fm Page 727 Friday, February 15, 2008 7:17 AM 728 CHAPTER 28 ■ MYSQL STORAGE ENGINES AND DATATYPES To select the table names and column names found in the corporate database having a datatype of VARCHAR, execute the following command: mysql>select table_name, column_name from columns WHERE -> data_type='varchar' and table_schema='corporate'; + + + | table_name | column_name | + + + | authentication_dynamic | username | | authentication_dynamic | pswd | | products | name | | selectallproducts | name | | users | username | | users | pswd | + + + 6 rows in set (0.02 sec) As you can see even from these brief examples, using SELECT queries to retrieve this information is infinitely more flexible than using SHOW. Remember, however, that INFORMATION_SCHEMA is only available as of version 5.0. Also, it’s unlikely the SHOW command will disappear anytime soon. Therefore, if you’re just looking for a quick summary of, say, databases found on the server, you’ll certainly save a few keystrokes by continuing to use SHOW. Summary In this chapter, you learned about the many ingredients that go into MySQL table design. The chapter kicked off the discussion with a survey of MySQL’s storage engines, discussing the purpose and advantages of each. This discussion was followed by an introduction to MySQL’s supported datatypes, offering information about the name, purpose, and range of each. It then examined many of the most commonly used attributes, which serve to further tweak column behavior. The chapter then moved on to a short tutorial on basic MySQL administration commands, demonstrating how databases and tables are listed, created, deleted, perused, and altered. Finally, you were introduced to the new INFORMATION_SCHEMA feature found in MySQL 5.0.2 and newer. Gilmore_862-8C28.fm Page 728 Friday, February 15, 2008 7:17 AM CHAPTER 28 ■ MYSQL STORAGE ENGINES AND DATATYPES 729 The next chapter dives into another key MySQL feature: security. You’ll learn all about MySQL’s powerful privilege tables, as well as learn more about how to secure the MySQL server daemon and create secure MySQL connections using SSL. Gilmore_862-8C28.fm Page 729 Friday, February 15, 2008 7:17 AM Gilmore_862-8C28.fm Page 730 Friday, February 15, 2008 7:17 AM [...]... deleted, and renamed As you’ll soon learn, it’s possible to create and effectively delete users by using the GRANT and REVOKE commands However, the fact that you can use these commands for such purposes may seem a tad nonintuitive given the command names, which imply the idea of granting privileges to and revoking privileges from existing users Therefore, in version 5, two new commands were added to MySQL s... old_user TO new_user [old_user TO new_user] An example follows: mysql> RENAME USER jason@localhost TO jasongilmore@localhost; Query OK, 0 rows affected (0.02 sec) 751 Gilmore _86 2-8C29.fm Page 752 Friday, February 15, 20 08 7:22 AM 752 CHAPTER 29 ■ SECURING M YSQL As the command prototype indicates, it’s also possible to simultaneously rename more than one user The GRANT and REVOKE Commands The GRANT and. .. TEMPORARY TABLE command CREATE USER Affects ability to create, drop, rename, and revoke privileges from users CREATE VIEW Affects the use of the CREATE VIEW command DELETE Affects the use of the DELETE command DROP Affects the use of the DROP TABLE command EXECUTE Affects the user’s ability to run stored procedures EVENT Affects the ability to execute events (as of MySQL 5.1.6) Gilmore _86 2-8C29.fm Page 753... REVOKE commands are used to manage access privileges As previously stated, you can also use them to create and delete users, although, as of MySQL 5.0.2, you can more easily accomplish this with the CREATE USER and DROP USER commands The GRANT and REVOKE commands offer a great deal of granular control over who can work with practically every conceivable aspect of the server and its contents, from who... database_name.*} FROM user_name [, user_name ] As with GRANT, the best way to understand use of this command is through some examples The following examples demonstrate how to revoke permissions from, and even delete, existing users ■Note If the GRANT and REVOKE syntax is not to your liking, and you’d prefer a somewhat more wizard-like means for managing permissions, check out the Perl script mysql_ setpermission... although it offers a very easy -to- use interface, it does not offer all the features that GRANT and REVOKE have to offer This script is located in the MYSQL- INSTALL-DIR/bin directory, and assumes that Perl and the DBI and DBD: :MySQL modules have been installed This script is bundled only for the Linux/Unix versions of MySQL Revoking Previously Assigned Permissions Sometimes you need to remove one or more previously... request If no, deny the request and end the control procedure By now you should be beginning to understand the generalities surrounding MySQL s access-control mechanism However, the picture isn’t complete until you’re familiar with the technical underpinnings of this process This matter is introduced next 737 Gilmore _86 2-8C29.fm Page 7 38 Friday, February 15, 20 08 7:22 AM 7 38 CHAPTER 29 ■ SECURING M YSQL... with a corresponding password to alleviate potential security issues Passwords are stored in a one-way hashed format, meaning that they cannot be converted back to their plain-text format Furthermore, as of version 4.1, the number of bytes required to store a password increased from 16 bytes to 41 bytes Therefore, if you’re importing data from a pre-4.1 version, and you want to take advantage of the added... existing databases and tables • Reload_priv: Determines whether the user can execute various commands specific to flushing and reloading of various internal caches used by MySQL, including logs, privileges, hosts, queries, and tables Gilmore _86 2-8C29.fm Page 743 Friday, February 15, 20 08 7:22 AM C HAPTE R 29 ■ SEC URING MY SQL • Shutdown_priv: Determines whether the user can shut down the MySQL server You... SHOW DATABASES command SHOW VIEW Affects the use of the SHOW CREATE VIEW command SHUTDOWN Affects the use of the SHUTDOWN command SUPER Affects the use of administrator-level commands such as CHANGE MASTER, KILL thread, mysqladmin debug, PURGE MASTER LOGS, and SET GLOBAL TRIGGER Affects the ability to execute triggers (as of MySQL 5.1.6) UPDATE Affects the use of the UPDATE command USAGE Connection . found in MySQL 5.0.2 and newer. Gilmore _86 2-8C 28. fm Page 7 28 Friday, February 15, 20 08 7:17 AM CHAPTER 28 ■ MYSQL STORAGE ENGINES AND DATATYPES 729 The next chapter dives into another key MySQL. rows in set (0.09 sec) Gilmore _86 2-8C 28. fm Page 727 Friday, February 15, 20 08 7:17 AM 7 28 CHAPTER 28 ■ MYSQL STORAGE ENGINES AND DATATYPES To select the table names and column names found in the. includes UNIQUE and PRIMARY KEY columns. Gilmore _86 2-8C 28. fm Page 726 Friday, February 15, 20 08 7:17 AM CHAPTER 28 ■ MYSQL STORAGE ENGINES AND DATATYPES 727 • TABLE_PRIVILEGES: Stores information

Ngày đăng: 09/08/2014, 14:21

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