5 min read

SQL: Create Database, Table Operations, And More

SQL: Create Database, Table Operations, And More

In programming, Structured Query Language (SQL) is used for managing data in a relational database management system (RDSMS). SQL remains widely popular due to its litany of advantages such as faster query processing and an interactive language that is easy for even novice coders to learn and understand.

In SQL, tables hold and manage all information in a database and keep it logically organized in a row and column format similar to a spreadsheet. This makes information easy to reference and manipulate. A SQL database prevents the possibility of human error by setting certain conditions and requirements for entering information.

While SQL may sound complicated, the fundamentals behind creating and manipulating databases and tables are fairly straightforward. Once you have the foundations down, it does get more complex, and there is a lot of theory and debate in the programming world about the best techniques for creating and managing SQL databases. However, most people can master the basics with a little practice and research.

Below, we'll provide a very brief introduction to SQL.

SQL: Create Database

In SQL, a database is a collection of tables that store a specific set of data. For example, a programmer working for a hospital might create a SQL database storing all information for patients in a particular department. Creating an SQL database is actually fairly simple as you just need to know basic syntax to get started.

As an example, let's imagine you're helping to build a website that has information about dog breeds. You have a specific database meant to store information about toy breeds. To create this database, all you need to do is write CREATE DATABASE, enter the name, and add a semicolon.

For this example, you would start with something like: “CREATE DATABASE toys;”

This will create a subdirectory on your file system's data directory called “toys.” This does not create any data or tables yet, but does set up a place to hold this information later on.

On a very basic level, this is all you need to do to create a database, but sometimes you may want to enter some additional information to set certain conditions. For example, beneath your first line, you could add “CHARACTER SET latinl” to indicate the default characters used in your tables will be Latin letters.

In that case, you would write:

CREATE DATABASE toys

CHARACTER SET latinl;

SQL: Adding Tables

Congratulations, you have successfully created your first database. However, as is, it remains empty. It is time to add your tables. A database is a collection of tables and each table is designed to hold different types of entries. Before adding data, you need to create your tables.

First, it is essential to have a primary key. This is basically a unique identifier for each row, sort of like the row's address. The simplest way to do this is setting the row number to auto increment. (That is, the value increases every time there is a new row, 1, 2, 3, 4, and so on.)

Returning to our example, you could start with:

dog_id INT AUTO_INCREMENT PRIMARY KEY.

What does this mean? Essentially, this means every dog added would have its own unique ID, beginning with one and going up each time. This ensures – when referencing information about a given breed – you are getting the correct information every time.

Moving forward, every column needs: the column name and the data type that goes into that column. The requirements and limitations you input on the backend help prevent human error when manually entering information on the front end.

Let's say your second column is the breed name. You want to make sure that this gets entered correctly on the front end.

First, you want to make sure that there is enough space to enter the proper information. To do this, write breed_name followed by VARCHAR, which stands for vari-able-width character type. This specifies how much memory space to set aside each cell. The purpose is to ensure you don't waste memory (you probably don't need a 1,000 character limit for breed name) but that you have enough space to enter relevant information (two characters, obviously, isn't enough). Most dog breed names are unlikely to be more than 50 characters long, so let's set the character limit at that.

So, in our example, so far your syntax would read:

CREATE TABLE toys (

dog_id INT AUTO_INCREMENT PRIMARY KEY,

breed_name VARCHAR(50));

But wait! You don't want users inputting repeat entries, and you can prevent as much by adding certain modifiers after the datatype. Adding UNIQUE means you cannot input a repeat value. For example, if you try to add “toy poodle” to the database twice, you'll get an error message. You have probably encountered this type of error message before. Ever been told a username you want for a website is already in use and to pick something else? That is likely due to a uniqueness check on a backend database.

In our example, our syntax now reads:

CREATE TABLE toys (

dog_id INT AUTO_INCREMENT PRIMARY KEY,

breed_name VARCHAR(50) UNIQUE);

Keep in mind that this is a rudimentary introduction. As you become more proficient in SQL, you will learn a lot about differing theories and opinions about how to best configure your tables.

SQL: Table Operations

Once you have created a table, you can add, change, and remove records using various commands. This can get complicated, especially as you dive into SQL coding and syntax in more depth, but we'll stick to the basics here. The commands you'll likely use most often are INSERT, UPDATE, and DELETE.

For the purpose of table operations, let's imagine you're manipulating patient data in a hospital database.

INSERT

Insert allows you to enter values into a table. If you are entering strings – that is, strings of characters – you need to provide quotation marks around them. This includes numbers that are part of a longer string of characters, such as someone's address.

However, for columns that ask for a numeric value (like age, which is saved as an integer) or a boolean (like true or false), you do not need quotation marks.

Say you're entering the information for a patient named John Smith and need to list his name, address, and age. You would write:

INSERT INTO Patients (name, address, age) VALUES (‘John Smith', ‘123 Maple Street', 47);

UPDATE

You can also update existing information in a database. Say you need to update John Smith's age.

You would write:

Site SET age = 48 WHERE name = ‘John Smith';

DELETE

Maybe John Smith is no longer a patient at your hospital and you've been asked to delete his records. You would write:

DELETE FROM patients WHERE id = ‘John Smith';

SELECT

The select command allows you to pull information from a table. If you are looking for, say, the names and addresses of of all patients over 65, you would write:

SELECT name, address FROM patients WHERE age >65

However, say you want more than just names and addresses. You need to pull all existing information about patients over 65. Adding an asterisk will provide every single column. You would write:

SELECT * FROM patients WHERE age >65

The Bottom Line

The above guide provides some rudimentary information on SQL databases, tables, and table operations. This should lay down some groundwork to help you get started with SQL. As you become more familiar with these basic commands, you will be able to explore more advanced features and functions of SQL.

Looking for solutions for your company? Xano is the fastest No Code Backend development platform on the market. We give you a scalable server, a flexible database, and a No Code API builder that can transform, filter, and integrate with data from anywhere. Sign up here to get started.

The post SQL: Create Database, Table Operations, And More appeared first on Xano.