佛曰:“先有库,后有表,再有数据…..”
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification] ... create_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name
例如:创建数据库,并指定默认字符集为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 //这条命令在升级数据库时,使用此命令重新编码数据库文件
alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_nam
修改数据库mydb的字符集为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 删除数据库非常的。。。快!所以呢,想好再回车。
MariaDB [(none)]> DROP DATABASE IF EXISTS mydb; Query OK, 0 rows affected (0.00 sec)
只需要在my.cnf中加入此指令即可,在[mysqld]下:
character_set_server = utf8
CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (create_definition,...) [table_options ]... [partition_options]
比较基础的就是表定义选项了,如下:
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]
例如:创建User表,有4个字段,分别为:ID、用户名、密码、登录时间
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);
在Mysql中,可以使用DESCRIBE table_name;来查看表的定义,DESCRIBE可以缩写为DESC,如下:
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)
当然,我们也可以使用SHOW CREATE TABLE table_name;来查看定义表时所使用的命令
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;
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/116725.html