Mariadb learning summary (2): database and table management
Buddha said: "First there is the database, then there are tables, then there are data..."
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification] ... create_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name
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
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)
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)
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)
Just add this command to my.cnf, under [mysqld]:
character_set_server = utf8
CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (create_definition,...) [table_options ]... [partition_options]
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]
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);
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)
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)
1.为user表添加一个新的字段registtime来记录用户的注册时间
MariaDB [mydb]> ALTER TABLE user ADD COLUMN registtime TIMESTAMP NOT NULL AFTER logintime;
所以,添加字段的格式可以为如下:
ALTER TABLE table_name ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name ]
其中,FIRST与AFTER是指定新添加的字段在什么位置,FIRST代表第一列,而AFTER指示在某一列之后
2.为user表修改一个字段,把刚才新加的registtime字段的数据类型修改为datatime类型
MariaDB [mydb]> ALTER TABLE user MODIFY COLUMN registtime DATETIME;
命令格式如下:
ALTER TABLE table_name MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
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]
4.删除这个createtime字段,所有行的这个字段的数据也会被删除
MariaDB [mydb]> ALTER TABLE user DROP COLUMN createtime;
5.修改表名user为users
MariaDB [mydb]> ALTER TABLE user RENAME TO users;
6.修改数据表的字符集
MariaDB [mydb]> ALTER TABLE users DEFAULT CHARACTER SET=utf8;
7.修改数据表的存储引擎,有关存储引擎这方面暂时了解的不深,所以这条命令....嗯,你懂得。
MariaDB [mydb]> ALTER TABLE users ENGINE=MyISAM;
会导致数据重建的...所以,谨慎修改
8.修改数据表的排序字段
MariaDB [mydb]> ALTER TABLE users ORDER BY logintime;
与删除数据库一样,谨慎按下回车键
MariaDB [mydb]> DROP TABLE IF EXISTS users;
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

How to run Python scripts in Sublime Text: Install Python interpreter configuration Interpreter path in Sublime Text Press Ctrl B (Windows/Linux) or Cmd B (macOS) to run the script If an interactive console is required, press Ctrl \ (Windows/Linux) or Cmd \ (macOS)
