Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15404 Server version: 5.6.31-77.0-log Percona Server (GPL), Release 77.0, Revision 5c1061c
Copyright (c) 2009-2016 Percona LLC and/or its affiliates Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
zrd
2.在线修改格式
PROMPT '\h=\u=\D=\d' PROMPT set to ''\h=\u=\D=\d'' 'localhost=root=Thu Aug 25 23:39:37 2016=(none)'
3.配置文件中指定
一些基本函数
语句
意义
SELECT VERSION ();
版本
SELECT USER ();
当前登入的用户名主机名
SELECT NOW();
当前时间
DELIMITER //
修改默认分隔符为//,原来是’ ; ‘
mysql>\T /tmp/mysql_sql_.log
Logging to file ‘/tmp/mysql_sql_.log’ 将此会话所执行的语句及结果全部保存到指定的文件中
\t
mysql>\t Outfile disabled. 取消保存执行语句记录到文件中
注释
注释的内容将不被执行
方法1 # 注释的内容 方法2 -- 注释的内容
3.数据库相关操作(DDL数据库定义语言)
{} 花括号必须要写写上相关的内容 | 二选一 [] 方括号是可选的
1.数据库创建
创建语法
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name
Name: 'TINYINT' Description: TINYINT[(M)] [UNSIGNED] [ZEROFILL] A very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255.
3.1.2 SMALLINT
Name: 'SMALLINT' Description: SMALLINT[(M)] [UNSIGNED] [ZEROFILL] A small integer. The signed range is -32768 to 32767. The unsigned range is 0 to 65535.
3.1.3 MEDIUMINT
Name: 'MEDIUMINT' Description: MEDIUMINT[(M)] [UNSIGNED] [ZEROFILL] A medium-sized integer. The signed range is -8388608 to 8388607. The unsigned range is 0 to 16777215.
3.1.4 INT
Name: 'INT' Description: INT[(M)] [UNSIGNED] [ZEROFILL] A normal-size integer. The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295.
3.1.5 BIGINT
-->? BIGINT Name: 'BIGINT' Description: BIGINT[(M)] [UNSIGNED] [ZEROFILL] A large integer. The signed range is -9223372036854775808 to 9223372036854775807. The unsigned range is 0 to 18446744073709551615.
3.1.6 BOOLEAN BOOL
Name: 'BOOLEAN' Description: BOOL, BOOLEAN These types are synonyms for TINYINT(1). A value of zero is considered false. Nonzero values are considered true:
-->SELECT * FROM num2; +------+------+ | num1 | num2 | +------+------+ | 0 | -12 | +------+------+ 1 row in set (0.00 sec) # 插入一个不符合的值 -->INSERT INTO num2 VALUES(-10,-12); ERROR 1264 (22003): Out of range value for column 'num1' at row 1
A small (single-precision) floating-point number. Permissible values are -3.402823466E+38 to -1.175494351E-38, 0, and 1.175494351E-38 to 3.402823466E+38. These are the theoretical limits, based on the IEEE standard. The actual range might be slightly smaller depending on your hardware or operating system. # 占用4个字节,单精度浮点型
A normal-size (double-precision) floating-point number. Permissible values are -1.7976931348623157E+308 to -2.2250738585072014E-308, 0, and 2.2250738585072014E-308 to 1.7976931348623157E+308. These are the theoretical limits, based on the IEEE standard. The actual range might be slightly smaller depending on your hardware or operating system. # 双精度浮点型
A packed "exact" fixed-point number. M is the total number of digits (the precision) and D is the number of digits after the decimal point (the scale). The decimal point and (for negative numbers) the "-" sign are not counted in M. If D is 0, values have no decimal point or fractional part. The maximum number of digits (M) for DECIMAL is 65. The maximum number of supported decimals (D) is 30. If D is omitted, the default is 0. If M is omitted, the default is 10. # 占用的长度为字节长度+2 和float一样
测试浮点型
-- 测试浮点型 CREATE TABLE IF NOT EXISTS `num4` ( num1 FLOAT(6,2), num2 DOUBLE(6,2), num3 DECIMAL(6,2) COMMENT '定点数不进行四舍五入,对精度要求比较高' ) CHARSET=GB2312;
A fixed-length string that is always right-padded with spaces to the specified length when stored. M represents the column length in characters. The range of M is 0 to 255. If M is omitted, the length is 1. # M个字节,0<=<=255
A variable-length string. M represents the maximum column length in characters. The range of M is 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. For example, utf8 characters can require up to three bytes per character, so a VARCHAR column that uses the utf8 character set can be declared to be a maximum of 21,844 characters. # L+1字节,其中L<=M且0<=M<=65535
3.3.3 TINYTEXT
Name: 'TINYTEXT' Description: TINYTEXT [CHARACTER SET charset_name] [COLLATE collation_name]
A TEXT column with a maximum length of 255 (28 - 1) characters. The effective maximum length is less if the value contains multi-byte characters. Each TINYTEXT value is stored using a 1-byte length prefix that indicates the number of bytes in the value. # L+1 个字节, L小于2的8次方
3.3.4 TEXT
Name: 'TEXT' Description: TEXT[(M)] [CHARACTER SET charset_name] [COLLATE collation_name]
A TEXT column with a maximum length of 65,535 (216 - 1) characters. The effective maximum length is less if the value contains multi-byte characters. Each TEXT value is stored using a 2-byte length prefix that indicates the number of bytes in the value. # L+2 个字节, L小于2的16次方
3.3.5 MEDIUMTEXT
Name: 'MEDIUMTEXT' Description: MEDIUMTEXT [CHARACTER SET charset_name] [COLLATE collation_name]
A TEXT column with a maximum length of 16,777,215 (224 - 1) characters. The effective maximum length is less if the value contains multi-byte characters. Each MEDIUMTEXT value is stored using a 3-byte length prefix that indicates the number of bytes in the value. # L+3 个字节, L小于2的24次方
3.3.6 ENUM
Name: 'ENUM' Description: ENUM('value1','value2',...) [CHARACTER SET charset_name] [COLLATE collation_name]
An enumeration. A string object that can have only one value, chosen from the list of values 'value1', 'value2', ..., NULL or the special '' error value. ENUM values are represented internally as integers. # 枚举类型,列举中的字节
-->SELECT * FROM cha1; +------+------+ | str1 | str2 | +------+------+ | 1 | 1 | | 1 | 1 | | a | a | | 我 | 我 | +------+------+ 4 rows in set (0.00 sec)
测试枚举类型
-- 测试枚举类型 CREATE TABLE IF NOT EXISTS `test7` ( sex ENUM('男','女','保密') COMMENT '枚举类型' );
INSERT INTO test7 VALUES('男'); INSERT INTO test7 VALUES('女'); INSERT INTO test7 VALUES('保密'); SELECT * FROM test7; +--------+ | sex | +--------+ | 男 | | 女 | | 保密 | +--------+ 3 rows in set (0.00 sec)
# 插入不符合情况时报错 INSERT INTO test7 VALUES('保密1'); Query OK, 1 row affected, 1 warning (0.03 sec) -->show warnings ; +---------+------+------------------------------------------+ | Level | Code | Message | +---------+------+------------------------------------------+ | Warning | 1265 | Data truncated for column 'sex' at row 1 | +---------+------+------------------------------------------+ 1 row in set (0.00 sec) -->SELECT * FROM test7; +--------+ | sex | +--------+ | 男 | | 女 | | 保密 | | | +--------+
# 可选择对应的序列插入 1-->男, 2-->女 , 3-->保密 从1开始计数, 只能从列表中选择一个对象,可以插入空值NULL INSERT INTO test7 VALUES('1'); -->SELECT * FROM test7; +--------+ | sex | +--------+ | 男 | | 女 | | 保密 | | | | 男 | +--------+ 5 rows in set (0.00 sec)
# 插入空值 INSERT INTO test7 VALUES('NULL'); -->SELECT * FROM test7; +--------+ | sex | +--------+ | 男 | | 女 | | 保密 | | 男 | | NULL | +--------+ 5 rows in set (0.00 sec)
测试集合类型
# 测试集合类型 和 枚举不同的是 集合可以选择多个值 CREATE TABLE IF NOT EXISTS test8( fav set('A','B','C','D') ); # 正常插入 从集合中选择元素 INSERT INTO test8 VALUES('A,B,C'); INSERT INTO test8 VALUES('D,B,A');
-->SELECT * FROM test8; +-------+ | fav | +-------+ | A,B,C | | A,B,D | +-------+ 2 rows in set (0.00 sec)
3.4 日期时间型
3.4.1 TIME
Name: 'TIME' Description: TIME[(fsp)]
A time. The range is '-838:59:59.000000' to '838:59:59.000000'. MySQL displays TIME values in 'HH:MM:SS[.fraction]' format, but permits assignment of values to TIME columns using either strings or numbers.
As of MySQL 5.6.4, an optional fsp value in the range from 0 to 6 may be given to specify fractional seconds precision. A value of 0 signifies that there is no fractional part. If omitted, the default precision is 0. # 3个字符
3.4.2 DATTE
Name: 'DATE' Description: DATE
A date. The supported range is '1000-01-01' to '9999-12-31'. MySQL displays DATE values in 'YYYY-MM-DD' format, but permits assignment of values to DATE columns using either strings or numbers. # 3个字节
3.4.3 DATETIME
Name: 'DATETIME' Description: DATETIME[(fsp)]
A date and time combination. The supported range is '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999'. MySQL displays DATETIME values in 'YYYY-MM-DD HH:MM:SS[.fraction]' format, but permits assignment of values to DATETIME columns using either strings or numbers.
As of MySQL 5.6.4, an optional fsp value in the range from 0 to 6 may be given to specify fractional seconds precision. A value of 0 signifies that there is no fractional part. If omitted, the default precision is 0. # 8个字节
3.4.4 TIMESTAMP
Name: 'TIMESTAMP' Description: TIMESTAMP[(fsp)]
A timestamp. The range is '1970-01-01 00:00:01.000000' UTC to '2038-01-19 03:14:07.999999' UTC. TIMESTAMP values are stored as the number of seconds since the epoch ('1970-01-01 00:00:00' UTC). A TIMESTAMP cannot represent the value '1970-01-01 00:00:00' because that is equivalent to 0 seconds from the epoch and the value 0 is reserved for representing '0000-00-00 00:00:00', the "zero" TIMESTAMP value. # 时间戳 4个字节
3.4.5 YEAR
Name: 'YEAR' Description: Syntax: YEAR(date)
Returns the year for date, in the range 1000 to 9999, or 0 for the "zero" date. # 1 个字节
测试时间类型
-- 测试YEAR类型 CREATE TABLE IF NOT EXISTS `test9`( `birth` YEAR );
INSERT INTO test9 VALUES(1901);
SELECT * FROM test9; +-------+ | birth | +-------+ | 1901 | +-------+ 1 row in set (0.00 sec)
-- 插入一个超出范围的例子 INSERT INTO test9 VALUES(2156); -->INSERT INTO test9 VALUES(2156); Query OK, 1 row affected, 1 warning (0.02 sec) -->SHOW WARNINGS ; +---------+------+------------------------------------------------+ | Level | Code | Message | +---------+------+------------------------------------------------+ | Warning | 1264 | Out of range value for column 'birth' at row 1 | +---------+------+------------------------------------------------+ # 当插入YEAR的值为0时 , 它会转为0000 INSERT INTO test9 VALUES(0); -->SELECT * FROM test9; +-------+ | birth | +-------+ | 1901 | | 0000 | +-------+ 2 rows in set (0.01 sec)
-- TIME类型 CREATE TABLE IF NOT EXISTS test10 ( test TIME );
-- 插入1 天 12小时12分12秒 INSERT INTO test10 VALUES('1 12:12:12'); SELECT * FROM test10; SELECT * FROM test10; +----------+ | test | +----------+ | 36:12:12 | +----------+ 1 row in set (0.00 sec)
-- 11:11:00 INSERT INTO test10 VALUES('11:11'); SELECT * FROM test10; SELECT * FROM test10; +----------+ | test | +----------+ | 36:12:12 | | 11:11:00 | +----------+ 2 rows in set (0.00 sec)
-- 12分34秒 INSERT INTO test10 VALUES('1234'); SELECT * FROM test10; SELECT * FROM test10; +----------+ | test | +----------+ | 36:12:12 | | 11:11:00 | | 00:12:34 | +----------+ 3 rows in set (0.00 sec)
-- 插入12秒 INSERT INTO test10 VALUES('12');
-- 00:00:00 INSERT INTO test10 VALUES('0');
3.5 BINARY
Name: 'BINARY' Description: BINARY(M)
The BINARY type is similar to the CHAR type, but stores binary byte strings rather than nonbinary character strings. M represents the column length in bytes.
4. 存储引擎
4.1 查看支持的存储引擎
SHOW ENGINES\G ***************************[ 1. row ]*************************** Engine | PERFORMANCE_SCHEMA Support | YES Comment | Performance Schema Transactions | NO XA | NO Savepoints | NO ***************************[ 2. row ]*************************** Engine | MRG_MYISAM Support | YES Comment | Collection of identical MyISAM tables Transactions | NO XA | NO Savepoints | NO ***************************[ 3. row ]*************************** Engine | CSV Support | YES Comment | CSV storage engine Transactions | NO XA | NO Savepoints | NO ***************************[ 4. row ]*************************** Engine | BLACKHOLE Support | YES Comment | /dev/null storage engine (anything you write to it disappears) Transactions | NO XA | NO Savepoints | NO ***************************[ 5. row ]*************************** Engine | MEMORY Support | YES Comment | Hash based, stored in memory, useful for temporary tables Transactions | NO XA | NO Savepoints | NO ***************************[ 6. row ]*************************** Engine | MyISAM Support | YES Comment | MyISAM storage engine Transactions | NO XA | NO Savepoints | NO ***************************[ 7. row ]*************************** Engine | ARCHIVE Support | YES Comment | Archive storage engine Transactions | NO XA | NO Savepoints | NO ***************************[ 8. row ]*************************** Engine | InnoDB Support | DEFAULT Comment | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys Transactions | YES # 支持事务 XA | YES # 支持分布式处理XA规范 Savepoints | YES # 保存点 ***************************[ 9. row ]*************************** Engine | FEDERATED Support | NO Comment | Federated MySQL storage engine Transactions | None XA | None Savepoints | None
查看当前使用的存储引擎
SHOW VARIABLES LIKE 'STORAGE_ENGINE'; +-----------------+---------+ | Variable_name | Value | |-----------------+---------| | storage_engine | InnoDB | +-----------------+---------+ 1 row in set
# 创建一张用户表 CREATE TABLE IF NOT EXISTS user10 ( id SMALLINT UNSIGNED KEY AUTO_INCREMENT, username VARCHAR(20) NOT NULL UNIQUE, password CHAR(32) NOT NULL, email varchar(50) NOT NULL DEFAULT '1234@163.com', age TINYINT UNSIGNED DEFAULT 18, addr VARCHAR(200) NOT NULL DEFAULT '北京', salary FLOAT(6,2), regTime INT UNSIGNED , face CHAR(100) NOT NULL DEFAULT 'default.jpg', sex ENUM('男','女','保密') DEFAULT '保密'
) ;
DESC user10; +----------+----------------------------+------+-----+--------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+--------------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | password | char(32) | NO | | NULL | | | email | varchar(50) | NO | | 1234@163.com | | | age | tinyint(3) unsigned | YES | | 18 | | | addr | varchar(200) | NO | | 北京 | | | salary | float(6,2) | YES | | NULL | | | regTime | int(10) unsigned | YES | | NULL | | | face | char(100) | NO | | default.jpg | | | sex | enum('男','女','保密') | YES | | 保密 | | +----------+----------------------------+------+-----+--------------+----------------+ 10 rows in set (0.00 sec)
# 方法1 TO 将user10 重命名为user11 ALTER TABLE user10 RENAME TO user11; # 方法2 AS ALTER TABLE user11 RENAME AS user10; # 方法3 直接写 ALTER TABLE user10 RENAME user11; # 以上 to 或者AS 可以省略
# 另一种方法修改 RENAME TABLE table_name_old TO table_name_new RENAME TABLE user11 TO user10; # TO 不能省略
4.4.2 添加字段
# 添加card 字段CHAR(18) DESC user8; +----------+----------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | password | char(32) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | 18 | | | addr | varchar(50) | NO | | beijing | | | sex | enum('男','女','保密') | NO | | 男 | | +----------+----------------------------+------+-----+---------+----------------+ 6 rows in set (0.00 sec)
ALTER TABLE `user8` ADD card CHAR(18); # 默认添加到末尾 Query OK, 0 rows affected (0.18 sec) Records: 0 Duplicates: 0 Warnings: 0 DESC user8; +----------+----------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | password | char(32) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | 18 | | | addr | varchar(50) | NO | | beijing | | | sex | enum('男','女','保密') | NO | | 男 | | | card | char(18) | YES | | NULL | | +----------+----------------------------+------+-----+---------+----------------+ 7 rows in set (0.00 sec)
# 添加完整性约束
# 添加到第一行 ALTER TABLE `user8` ADD COLUMN `test1` VARCHAR(100) NOT NULL FIRST; # 添加到指定的字段(username之后) ALTER TABLE user8 ADD test3 INT NOT NULL DEFAULT 100 AFTER username; DESC user8; +----------+----------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+---------+-------+ | test1 | varchar(100) | NO | | NULL | | | username | varchar(20) | NO | | NULL | | | test3 | int(11) | NO | | 100 | | | password | char(32) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | 18 | | | addr | varchar(50) | NO | | beijing | | | sex | enum('男','女','保密') | NO | | 男 | | | card | char(18) | YES | | NULL | | +----------+----------------------------+------+-----+---------+-------+ 8 rows in set (0.00 sec) # 选中一次表 完成多个操作 ALTER TABLE user8 ADD test4 INT NOT NULL DEFAULT 123 AFTER password, ADD test5 FLOAT(6,2) FIRST, ADD test6 SET('A','B','C');
# 删除字段test6 ALTER TABLE user8 DROP test6; DESC user8; +----------+----------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+---------+-------+ | test5 | float(6,2) | YES | | NULL | | | test1 | varchar(100) | NO | | NULL | | | username | varchar(20) | NO | | NULL | | | test3 | int(11) | NO | | 100 | | | password | char(32) | NO | | NULL | | | test4 | int(11) | NO | | 123 | | | age | tinyint(3) unsigned | YES | | 18 | | | addr | varchar(50) | NO | | beijing | | | sex | enum('男','女','保密') | NO | | 男 | | | card | char(18) | YES | | NULL | | +----------+----------------------------+------+-----+---------+-------+ 10 rows in set (0.00 sec)
# 一次选中表,删除多个字段 ALTER TABLE user8 DROP test3, DROP test4, DROP test5; Query OK, 0 rows affected (0.11 sec) Records: 0 Duplicates: 0 Warnings: 0 DESC user8; +----------+----------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+---------+-------+ | test1 | varchar(100) | NO | | NULL | | | username | varchar(20) | NO | | NULL | | | password | char(32) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | 18 | | | addr | varchar(50) | NO | | beijing | | | sex | enum('男','女','保密') | NO | | 男 | | | card | char(18) | YES | | NULL | | +----------+----------------------------+------+-----+---------+-------+ 7 rows in set (0.00 sec)
# 添加test字段 删除card字段 ALTER TABLE user8 ADD test INT UNSIGNED NOT NULL DEFAULT 10 AFTER sex, DROP card; Query OK, 0 rows affected (0.11 sec) Records: 0 Duplicates: 0 Warnings: 0 +----------+----------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+---------+-------+ | test1 | varchar(100) | NO | | NULL | | | username | varchar(20) | NO | | NULL | | | password | char(32) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | 18 | | | addr | varchar(50) | NO | | beijing | | | sex | enum('男','女','保密') | NO | | 男 | | | test | int(10) unsigned | NO | | 10 | | +----------+----------------------------+------+-----+---------+-------+ 7 rows in set (0.00 sec)
4.4.4 修改字段
# 修改字段 ALTER TABLE tbl_name MODIFY 字段名 字段类型[完整性约束] [FIRST|AFTER]
# 将Email 修改成VARCHAR(200) ALTER TABLE user10 MODIFY email VARCHAR(200) NOT NULL DEFAULT '2920@qq.com'; Query OK, 0 rows affected (0.12 sec) Records: 0 Duplicates: 0 Warnings: 0 desc user10; +----------+----------------------------+------+-----+-------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+-------------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | password | char(32) | NO | | NULL | | | email | varchar(200) | NO | | 2920@qq.com | | | age | tinyint(3) unsigned | YES | | 18 | | | addr | varchar(200) | NO | | 北京 | | | salary | float(6,2) | YES | | NULL | | | regTime | int(10) unsigned | YES | | NULL | | | face | char(100) | NO | | default.jpg | | | sex | enum('男','女','保密') | YES | | 保密 | | +----------+----------------------------+------+-----+-------------+----------------+ 10 rows in set (0.00 sec)
# 修改字段的位置 将 addr 移动到sex之后 ALTER TABLE user10 MODIFY addr VARCHAR(200) NOT NULL DEFAULT '北京' AFTER sex; Query OK, 0 rows affected (0.08 sec) Records: 0 Duplicates: 0 Warnings: 0 DESC user10; +----------+----------------------------+------+-----+-------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+-------------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | password | char(32) | NO | | NULL | | | email | varchar(200) | NO | | 2920@qq.com | | | age | tinyint(3) unsigned | YES | | 18 | | | salary | float(6,2) | YES | | NULL | | | regTime | int(10) unsigned | YES | | NULL | | | face | char(100) | NO | | default.jpg | | | sex | enum('男','女','保密') | YES | | 保密 | | | addr | varchar(200) | NO | | 北京 | | +----------+----------------------------+------+-----+-------------+----------------+ 10 rows in set (0.00 sec)
# 将addr字段修改位CHAR(32) NOT NULL DEFAULT '123' 移动到第一个位置 ALTER TABLE user10 MODIFY addr CHAR(30) NOT NULL DEFAULT '123' FIRST; DESC user10; +----------+----------------------------+------+-----+-------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+-------------+----------------+ | addr | char(30) | NO | | 123 | | | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | password | char(32) | NO | | NULL | | | email | varchar(200) | NO | | 2920@qq.com | | | age | tinyint(3) unsigned | YES | | 18 | | | salary | float(6,2) | YES | | NULL | | | regTime | int(10) unsigned | YES | | NULL | | | face | char(100) | NO | | default.jpg | | | sex | enum('男','女','保密') | YES | | 保密 | | +----------+----------------------------+------+-----+-------------+----------------+ 10 rows in set (0.00 sec)
# 字段名修改 ALTER TABLE tbl_name CHANGE 旧字段名称 新字段名称 字段类型 [完整性约束条件] [FIRST|AFTER 字段名称] # 将test字段改为test1 ALTER TABLE user10 CHANGE test test1 CHAR(32) NOT NULL DEFAULT '123'; DESC user10; +----------+----------------------------+------+-----+-------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+-------------+----------------+ | addr | char(30) | NO | | 123 | | | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | password | char(32) | NO | | NULL | | | email | varchar(200) | NO | | 2920@qq.com | | | age | tinyint(3) unsigned | YES | | 18 | | | salary | float(6,2) | YES | | NULL | | | regTime | int(10) unsigned | YES | | NULL | | | face | char(100) | NO | | default.jpg | | | sex | enum('男','女','保密') | YES | | 保密 | | | test1 | char(32) | NO | | 123 | | +----------+----------------------------+------+-----+-------------+----------------+ 11 rows in set (0.00 sec)
# 修改字段属性,但是不修改字段名 ALTER TABLE user10 CHANGE test1 test1 INT; DESC user10; +----------+----------------------------+------+-----+-------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+-------------+----------------+ | addr | char(30) | NO | | 123 | | | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | password | char(32) | NO | | NULL | | | email | varchar(200) | NO | | 2920@qq.com | | | age | tinyint(3) unsigned | YES | | 18 | | | salary | float(6,2) | YES | | NULL | | | regTime | int(10) unsigned | YES | | NULL | | | face | char(100) | NO | | default.jpg | | | sex | enum('男','女','保密') | YES | | 保密 | | | test1 | int(11) | YES | | NULL | | +----------+----------------------------+------+-----+-------------+----------------+ 11 rows in set (0.00 sec)
4.4.5 添加删除默认值
# 添加默认值 ALTER TABLE tbl_name ALTER 字段名称 SET DEFAULT 默认值
CREATE TABLE IF NOT EXISTS user11 ( id TINYINT UNSIGNED KEY AUTO_INCREMENT, username VARCHAR(20) NOT NULL UNIQUE, age TINYINT UNSIGNED );
DESC user11; +----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | +----------+---------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
ALTER TABLE user11 ALTER age SET DEFAULT 18;
DESC user11; +----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | age | tinyint(3) unsigned | YES | | 18 | | +----------+---------------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec) # 添加email默认值 ALTER TABLE user11 ADD email VARCHAR(50) ; ALTER TABLE user11 ALTER email SET DEFAULT '2920@qq.com'; DESC user11; +----------+---------------------+------+-----+-------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+-------------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | age | tinyint(3) unsigned | YES | | 18 | | | email | varchar(50) | YES | | 2920@qq.com | | +----------+---------------------+------+-----+-------------+----------------+ 4 rows in set (0.00 sec)
# 删除默认值 ALTER TABLE tbl_name ALTER 字段名称 DROP DEFAULT
# 删除age默认值 ALTER TABLE user11 ALTER age DROP DEFAULT; DESC user11; +----------+---------------------+------+-----+-------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+-------------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | | email | varchar(50) | YES | | 2920@qq.com | | +----------+---------------------+------+-----+-------------+----------------+ 4 rows in set (0.00 sec) # 删除email 默认值 ALTER TABLE user11 ALTER email DROP DEFAULT; DESC user11; +----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | | email | varchar(50) | YES | | NULL | | +----------+---------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
4.4.6 添加删除主键
# 添加主键 CREATE TABLE IF NOT EXISTS test12( id INT ); DESC test12; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.00 sec) # 简单写法 ALTER TABLE test12 ADD PRIMARY KEY(id); DESC test12; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | 0 | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.00 sec)
CREATE TABLE IF NOT EXISTS test13 ( id INT, card CHAR(20), username VARCHAR(20) NOT NULL );
# 复合主键 ALTER TABLE test13 ADD PRIMARY KEY (id,card);
# 删除主键 ALTER TABLE tbl_name DROP PRIMARY KEY # 删除tes12的主键 ALTER TABLE test12 DROP PRIMARY KEY; # 因为一张表中只有一个主键 DESC test12; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | | 0 | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.00 sec)
ALTER TABLE test13 DROP PRIMARY KEY; DESC test13; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | id | int(11) | NO | | 0 | | | card | char(20) | NO | | | | | username | varchar(20) | NO | | NULL | | +----------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
# 添加主键,完整形式 ALTER TABLE test12 ADD CONSTRAINT symbol PRIMARY KEY index_type(id); +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | 0 | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.00 sec)
# 删除主键 CREATE TABLE IF NOT EXISTS test14( id INT UNSIGNED KEY AUTO_INCREMENT ); DESC test14; +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | +-------+------------------+------+-----+---------+----------------+ 1 row in set (0.00 sec)
ALTER TABLE test14 DROP PRIMARY KEY; ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key # 一张表中有且一列是自增长列且要被定义成主键 # 所以要先去掉自增长 ALTER TABLE test14 MODIFY id INT UNSIGNED ; DESC test14; +-------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | PRI | 0 | | +-------+------------------+------+-----+---------+-------+ 1 row in set (0.00 sec)
ALTER TABLE test14 DROP PRIMARY KEY; Query OK, 0 rows affected (0.11 sec) ESC test14; +-------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | | 0 | | +-------+------------------+------+-----+---------+-------+ 1 row in set (0.01 sec)
CREATE TABLE IF NOT EXISTS user12 ( id TINYINT UNSIGNED KEY AUTO_INCREMENT, username VARCHAR(20) NOT NULL, card CHAR(18), test VARCHAR(20) NOT NULL, test1 CHAR(32) NOT NULL );
DESC user12; +----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | card | char(18) | YES | | NULL | | | test | varchar(20) | NO | | NULL | | | test1 | char(32) | NO | | NULL | | +----------+---------------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec)
# 添加唯一性索引 ALTER TABLE user12 ADD UNIQUE(username); DESC user12; +----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | card | char(18) | YES | | NULL | | | test | varchar(20) | NO | | NULL | | | test1 | char(32) | NO | | NULL | | +----------+---------------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec) # 添加唯一性索引(完整写法) ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [索引名称] (字段名称,...) ALTER TABLE user12 ADD CONSTRAINT symbol UNIQUE KEY uni_card(card); desc user12; +----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | card | char(18) | YES | UNI | NULL | | | test | varchar(20) | NO | | NULL | | | test1 | char(32) | NO | | NULL | | +----------+---------------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec)
SHOW CREATE TABLE user12; CREATE TABLE `user12` ( `id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(20) NOT NULL, `card` char(18) DEFAULT NULL, `test` varchar(20) NOT NULL, `test1` char(32) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`), UNIQUE KEY `uni_card` (`card`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
# 添加联合索引 ALTER TABLE user12 ADD CONSTRAINT symbol UNIQUE INDEX mulUni_test_test1(test,test1); DESC user12; +----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | card | char(18) | YES | UNI | NULL | | | test | varchar(20) | NO | MUL | NULL | | | test1 | char(32) | NO | | NULL | | +----------+---------------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec)
# 测试非空约束 NOT NULL CREATE TABLE IF NOT EXISTS user7 ( id INT UNSIGNED KEY AUTO_INCREMENT, username VARCHAR(20) NOT NULL , password CHAR(32) NOT NULL, age TINYINT UNSIGNED );
DESC user7; +----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | password | char(32) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | +----------+---------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
CREATE TABLE IF NOT EXISTS user8 ( id INT UNSIGNED KEY AUTO_INCREMENT, username VARCHAR(20) NOT NULL , password CHAR(32) NOT NULL, age TINYINT UNSIGNED DEFAULT 18, addr VARCHAR(50) NOT NULL DEFAULT 'beijing', sex ENUM ('男','女','保密') NOT NULL DEFAULT '男' );
DESC user8; +----------+----------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | password | char(32) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | 18 | | | addr | varchar(50) | NO | | beijing | | | sex | enum('男','女','保密') | NO | | 男 | | +----------+----------------------------+------+-----+---------+----------------+ 6 rows in set (0.01 sec)
# 插入记录测试 INSERT INTO user8(username,password) VALUES('ZRD','A123456');
SELECT * FROM user8; +----+----------+----------+------+---------+-----+ | id | username | password | age | addr | sex | +----+----------+----------+------+---------+-----+ | 1 | ZRD | A123456 | 18 | beijing | 男 | +----+----------+----------+------+---------+-----+ 1 row in set (0.00 sec)
# 插入完整的记录 INSERT INTO user8 (username,password,age,addr,sex) VALUES('Alice','SDF123',23,'上海','女'); SELECT * FROM user8; +----+----------+----------+------+---------+-----+ | id | username | password | age | addr | sex | +----+----------+----------+------+---------+-----+ | 1 | ZRD | A123456 | 18 | beijing | 男 | | 2 | Alice | SDF123 | 23 | 上海 | 女 | +----+----------+----------+------+---------+-----+ 2 rows in set (0.00 sec)
# default 默认 INSERT INTO user8 (id,username,password,age,addr,sex) VALUES(3,'tomcat','SDF123',DEFAULT,DEFAULT,DEFAULT); SELECT * FROM user8; +----+----------+----------+------+---------+-----+ | id | username | password | age | addr | sex | +----+----------+----------+------+---------+-----+ | 1 | ZRD | A123456 | 18 | beijing | 男 | | 2 | Alice | SDF123 | 23 | 上海 | 女 | | 3 | tomcat | SDF123 | 18 | beijing | 男 | +----+----------+----------+------+---------+-----+ 3 rows in set (0.00 sec)
INSERT INTO user9 (username) VALUES('A'); # 受唯一性约束第二条记录插入失败 ERROR 1062 (23000): Duplicate entry 'A' for key 'username'
# NULL 为特殊字符,不算重复 INSERT INTO user9(username,card) VALUES('B',NULL); INSERT INTO user9(username,card) VALUES('B1',NULL); INSERT INTO user9(username,card) VALUES('B2',NULL); SELECT * FROM user9; +----+----------+------+ | id | username | card | +----+----------+------+ | 1 | A | NULL | | 3 | B | NULL | | 4 | B1 | NULL | | 5 | B2 | NULL | +----+----------+------+
收回权限
指定到数据库名 REVOKE ALL PRIVILEGES ON `bbs_db`.* FROM 'bbs_user'@'10.20.25.15';
查看权限
SHOW GRANTS FOR 'bbs_user'@'10.20.25.15';
授权
GRANT SELECT ON `bbs_db`.* TO 'bbs_user'@'10.20.25.15' IDENTIFIED BY '1YWRzZmFzZGYK@';
5.7
登录root账户后执行下面操作:
1、创建用户,名字为userone,密码为123456 create user userone identified by '123456'; 2、创建名字为dbone的数据库 create database dbone; 3、授予dbone数据库的所有权限给userone用户: grant all on dbone.* to userone