SECTION 5: Modifying And Controlling Tables
Changing Tables
In many cases, SQL database tables that are newly created need to be tweaked a little. For instance, when a programmer is working for someone else, a client may come to them after the database table has already been created and request to keep track of other data items. It means that the programmers will have to return to the drawing board. When one is building databases for their use, some deficiencies in their structures may be quite apparent after the structure is created. For instance, one can get some proposals from different countries and will be required to add county columns. It is the time that a programmer needs to make some modifications to their table.
Simple Modifications to SQL Table Structures
SQL databases are used in storing relationships and data within tables. Changes within the tables are likely to occur from time to time. The changes cater to new or missed out requirements. All SQL tables are structured in ways that they can be modified. Modification of the SQL table structures depends on their complexity; this means that some may be easier than others. Below is a precise analysis of the change of SQL table structures.
All the SQL databases make use of the containers referred to as tables as storage of data and the relationships between these data. There are several rows existing in each table. They are also referred to as tuples where all related data is stored. Each row in an SQL database table usually comprises of columns. These columns are also referred to as fields. Each column stores original data contents. Over time the structure of the table is likely to change. In the case of a business, a supplier ID may need to be increased due to business expansion. Therefore, the number of suppliers may need to improve, and this calls for some changes to the SQL database table structure.
Some of the standard generic modifications that are made to tables include the following.
1-Changes to the columns in the table- this can involve changing, adding or removing column types or sizes.
2-Changes to constraints and the rules applying to the tables or the relationships among the charts.
3-Changing the values of data that are stored in the tables. This can involve, for instance, adding new records, deleting new files or changing the values of documents that are already existing. However, this does not include the modification of the SQL database table structure.
Numerous SQL implementations provide two options that can be used in making modifications within the SQL table. One of the alternatives requires one to use SQL statements. The second option requires one to use GUI tools that are provided by SQL implementation.
Below are some of the steps that one can follow to add new rows and enhance change accommodation successfully. A programmer should open their initial table and follow each step appropriately.
In the window showing table-creation, one should right-click within a small, colored square on the left of the City field. It will help in selecting rows. Then choose Insert Rows from a menu that will pop up. A blank row will appear above the cursor position and will push down every row that exists.
Second, one is required to enter the fields that they want to be added to the existing table. For instance, one can add an Address3 area above the Country and County fields.
The final step is making sure after the modifications are done, the table is saved. This is a crucial step before closing the table. It helps in ensuring that all the changes that have been made are protected as it was required.
The SQL ALTER TABLE statement is used when adding, deleting or modifying existing tables in SQL programming. It is also used in dropping and adding some constraints to SQL existing tables.
The ALTER TABLE- ADD Column is used when adding columns to existing tables. The following syntax is used in the process: it is one of the most straightforward modifications that can be done on current tables. The operation is considered to be free from potential risks. This is because it is not dependent on other elements in the existing database.
ALTER TABLE table_ name
ADD column_name datatype;
When adding an Email column to a “customers” table, the following SQL is used.
ALTER TABLE Customers
ADD Email varchar (255)
ALTER TABLE- DROP COLUMN is used when deleting columns present in a table. There are some databases that do not allow for columns to be removed. For those that would enable, the following syntax is used;
ALTER TABLE table_name
DROP COLUMN column_name;
When deleting an Email column from an existing “customers” table, the following SQL is used.
ALTER TABLE Customers
DROP COLUMN Email;
ALTER TABLE- ALTER/ MODIFY COLUMN is used when making adjustments to the data type existing in a column. The SQL Server/ MS Access is the syntax used in such cases. They are applied in the following manner.
ALTER TABLE table_name
ALTER COLUMN column_ name datatype
Creating some changes in the field structure requires adequate preparation. This is because in cases where the specific field needs some shrinking, essential precautions must be followed. These precautions help in ensuring that data that is already existing in the field is not tampered with as a result of the changes. When numeric field formats are changed, it is possible that inaccuracy or loss of precision can be experienced. In most cases, the SQL systems send some warning if any tampering occurs and the change may not occur if it is affecting the type of data.
Modifying and Removing Constraints
In some databases, there are constraints or rules that are mainly defined for the tables. Therefore, this means that to make successful modifications, one needs to be adequately prepared and make some considerable plans and efforts. This is because in most cases, the adjustments being made are likely to affect the other tables as well.
Dropping columns from existing tables can have some adverse effects on the integrity of the database. This mainly happens when the field being cut is a foreign or primary key of reference by the other tables. The process of dropping the area may be quite challenging. Overcoming it needs a programmer to be adequately prepared and follow all the necessary steps. One should also consider being well conversant with the SQL programming language to ensure they are capable of tackling every task required.