Creating Database Objects
This tutorial describes how to create tables, stored procedures and other objects on a MySQL server. The tutorial consists of the following sections:
- General information
- Using MySQL Command Line Client
- Using dbForge Fusion for MySQL
In order to create database objects you have to connect to server. This process is described in details in the tutorial Logging onto the server.
Database objects are created using Data Definition Language (DDL), which is a part of SQL. The DDL statements can be executed on server by account that has necessary privileges.
There are two ways to manipulate a database. You can build DDL statements manually and run them within MySQL Command Line Client or a component like MySqlCommand. Another way is to use IDE - visual shells that provide graphical user interface to manage database. We will discuss both ways.
- Launch the MySQL Client and authorize yourself.
- Type CREATE DATABASE demo; and press Enter. This will create new database with name demo, which we will use later. From now on typing a query and pressing Enter will be referred as running a query.
- Run query USE demo;. This will allow to omit database name in later queries.
- Run the following query:
CREATE TABLE dept ( deptno INT PRIMARY KEY, dname VARCHAR(14), loc VARCHAR(13) ) ENGINE = InnoDB;
Press Enter. This will create first of the tables we'll use for tutorial purposes.
Run the following query:
CREATE TABLE emp ( empno INT PRIMARY KEY, ename VARCHAR(10), job VARCHAR(9), mgr INT, hiredate DATETIME, sal FLOAT, comm FLOAT, deptno INT, CONSTRAINT emp_fk FOREIGN KEY (deptno) REFERENCES dept (deptno) ) ENGINE = InnoDB;
- These two tables are enough to demonstrate basic functionality. Now you can type exit to exit the MySQL Client.
Things are much simpler when you control them visually. The same statements can be executed without writing a line of code. This section describes how to manipulate database objects visually in dbForge Fusion for MySQL.
- Open the Database Explorer toolwindow if it is not open yet. To do this, in View menu point to dbForge Fusion, and then click Database Explorer item.
- Click on the New Connection button on Database Explorer or the toolbar.
- In the Database Connection Properties dialog specify connection parameters and click OK.
- Right-click on the connection node and choose New Database from popup menu.
- Type in database name and click OK.
- Expand the database you've created and right-click on Tables node.
- Choose New Table from popup menu.
- Input name of the table: dept, press OK.
- In Table menu, choose New Column.
- In the Column Editor describe first column and press OK.
- Repeat steps 9 and 10 for every column in the table.
- Press Ctrl+S to save the document. The table will be created in the database.
Note that dbForge Fusion for MySQL has its own comprehensive documentation with tutorials.
This article describes creating MySQL tables that are used in other tutorials for dotConnect for MySQL, a high-performance ADO.NET provider from Devart with ORM support.
There are more ways to create tables on a MySQL server. Any tool or component that is capable of running a SQL query, can be used to manage database objects. For example, such dotConnect for MySQL class as MySqlCommand suits fine for creating objects one by one, while MySqlScript is designed for executing series of DDL/DML statements. For information on DDL statements syntax refer to MySQL documentation.
You may also be interested in:
- Logging onto the server
- Inserting Data Into Tables
- Entity Framework Tutorial
- Entity Framework Core Database-First Tutorial for Full .NET Framework
- Tutorial: How to Connect Visual Studio LightSwitch to MySQL
- LINQ to MySQL Tutorial
- SimpleMembership and SimpleRole Providers for MySQL in ASP.NET MVC 4 Application Tutorial