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!