This post is aimed at Information Technology (“IT”) students in DBE and IEB schools. When learning the Structured Query Language (“SQL”) portion of the syllabus, ideally you want to be using Microsoft SQL Server, or MySQL. If you are using Microsoft Access, you should specifically be aware of the difference in data types and SQL syntax.

1. Basic syntax

Creating a table in MySQL is as easy as can be! The syntax is as follows:

MySQL

CREATE TABLE tblMyTableName
(
	fieldName1 datatype,
	fieldName2 datatype,
	fieldName3 datatype,
	fieldName4 datatype
);

Access

CREATE TABLE tblMyTableName
(
	fieldName1 datatype,
	fieldName2 datatype,
	fieldName3 datatype,
	fieldName4 datatype
);

Below is an example of a complete CREATE TABLE statement:

MySQL

CREATE TABLE tblCustomers
(
	customerID INT(255),
	customerSurname VARCHAR(100),
	customerCellnumber CHAR(10),
	customerCreditApproved BOOLEAN
);

Access

CREATE TABLE tblCustomers
(
	customerID INT,
	customerSurname TEXT(100),
	customerCellnumber TEXT(10),
	customerCreditApproved YesNo
);

Read: The Primary Key in a database table for more about the PK.

2. Additional syntax

To add a Primary Key (“PK”), the PK field must be declared NOT NULL and the PRIMARY KEY constraint must be added at the end of the statement:

MySQL

CREATE TABLE tblCustomers
(
	customerID INT NOT NULL,
	customerSurname VARCHAR(100) NOT NULL,
	customerCellnumber CHAR(10),
	customerCreditApproved BOOLEAN,
	PRIMARY KEY (customerID)
);

Access

CREATE TABLE tblCustomers
(
	customerID INT NOT NULL,
	customerSurname TEXT(100) NOT NULL,
	customerCellnumber TEXT(10),
	customerCreditApproved YesNo,
	PRIMARY KEY (customerID)
);

3. Data types

The most common data types:

  • CHAR(size)
  • VARCHAR(size)
  • INT(size)
  • BIGINT(size)
  • BOOLEAN
  • DATE
  • TIMESTAMP

There are small (and some not-so-small) differences between data types in MySQL, SQL Server and Microsoft Access. Below is a selection of data types and their approximate equivalents:

MySQLSQLMicrosoft Access
CHAR(n)char(n)TEXT(n)
LONGTEXTtextLONGTEXT
INT(size)intINT
BIGINT(size)bigintLong Integer
TIMESTAMP(fsp)timestampDate/Time with Default: NOW()
BOOLEANbitYesNo [5]
MySQL/SQL/Microsoft Access data type comparison

For a full list of data types, consult https://www.w3schools.com/sql/sql_datatypes.asp


References:

  1. o365devx (no date) Create and delete tables and indexes using Access SQLMicrosoft Learn. Available at: https://learn.microsoft.com/en-us/office/vba/access/concepts/structured-query-language/create-and-delete-tables-and-indexes-using-access-sql (Accessed: 21 April 2024).
  2. W3schools. (2023). SQL CREATE TABLE Statement. Available at: https://www.w3schools.com/sql/sql_create_table.asp (Accessed: 13 February 2023).
  3. W3schools. (2023). SQL PRIMARY KEY Constraint. Available at: https://www.w3schools.com/sql/sql_primarykey.asp (Accessed: 16 February 2023).
  4. W3schools. (2023). SQL Data Types for MySQL, SQL Server, and MS Access. Available at: https://www.w3schools.com/sql/sql_datatypes.asp (Accessed: 14 February 2023).
  5. Raskew (2008) ‘Create Table’ and Yes/No FieldAccess World Forums. Access World Forums. Available at: https://www.access-programmers.co.uk/forums/threads/create-table-and-yes-no-field.152693/#post-723678 (Accessed: 21 April 2024).

By MisterFoxOnline

Mister Fox AKA @MisterFoxOnline is an ICT, IT and CAT Teacher who has just finished training as a Young Engineers instructor. He has a passion for technology and loves to find solutions to problems using the skills he has learned in the course of his IT career.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.