The Tables of a Database |
|
Tables are the foundation of organizing lists of items. This concept is valid regardless of the type of list. As a database application is primarily a list of things, the SQL uses the same approach at organizing information. Before creating a table, you must specify what database it would belong to.
The statement used to create a new table uses the following formula CREATE TABLE TableName The CREATE and TABLE keywords must be used to let SQL know that you want to create a table. The TableName factor specifies the name of the new table. The TableName can use the rules and suggestions we reviewed for the database objects. After specifying the name of the table, you must list the columns of the table. The list of columns starts with an opening parenthesis "(". The list ends with a closing parenthesis ")". Each column must be separated from the next with a comma, except for the last column. You can include all columns on the same line if possible as follows: CREATE TABLE Country(Column1, Column2, Column3) There are two primary pieces of information you must specify for each column: its name and its type. Therefore, the syntax of creating a column is: ColumnName DataType Options To programmatically create a table, you can follow the same formula rules of creating a table using SQL code. Once the statement is ready, you can pass it to a MySqlCommand object. To execute the statement, you can call the SqlCommand.ExecuteNonQuery() method. |
The Name of a Table |
While creating a table, you must name it. If you are creating a table using SQL code, you must name your table using the above formula of CREATE TABLE TableName. The name of a table:
Besides these rules, you can make up yours. In our lessons, the name of a table
As implied above, to create a table, you must include at least one column. In the next lesson, we will have more details on how to create and manage columns. |
Practical Learning: Creating Tables |
|
Table Maintenance |
Renaming a Table |
If you have a table whose name is not appropriate, you can change it. Before renaming a table, make sure this is what you want to do and make sure you can take care of it in your code. To rename a table in MySQL, use the following formula: RENAME TABLE ExistingName TO NewName; When using this formula, replace the ExistingName factor with the name of the table you want to rename. Replace the NewName factor with the name you want the table to have after renaming it. Here is an example: Enter password: ******** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 to server version: 4.1.12a-nt Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> USE CountriesStats; Database changed mysql> RENAME TABLE Countries TO Pais; Query OK, 0 rows affected (0.59 sec) mysql> To rename a table in a C# code, pass the sp_rename code as string to a MySqlCommand object and call the SqlCommand.ExecuteNonQuery() method. |
Practical Learning: Renaming a Table |
|
private: System::Void btnRenameTable_Click(System::Object * sender, System::EventArgs * e) { String *strExistingName = txtExistingTblName->Text; String *strNewName = txtNewTblName->Text; if( strExistingName->Equals(S"") ) { MessageBox::Show(S"To rename a table, you must provide a " S"valid name for an existing table"); return; } if( strNewName->Equals(S"") ) { MessageBox::Show(S"To rename a table, you must provide the name " S"that will replace the existing name of the table"); return; } String *strConnection = String::Concat(S"RENAME TABLE ", strExistingName, S" TO ", strNewName, S";"); MySqlConnection *conDatabase = new MySqlConnection(S"Data Source=localhost;Database='CountriesStats';Persist Security Info=yes;"); MySqlCommand *cmdDatabase = new MySqlCommand(strConnection, conDatabase); conDatabase->Open(); cmdDatabase->ExecuteNonQuery(); conDatabase->Close(); txtExistingTblName->Text = S""; txtNewTblName->Text = S""; txtExistingTblName->Focus(); } |
Removing a Table |
If you have a table you don't need anymore in your database, you can remove it. Before performing this operation, you should make sure you are familiar with the role of the table. In fact, you should try to delete only a table you know you have created and you don't need anymore. If you work in a team environment and you find a table that may have been created by someone else, you should enquire before deleting it. The code used to delete a table uses the following formula: DROP TABLE TableName The DROP TABLE expression is required and it is followed by the name of the table as TableName. Here is an example: DROP TABLE FriendsOfMine; If you are working from C++ code, create a DROP TABLE TableName; expression and pass it to a MySqlCommand object before calling the SqlCommand::ExecuteNonQuery() method. It is extremely important to know that, when working with the DROP TABLE TableName statement, you would not receive any warning. If you are working in a Windows Forms Application, you should create your own warning in a message box to make sure that the user really wants to delete the table. |
Practical Learning: Finalizing the Application |
|
|
||
Previous | Copyright © 2005-2016, FunctionX | Next |
|