Home > System Tutorial > LINUX > body text

Mariadb learning summary (2): database and table management

PHPz
Release: 2024-07-17 08:58:29
Original
450 people have browsed it
Database Management

Buddha said: "First there is the database, then there are tables, then there are data..."

Mariadb learning summary (2): database and table management

Create database
CREATE DATABASE [IF NOT EXISTS] db_name
    [create_specification] ...

create_specification:
    [DEFAULT] CHARACTER SET [=] charset_name
  | [DEFAULT] COLLATE [=] collation_name
Copy after login

For example: Create a database and specify the default character set as UTF-8

SHOW CHARACTER SET;//查看支持的字符集
CREATE DATABASE mydb CHARACTER SET='utf8'; //创建数据库mydb,并指定字符集为utf-8
Copy after login
View database creation information
MariaDB [(none)]> SHOW CREATE DATABASE mydb;
+----------+---------------------------------------------------------------+
| Database | Create Database                                               |
+----------+---------------------------------------------------------------+
| mydb     | CREATE DATABASE `mydb` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
Copy after login
Modify database

ALTER {DATABASE | SCHEMA} [db_name]
alter_specification ...
ALTER {DATABASE | SCHEMA} db_name
UPGRADE DATA DIRECTORY NAME //Use this command to re-encode the database file when upgrading the database

alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_nam

Modify the character set of the database mydb to utf-16:

MariaDB [(none)]> ALTER DATABASE mydb CHARACTER SET = utf16;
Query OK, 1 row affected (0.00 sec)
Copy after login
Delete database

DROP {DATABASE | SCHEMA} [IF EXISTS] db_name deletes the database very well. . . quick! So, think about it before entering the car.

MariaDB [(none)]> DROP DATABASE IF EXISTS mydb;
Query OK, 0 rows affected (0.00 sec)
Copy after login
Set database default character set

Just add this command to my.cnf, under [mysqld]:

character_set_server = utf8
Copy after login
Table Management
Create table
CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    (create_definition,...) [table_options    ]... [partition_options]
Copy after login

The more basic one is the table definition option, as follows:

column_definition:
    data_type
      [NOT NULL | NULL] [DEFAULT default_value | (expression)]
      [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
      [INVISIBLE] [{WITH|WITHOUT} SYSTEM VERSIONING]
      [COMMENT 'string']
      [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
      [reference_definition]
Copy after login

For example: Create a User table with 4 fields: ID, username, password, login time

MariaDB [mydb]> CREATE TABLE IF NOT EXISTS user(
    -> id INT AUTO_INCREMENT PRIMARY KEY,
    -> username VARCHAR(10) NOT NULL,
    -> password VARCHAR(32) NOT NULL,
    -> logintime TIMESTAMP NOT NULL);
Copy after login
View table

In Mysql, you can use DESCRIBE table_name; to view the definition of the table. DESCRIBE can be abbreviated as DESC, as follows:

MariaDB [mydb]> DESC user;
+-----------+-------------+------+-----+-------------------+-----------------------------+
| Field     | Type        | Null | Key | Default           | Extra                       |
+-----------+-------------+------+-----+-------------------+-----------------------------+
| id        | int(11)     | NO   | PRI | NULL              | auto_increment              |
| username  | varchar(10) | NO   |     | NULL              |                             |
| password  | varchar(32) | NO   |     | NULL              |                             |
| logintime | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-----------+-------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.03 sec)
Copy after login

Of course, we can also use SHOW CREATE TABLE table_name; to view the commands used to define the table

MariaDB [mydb]> SHOW CREATE TABLE user;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                           |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user  | CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(10) NOT NULL,
  `password` varchar(32) NOT NULL,
  `logintime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf16 |  //这里可以看到这张表使用的存储引擎和字符集
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Copy after login
Modify table

1.为user表添加一个新的字段registtime来记录用户的注册时间

MariaDB [mydb]> ALTER TABLE user ADD COLUMN registtime TIMESTAMP NOT NULL AFTER logintime;
Copy after login

所以,添加字段的格式可以为如下:

ALTER TABLE table_name ADD [COLUMN] col_name column_definition
        [FIRST | AFTER col_name ]
Copy after login

其中,FIRST与AFTER是指定新添加的字段在什么位置,FIRST代表第一列,而AFTER指示在某一列之后

2.为user表修改一个字段,把刚才新加的registtime字段的数据类型修改为datatime类型

MariaDB [mydb]> ALTER TABLE user MODIFY COLUMN registtime DATETIME;
Copy after login

命令格式如下:

ALTER TABLE table_name MODIFY [COLUMN] col_name column_definition
        [FIRST | AFTER col_name]
Copy after login

3.修改字段registtime为createtime
MariaDB [mydb]> ALTER TABLE user CHANGE registtime createtime DATETIME NOT NULL;
命令格式如下,需要重新定义下新的字段:

ALTER TABLE table_name CHANGE [COLUMN] old_col_name new_col_name column_definition
        [FIRST|AFTER col_name]
Copy after login

4.删除这个createtime字段,所有行的这个字段的数据也会被删除

MariaDB [mydb]> ALTER TABLE user DROP COLUMN createtime;
Copy after login

5.修改表名user为users

MariaDB [mydb]> ALTER TABLE user RENAME TO users;
Copy after login

6.修改数据表的字符集

MariaDB [mydb]> ALTER TABLE users DEFAULT CHARACTER SET=utf8;
Copy after login

7.修改数据表的存储引擎,有关存储引擎这方面暂时了解的不深,所以这条命令....嗯,你懂得。

MariaDB [mydb]> ALTER TABLE users ENGINE=MyISAM;
Copy after login

会导致数据重建的...所以,谨慎修改

8.修改数据表的排序字段

MariaDB [mydb]> ALTER TABLE users ORDER BY logintime;
Copy after login
删除一个数据表

与删除数据库一样,谨慎按下回车键

MariaDB [mydb]> DROP TABLE IF EXISTS users;
Copy after login

The above is the detailed content of Mariadb learning summary (2): database and table management. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template