Explore SQL with MatrixOne

MatrixOne is compatible with MySQL, you can use MySQL statements directly in most of the cases. For unsupported features, see MySQL Compatibility, and for more information on connecting to MatriOne, see Using client connect to the MatrixOne server.

What is SQL?

The Structured Query Language (SQL) is used to manage a relational database management system (RDBMS). The scope of SQL includes data insertion, query, update, and delete, database schema creation and modification, and data access control.

How many types SQL does MatrixOne have?

SQL is divided into the following 6 types according to their functions in MatrixOne:

  • DDL (Data Definition Language): It is used to define database objects, including databases, tables, and views. Such as CREATE, ALTER, and DROP.

  • DML (Data Manipulation Language): It is used to manipulate application related records. Such as SELECT, DELETE, and INSERT.

  • DQL (Data Query Language): It is used to query the records after conditional filtering. Such as SELECT...FROM...[WHERE].

  • TCL (Transaction Control Language): It is used to manage transactions. Such as COMMIT,ROLLBACK, or SET TRANSACTION.

  • DCL (Data Control Language): It is used to define access privileges and security levels. Such as CREATET ACCOUNT, DROP ACCOUNT, and GRANT.

  • Other: Other types of management languages ​​in MatrixOne are not directly related to data and are a general term for the acquisition and modification of database parameters and resource allocation. Such as SHOW, SET variable, and KILL.

Basic SQL about SHOW, CREATE and DROP a database

A database in MatrixOne can be considered as a collection of objects such as tables.

  • To show the list of databases, use the SHOW DATABASES statement:
  1. SHOW DATABASES;
  • To create the database named dbdemo, use the CREATE DATABASE db_name [options]; statement, for example:
  1. CREATE DATABASE dbdemo;

or:

  1. CREATE DATABASE IF NOT EXISTS dbdemo;

Add IF NOT EXISTS to prevent an error if the database exists.

  • To use the database named dbdemo, use the following statement:
  1. USE dbdemo;
  • To show all the tables in a database, use the SHOW TABLES statement:
  1. SHOW TABLES FROM dbdemo;
  • To delete a database, use the DROP DATABASE statement:
  1. DROP DATABASE dbdemo;

Basic SQL about CREATE, SHOW, and DROP a table

  • To create a table, use the CREATE TABLE statement:
  1. CREATE TABLE table_name column_name data_type constraint;

For example, to create a table named person which includes fields such as number, name, and birthday, use the following statement:

  1. CREATE TABLE person (
  2. id INT(11),
  3. name VARCHAR(255),
  4. birthday DATE
  5. );
  • To view the statement that creates the table (DDL), use the SHOW CREATE statement:
  1. SHOW CREATE table person;

Expected result:

  1. +--------+-----------------------------------------------------------------------------------------------------------------+
  2. | Table | Create Table |
  3. +--------+-----------------------------------------------------------------------------------------------------------------+
  4. | person | CREATE TABLE `person` (
  5. `id` INT DEFAULT NULL,
  6. `name` VARCHAR(255) DEFAULT NULL,
  7. `birthday` DATE DEFAULT NULL
  8. ) |
  9. +--------+-----------------------------------------------------------------------------------------------------------------+
  10. 1 row in set (0.01 sec)
  • To delete a table, use the DROP TABLE statement:
  1. DROP TABLE person;

Basic SQL about INSERT, UPDATE, and DELETE data

Common DML features are adding, modifying, and deleting table records. The corresponding commands are INSERT, UPDATE, REPLACE and DELETE.

  • To insert data into a table, use the INSERT statement:
  1. INSERT INTO person VALUES(1,'tom','20170912');
  • To insert a record containing data of some fields into a table, use the INSERT statement:
  1. INSERT INTO person(id,name) VALUES('2','bob');
  • To update some fields of a record in a table, use the UPDATE statement:
  1. UPDATE person SET birthday='20180808' WHERE id=2;
  • To replace some field data of table records in the table, use the UPDATE statement:
  1. REPLACE INTO person SET birthday='20180809' WHERE id=2;
  • To delete the data in a table, use the DELETE statement:
  1. DELETE FROM person WHERE id=2;

Note

The UPDATE and DELETE statements without the WHERE clause as a filter operate on the entire table.

Basic SQL about Query data

DQL is used to retrieve the desired data rows from a table or multiple tables.

  • To view the data in a table, use the SELECT statement:
  1. SELECT * FROM person;

Expected result:

  1. +------+------+------------+
  2. | id | name | birthday |
  3. +------+------+------------+
  4. | 1 | tom | 2017-09-12 |
  5. +------+------+------------+
  6. 1 row in set (0.00 sec)
  • To query a specific column, add the column name after the SELECT keyword:
  1. SELECT name FROM person;
  2. +------+
  3. | name |
  4. +------+
  5. | tom |
  6. +------+
  7. 1 rows in set (0.00 sec)

Use the WHERE clause to filter all records that match the conditions and then return the result:

  1. SELECT * FROM person where id<5;

Expected result:

  1. +------+------+------------+
  2. | id | name | birthday |
  3. +------+------+------------+
  4. | 1 | tom | 2017-09-12 |
  5. +------+------+------------+
  6. 1 row in set (0.00 sec)

Basic SQL about CREATE, GRANT, and DROP a user

CREATE, GRANT, and DROP are usually used to create or delete users, and manage user privileges.

  • To create a user, use the CREATE USER statement. The following example creates a user named mouser with the password 111:
  1. > CREATE USER mouser IDENTIFIED BY '111';
  2. Query OK, 0 rows affected (0.10 sec)
  • To create a role for the user:
  1. > CREATE ROLE role_r1;
  2. Query OK, 0 rows affected (0.05 sec)
  • To grant mouser the role_r1:
  1. > GRANT role_r1 to mouser;
  2. Query OK, 0 rows affected (0.04 sec)
  • To grant mouser the privilege to create table in the dbdemo database:
  1. GRANT create table on database * to role_r1;
  • To check the privileges of mouser:
  1. > SHOW GRANTS for mouser@localhost;
  2. +-------------------------------------------------------+
  3. | Grants for mouser@localhost |
  4. +-------------------------------------------------------+
  5. | GRANT create table ON database * `mouser`@`localhost` |
  6. | GRANT connect ON account `mouser`@`localhost` |
  7. +-------------------------------------------------------+
  8. 2 rows in set (0.02 sec)

You have successfully granted the permission of create table in the database to mouser.

  • To delete mouser:
  1. DROP USER mouser;

Privilege management is a huge but very useful function. For more information, see Privilege Management.