Mysql create table
Considering MySQL? Read on … Part I: a powerful combination creates a strong architecture
MySQL is a small, fast, and efficient database. This article discusses leveraging MySQL as the database with BEA WebLogic Server 8.1.
We will look at using MySQL as the database engine where the application is developed using BEA WebLogic Workshop 8.1 and deployed to BEA WebLogic Server 8.1. Using an archetypical Java 2 Enterprise Edition (J2EE) architecture, I will evaluate the impact of using MySQL from various aspects such as choosing the correct version of MySQL, setting up the server, and making development adjustments. The impact on development and deployment of bread-and-butter technologies such as Enterprise JavaBeans (EJBs), Java DataBase Connectivity (JDBC), Java Message Service (JMS), and the Java Transaction API (JTA) are evaluated. Various pitfalls are uncovered, logically approached, and methodically solved. The information presented here will not only enhance your understanding of the tools and technologies utilized, but also save you countless hours. Even readers who employ different database technologies will find the information and material practical and useful.
Introduction
The development tool of choice is BEA WebLogic Workshop 8.1. I'll describe an archetypical J2EE architecture and explore the impact of the decision to use MySQL with J2EE technologies such as JDBC and JMS. Part 2 of this series will evaluate the impact of EJBs, the core component model of J2EE as well as the JTA. The article describes configuration and development changes, adjustments, and modifications.
Architecture
The sample application has a standard J2EE architecture consisting of a database tier, an application tier, and an interface tier as depicted in the Figure 1. The database, or back-end, tier consists of MySQL as the Relational DataBase Management System (RDBMS). The application tier is BEA WebLogic Server 8.1. The application server includes a JMS server and an EJB container. The JMS server hosts destinations such as a queue utilized by the sample application for asynchronous processing. The sample application consists of many EJBs that leverage container managed transaction demarcation (CMTD). Tables in the database are mapped to entity EJBs with container-managed persistence (CMP). The entity EJBs are fronted with a session EJB facade. There is a Message-Driven Bean (MDB) that listens to a queue and processes messages. Other applications, such as command-line applications, rich graphical user interface (GUI) applications, or Web applications, leverage the EJB components through the facade. They exchange data with the session facade using value objects (also known as data transfer objects). The value objects corresponding directly to the entity beans are automatically generated by WebLogic Workshop.
[FIGURE 1 OMITTED]
Choosing the "Right" MySQL
MySQL comes in various shapes and forms and has at least four different incarnations: MySQL Standard, MySQL Max, MySQL Pro, and MySQL Classic. MySQL Standard and MySQL Pro are identical except for the license. MySQL Standard is licensed under the GNU Public License (GPL), whereas MySQL Pro is a commercially licensed version of MySQL Standard. MySQL Max includes cuttingedge and experimental features and is not recommended for production use. MySQL Classic is available only under commercial license and excludes important features. For this application, support is required for critical features such as transactions and referential-integrity (i.e., foreign key) constraints. The choice of MySQL among its various incarnations is either MySQL Standard or MySQL Pro. Note: Any further reference in this article to MySQL implies usage of MySQL Standard. (More information about the different types of MySQL is available at www.mysql.com/products/ mysql/index.html.)
MySQL presents a number of choices for table types, each offering features that have their own pros and cons. The table types are ISAM, MyISAM, HEAP, MERGE, BDB, and InnoDB. Many factors determine the choice of a table type. These factors include, but are not limited to, performance, transactions, rowlevel locking, and crash recovery. However, the crucial features for the sample application are transactions and referentialintegrity constraints.
The InnoDB table type is the only one that meets the criteria. There are at least two ways to specify an InnoDB table type. One is to start the MySQL database server using--default-tabletype= InnoDB. A table created with this option is of InnoDB type. (Note: If the default table type of InnoDB is not specified, the default table type is MyISAM.) The other way to specify InnoDB table type is to explicitly mention table type in the Data Description Language (DDL) of the create table script. (More information about MySQL table types is available at www.mysql.com/ doc/en/Table_types.html.)
A specific feature about MySQL and foreign constraints is that before the constraint can be created, an index on the column must already exist. For example, consider a one-to-one relationship between the tables Person and Buyer. Buyer has a foreign key to Person. Buyer has a column named Person_Id that is a foreign key to a column named Person_Id in the Person table. But before the foreign key can be established, an index must be created on column Person_Id in the Buyer table. Otherwise, the creation of a foreign key constraint fails. Refer to the sem.sql DDL file in the source code example (the source code is online at www.sys-con.com/ weblogic/sourcec.cfm).
The default database privileges are different for different operating systems. For example, the default privileges on Windows give all local users full privileges without specifying a username or password. Therefore, an important validation is being able to connect to the MySQL database server engine. One way to connect is by using the client program that comes with MySQL. Another way is by using JDBC and a program like DbVisualizer. (See the section "Verify Connectivity using DbVisualizer". Information about default privileges is available at www.mysql.com/doc/en/Default_ privileges.html.)
The default case sensitivity of table names is based upon the operating system. For example, on Windows the table names are case insensitive. This is especially important if development is done on one platform but deployment is on another. To avoid such problems, one recommendation is to start the server by setting lower_case_table_names=1 variable. (More information on this variable is available at www.mysql.com/doc/en/Name_case_sensitivity. html.)
Logging is enabled in MySQL by starting the MySQL database server using the--log option. Starting the MySQL server with logging enabled is highly recommended. Logging enables monitoring of Structured Query Language (SQL) statements executed by MySQL and can be a crucial tool in debugging and trouble-shooting issues. (More information on the MySQL log file is available at www.mysql.com/doc/en/Query_log.html.)
The various options, such as default table type and logging, can be specified in a couple of ways. One is to pass the options in as command-line arguments to mysqld, the MySQL database engine executable. The other option is to specify these options in either a my.ini file or my.cnf option file. (More information on option files, including where to place them, is available at www.mysql.com/ doc/en/Option_files.html.)
Before we proceed further, complete the following steps:
1. Download MySQL: www.mysql.com/downloads/mysql-4.0.html.
2. Install MySQL with these instructions: www.mysql.com/doc/en/Installing.html.
3. Specify additional options such as logging and table type.
4. Start MySQL (e.g., using mysqld--console).
5. Verify connectivity to the server using mysql (e.g., mysql -p -u root). Note: Just press enter when prompted for a password if local on Windows)
6. Create a database named semdb using create database semdb.
7. Use the database semdb using semdb.
8. Load schema using mysql command source and specifying the fully qualified path to sem.sql (e.g., source c:/MySQL_WLS/db/-sem.sql). Note: Use forward slashes even on Windows.
9. Using the grant.sql file available in the source code, grant privileges using source fully qualified path to grant.sql. Note: Use forward slashes even on Windows.
Downloading the JDBC Driver
JDBC requires a JDBC driver to connect to the database server. Connector/J is the JDBC driver for MySQL. Download and install Connector/J from www.mysql.com/downloads/api-jdbcstable. html. The download contains a mysql-connector-java-3.0.9-stable-bin.jar file that contains the JDBC drivers.
The following section on verifying the connectivity to MySQL server is optional, but highly recommended.
Verify Connectivity Using DbVisualizer