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!

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

Introduction: Securing the Digital Frontier with Linux-Based Ethical Hacking In our increasingly interconnected world, cybersecurity is paramount. Ethical hacking and penetration testing are vital for proactively identifying and mitigating vulnerabi

The methods for basic Linux learning from scratch include: 1. Understand the file system and command line interface, 2. Master basic commands such as ls, cd, mkdir, 3. Learn file operations, such as creating and editing files, 4. Explore advanced usage such as pipelines and grep commands, 5. Master debugging skills and performance optimization, 6. Continuously improve skills through practice and exploration.

Linux is widely used in servers, embedded systems and desktop environments. 1) In the server field, Linux has become an ideal choice for hosting websites, databases and applications due to its stability and security. 2) In embedded systems, Linux is popular for its high customization and efficiency. 3) In the desktop environment, Linux provides a variety of desktop environments to meet the needs of different users.

The disadvantages of Linux include user experience, software compatibility, hardware support, and learning curve. 1. The user experience is not as friendly as Windows or macOS, and it relies on the command line interface. 2. The software compatibility is not as good as other systems and lacks native versions of many commercial software. 3. Hardware support is not as comprehensive as Windows, and drivers may be compiled manually. 4. The learning curve is steep, and mastering command line operations requires time and patience.

Linuxisnothardtolearn,butthedifficultydependsonyourbackgroundandgoals.ForthosewithOSexperience,especiallycommand-linefamiliarity,Linuxisaneasytransition.Beginnersmayfaceasteeperlearningcurvebutcanmanagewithproperresources.Linux'sopen-sourcenature,bas

The five basic components of Linux are: 1. The kernel, managing hardware resources; 2. The system library, providing functions and services; 3. Shell, the interface for users to interact with the system; 4. The file system, storing and organizing data; 5. Applications, using system resources to implement functions.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function