Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

Step-by-step creation of SQL table

11 September 2020

Step-by-step creation of SQL table

Before starting to create an SQL table, it is necessary to define the database model. Design an ER diagram in which to define the entities, attributes, and relationships.

Basic concepts

Essences – objects or facts, information about which should be kept. For example, an employee of a company or project implemented by the company. Attributes – a component that describes or qualifies an entity. For example, an attribute of an entity “employee” – salary and attribute of an entity “project” – estimated value.

Links – associations between two elements. It may be bidirectional. There is also a recursive connection, i.e. the connection of the entity with itself.

Basic concepts

It is also necessary to define the keys and conditions under which the database integrity will be preserved. What does it mean? In other words, it means the constraints that will help keep the databases in a correct and consistent form.

The transition from the ER-diagram to the tabular model

Rules of transition to a tabular model:

  • Convert all entities into tables.
  • Convert all attributes into columns, i.e. each attribute of an entity must be displayed in the column name of the table.
  • Convert unique identifiers to primary keys.
  • Convert all relations into external keys.
  • Create an SQL table.

Creating the Base

First, you need to run the MySQL server. To start it, go to the Start menu, then to Programs, then to MySQL and MySQL Server, select MySQL-Command-Line-Client.

The Create Database command is used to create a database. This function has the following format:

CREATE DATABASE Database_name

  • The restrictions on the database name are as follows: the length is up to 64 characters and may include letters, digits, ” and “;
  • the name may start with a digit but must contain letters.

CREATE DATABASE Database_name

You should also remember the general rule: any query or command ends with a delimiter. In SQL it is common to use a semicolon as a delimiter. The server shall specify which database it shall work with. The USE operator shall be used for this purpose. This operator has a simple syntax:

USE name_database_data

Create an SQL table

So, the model is designed, the database is created and the server is instructed how to work with it. Now you can start creating SQL tables. There is a data definition language (DDL). It is used to create an MS SQL table, and also to define objects and work with their structure. DDL includes a set of commands.

SQL Server to create a table

Using only one DDL command, you can create different database objects by varying its parameters. The Create Table command is used to create an SQL table. The tt format is as follows:

CREATE TABLE name_table, (column_name1 data type [DEFAULT expression] [column_limit], column_name2 data type [DEFAULT expression] [column_limit], [table_limit])

Create an SQL table

The syntax of this command should be described in more detail:

  • The name of the table must be up to 30 characters long and start with a letter. Only characters of the alphabet, letters, and “_”, “$” and “#” are allowed. Cyrillic characters are allowed. It is important to note that the table names shall not coincide with the names of other objects and the reserved words of the database server, such as Column, Table, Index, etc.
  • The data type shall be specified for each column. There shall be a standard set used by the majority. For example, Char, Varchar, Number, Date, Null type, etc.

The syntax of this command should be described

  • With the Default parameter, you can set the default value. This ensures that there are no undefined values in the table. What does it mean? The default value can be a symbol, an expression, a function. It is important to remember that the type of these defaults should be the same as the type of data entered in the column.
  • Restrictions on each column are used to implement integrity conditions for data at the table level. There are other nuances. It is forbidden to delete a table if there are other tables dependent on it.

How to work with the database

Large projects often require the creation of several databases, and each requires many tables. Of course, it is impossible for users to keep all information in their heads. For this purpose, there is an option to view the structure of databases and tables in them. There are several commands, namely:

  • SHOW DATABASES – shows all created SQL databases on the screen;
  • SHOW TABLES – displays the list of all tables for the current database, which are selected by the USE command;
  • DESCRIBE name_table – displays the description of all columns in the table;
  • ALTER TABLE – allows you to change the table structure.

The last command enables you to: add a column or a restriction to the table; change an existing column; delete a column or columns; delete integrity restrictions. The syntax of this command looks like this:

ALTER TABLE column_name { [ADD column_name or restriction] | [MODIFY column_name] | [DROP column_name_removeable_limit] | [{ENABLE | DISABLE} CONSTANT name_limit ] | }.

There are other commands as well:

  • RENAME – rename the table
  • TRUNCATE TABLE – removes all rows from the table. This function may be needed when you need to fill the table anew and there is no need to store the previous data.

There may also be situations when the database structure has changed and the table shall be deleted. The DROP command shall be used for this purpose. Of course, you shall first select the database from which the table shall be deleted if it differs from the current one.

The syntax of the command is quite simple:

DROP TABLE name_table

DROP TABLE name_table

In SQL Access, creating tables and modifying them is done with the same commands listed above.

With CREATE TABLE you can create an empty table and fill it with data later on. But that’s not all. You can also immediately create a table from another table. How does it work? In other words, you can define a table and fill it with data from another table. For this purpose, there is a special keyword AS.

The syntax is very simple:

CREATE TABLE name_table [(column_definition)] AS subquery;

  • column_definition – column names, integrity rules for columns of the newly created table and default values;
  • subquery – returns the rows to be added to the new table.

Thus, this command creates a table with specific columns, inserts rows that are returned in the query.

Time tables

Temporary tables are tables in which data is erased at the end of each session or earlier. They are used to record intermediate values or results. They can be used as worksheets.

Temporary tables can be defined in any session, and their data can only be used in the current session. Creation of temporary SQL tables is similar to the usual one, using the CREATE TABLE command. To show the system that a table is temporary, you need to use the GLOBAL TEMPORARY option.

Time tables

The ON COMMIT offer sets the lifetime of data in such a table and can perform the following actions:

  • DELETE ROWS – clear the temporary table (delete all session data) after each end of the transaction. Usually this value is used by default.
  • PRESERVE ROWS – leave data to use in the next transaction. In addition, you can clear the table only after the end of the session. But there are features. If there is a rollback of a transaction (ROLLBACK), the table will be returned to the end of the previous transaction.

The syntax for creating a temporary table can be represented in this way:

CREATE [GLOBAL TEMPORARY] TABLE name_table, (column_name1 data type [DEFAULT expression] [restriction_column], column_name2 data type [DEFAULT expression] [restriction_column], [restriction_table]).

 
Tags: , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...