How to make a SQL database

How to make a SQL database. Welcome to Learn SQL series’ first article. We will start with two main commands in SQL in this section: Database creation and table creation. They should be utilized before starting work on anything with data, but both are quite basic (unless you use some template database).

Later in this series, I’m going to try to cover everything needed in the magic world of SQL and databases for the entire newbie. Let’s begin, then:

How to make a SQL database

Need SQL assignment done? Place an Order now
The objective of this post will be to build a database (by the command SQL Create Database) as indicated in the picture above and two tables (by using the command SQL Create Table). We put data in these tables, update and delete data, add new tables and make queries in the next articles.

 

What is the basis of data?

I want to define which database is before creating a database using the SQL Create database command. I’m going to utilize Oracle’s definition:

 

A database is an organized collection of structured or electronically stored information or data. Usually, a database is controlled by a database management system (DBMS).

 

I will use the Microsoft SQL Server Express edition in this article. Thus, DBMS is SQL, and T-SQL is the language we will employ. I’m going to use a quote again:

 

T-SQL is a set of Sybase and Microsoft programming extensions that add a number of features, including transaction control and exception and error handling, row processing, and declared variables to the Structured Query Language (SQL).

I’m not going to go into detail in this post, but we can finish the section by saying that a database is an ordered group of tables that include real-world data and some additional columns required to make the system work correctly. In future articles, we will discuss these.

 

Create SQL  Statement of Database

Our screen appears somewhat like that when you install and launch Microsoft SQL Server Management Studio:

SQL Create Database statement

It’s not at all fun. By developing a new database, we will make it more fun. The new window opens, and after clicking on the New Query, we may write something into it. The following image looks like this:

SQL Create Database statement

Need SQL assignment done? Place an Order now
We should be sure that we type it in the appropriate method before typing anything. It has the words of T-SQL – a collection of rules for the writing of distinct commands.

CREATE DATABASE database_name;

Fortunately, the SQL Create Database command is one of these commands. On Microsoft pages, you see the complete T-SQL Database syntax.

 

Need SQL assignment done? Place an Order now
I’m going to simplify it a great deal and just go with the fundamental form. We must use the following command to create a new database on our server:

CREATE DATABASE our_first_database;

 

Where we use the name you choose instead of the name of the database.

 

SQL Example Create Database

All right, let’s try it. All right. We’re going to execute a command:

 

CREATE DATABASE our database first;

 

Our databases are created and may be seen in the database list after the execution of this command.

You will also note that our first database has been created, alongside the other two directories you will see.

 

This is cool because your first database has just been successfully built. The difficulty is that in the database we have no storage. Let’s change that. Let’s change that.

 

SQL Table Statement Create

A table is a structure (the “Basic Unit”) used to hold data in a database in database theory.

 

Need SQL assignment done? Place an Order now
I like using analogies very much, therefore I’m going to do it here too. When you think about a library, one shelf with books is a database, and each book is a table. Each book has its unique content but is tied somehow to other volumes in the same shelf, either by sharing certain features or by being close.

 

Behind database tables, there is a whole lot of theory and how to decide where, but the easiest thing you can do is to follow. If we look at our data and decide what we should group data in tables so that everything belonging to the same real-life entity goes to the same table.

 

For example, if we want to save the city and country data, we will have in our database two separate tables – one for cities and the other for nations. We’re not going to jumble your data but relate it. This is not addressed by this Article and will be discussed in the next parts of this series.

 

Need SQL assignment done? Place an Order now
We will follow the syntax to define a table. You may see the complete T-SQL Table Syntax here, but the statement will be simplified again:

 

Table Name CREATE TABLE (

column name column type, column name column type

column name column type; column name type;

);

We’re only going to choose our table name and list in this table all the columns that we desire. Columns are also called attributes, and a property of one record in the table is described in every column. The column has its type and the value we are expecting from this column should be selected (number, text, etc.).

 

Table example Create SQL

Let’s look at our two tables definition:

 

First, the city table will be defined.

 

Table: town – Table

The town of

CREATE TABLE (

JOT IDENTITY NOT NULL (1, 1),

Char town name (128) NOT zero, NOT.

NOT NULL, lat decimal(9,6),

NOT NULL long decimal(9,6),

NOT NULL country id,

City pk CONSTRAINT Cloud PRIMARY (id)

);

A few things, please note:

 

NOT NULL -> This is a property that can’t be empty of this column (must be defined)

IDENTITY(1, 1) –> is also a column property, indicating us that this value is automatically created from 1 to 1 for each new entry.

City pk CONSTRAINT CLIC (id) -> PRIMARY It is not a column, but rather a rule that says that column id only has UNIQUE values. There can be just one city = 5

 

— Table: country country

Country

CREATES TABLE (

JOT IDENTITY NOT NULL (1, 1),

Name of country char (128) NOT zero, NOT.

Charcode name eng (128) NOT zero, NOT.

Charcode code (8) NOT zero, NOT.

country ak 1 UNIQUE (country name), CONSTRAINT country ak 1

UNIQUE (country name eng; CONSTRAINT country ak 2);

UNIQUE (country code), CONSTRAINT country ak 3

Country pk CONSTRAINT Cloud PRIMARY (id)

);

Here’s a new CONSTRAINT, the UNIQUE restriction. This says that in this table this value should be UNIQUE. E.g. CONSTRAINT country ak 1 UNIQUE defines that it is not possible to hold 2 same-name nations.

 

The definition of foreign keys is the last component of the script. It refers to city.county id=country.id and just 1 key is presented here.

 

 

— Keys from the outside

City country – reference – (table: city)

City Tab ADD City country ALTHER TABLE

Classical Class (country id)

Country (id) REFERENCE;

 

The keys are too complex and must be handled in a separate article (main and foreigner). The status of our database is as seen in the figure below upon executing these commands:

Need SQL assignment done? Place an Order now

Conclusion

Congratulations. With SQL Create Database and Table Commands, you have successfully built the first database. Our database contains 2 tables. We are now ready to add data for them and test them if we have done so as intended. In the future article we are going to do it, so stay tuned!

Leave a Comment

Your email address will not be published. Required fields are marked *