SQL to create table is adding spaces

Status
Not open for further replies.
I have the following SQL in a VB script to create a new table in a Microsoft Access 2010 Database:

Code:
CREATE TABLE ElectionFile( Record_Type CHAR(50) WITH COMPRESSION NULL, 
Employer_ID CHAR(50) WITH COMPRESSION NULL, 
Member_Number CHAR(50) WITH COMPRESSION NULL, 
Plan_Year_Effective_Date CHAR(50) WITH COMPRESSION NULL, 
Account_Type INT, 
Election_Effective_Date CHAR(50) WITH COMPRESSION NULL, 
Annual_Deduction_Amount CHAR(50) WITH COMPRESSION NULL, 
Annual_Contribution_Amount CHAR(50) WITH COMPRESSION NULL, 
Scheduled_Deduction_Amount CHAR(50) WITH COMPRESSION NULL, 
Scheduled_Contribution_Amount CHAR(50) WITH COMPRESSION NULL, 
Debit_Card_Selected CHAR(50) WITH COMPRESSION NULL, 
Auto_Pay_Dental CHAR(50) WITH COMPRESSION NULL, 
Auto_Pay_Healthcare CHAR(50) WITH COMPRESSION NULL, 
Auto_Pay_Vision CHAR(50) WITH COMPRESSION NULL, 
Auto_Pay_RX CHAR(50) WITH COMPRESSION NULL, 
Election_Expiration CHAR(50) WITH COMPRESSION NULL, 
FSA_First CHAR(50) WITH COMPRESSION NULL, 
CoPayCode_Medical CHAR(50) WITH COMPRESSION NULL, 
CopayCode_Dental CHAR(50) WITH COMPRESSION NULL, 
CopayCode_Vision CHAR(50) WITH COMPRESSION NULL, 
CopayCode_RX CHAR(50) WITH COMPRESSION NULL, 
CSA_Number CHAR(50) WITH COMPRESSION NULL)"

The Length for each of them is arbitrary. I just want to give each of the fields enough space that I never have to deal with one being too small. The SQL works correctly and the table is created in the Access Database, but when I try to put anything in one of the fields, it autofills the rest of the field with spaces. So if I put in say
Code:
"Record"
in the first field it adds 44 spaces so the field now contains
Code:
"Record                                            "
. Needless to say, this is a problem when I attempt to export the table, the file has thousands of unnessesary spaces. Does anyone know a way to solve this issue?
 

Hattori HANZo

Ars Scholae Palatinae
1,205
Yes, you should use proper types. One of the key advantages of a relational database over a simple text file is that you can impose structure, rigidly and on a low level. If you saves numbers and money as varchar, that is plain terrible.

Access has, iirc, a dedicated money data type. If you don't like that use at least long and track cents.
Having IDs and saving them as text is also crazy for several reasons. Use an autogenerated key, which will be int or long internally, iirc. With proper datatypes for dates, you can do date calculations. And you can force consistency, to prevent a date column that contains 14.11.2012, 2012-11-14 and 11/14/12. If you do not enforce that, you can end up with data that are worthless later. If you take care of the formatting yourself, you are doing work that flew into the creation of the database yourself and by experience you'll do worse.

Besides that, you should define at least a primary key. That is a value in one column (or a combination of multiple ones) that identifies each dataset (row) unambiguously. If you refer to other tables' primary keys with e.g. MemberNumber and EmployerID, make them foreign keys. This way they get tied together logically and you can specify what happens or may not happen when things interconnected logically get modified or deleted. It looks like this is about financial data. You maybe want to make really sure that if you are entering credit card numbers that the people and employers referenced cannot change by some accident, leaving you with customers and credit cards numbers where you can't be sure of matching anymore.
You might also want to define some columns as necessary with the NOT NULL keyword, meaning that there has to be a value put in for the database to accept the record.


If you have no idea of databases, get help, educate yourself and only as a last resort try to "just make it work somehow".
 

CaptainBlye

Seniorius Lurkius
9
Hattori HANZo":3oa29ybj said:
Yes, you should use proper types. One of the key advantages of a relational database over a simple text file is that you can impose structure, rigidly and on a low level. If you saves numbers and money as varchar, that is plain terrible.

Access has, iirc, a dedicated money data type. If you don't like that use at least long and track cents.
Having IDs and saving them as text is also crazy for several reasons. Use an autogenerated key, which will be int or long internally, iirc. With proper datatypes for dates, you can do date calculations. And you can force consistency, to prevent a date column that contains 14.11.2012, 2012-11-14 and 11/14/12. If you do not enforce that, you can end up with data that are worthless later. If you take care of the formatting yourself, you are doing work that flew into the creation of the database yourself and by experience you'll do worse.

Besides that, you should define at least a primary key. That is a value in one column (or a combination of multiple ones) that identifies each dataset (row) unambiguously. If you refer to other tables' primary keys with e.g. MemberNumber and EmployerID, make them foreign keys. This way they get tied together logically and you can specify what happens or may not happen when things interconnected logically get modified or deleted. It looks like this is about financial data. You maybe want to make really sure that if you are entering credit card numbers that the people and employers referenced cannot change by some accident, leaving you with customers and credit cards numbers where you can't be sure of matching anymore.
You might also want to define some columns as necessary with the NOT NULL keyword, meaning that there has to be a value put in for the database to accept the record.


If you have no idea of databases, get help, educate yourself and only as a last resort try to "just make it work somehow".

Also something to keep in mind is that you can't run an update or delete query in MS ACCESS if it is based on a table with no primary key.
 
Status
Not open for further replies.