MySQL学习笔记 初涉MySQL
短信预约 -IT技能 免费直播动态提醒
1.在Linux下安装MySQL
# yum -y install mysql mysql-server mysql-devel
修改字符集:/etc/my.conf配置文件
vi /etc/my.conf
[mysqld]
default-character-set=utf8
character_set_server=utf8
[client]
default-character-set=utf8
2.启动和停止MySQL服务
# service mysqld start
3.登录MySQL:
# mysql -uroot
4.修改MySQL提示符:
修改MySQL提示符
# mysql -uroot -proot --prompt ‘<提示符>’ //在windows中不用‘’
或 连接进MySQL后修改:
mysql> prompt <提示符>
常用的MySQL提示符:
\D | 完整的日期 | |
\d | 当前数据库 | |
\h | 服务器名称 | |
\u | 当前用户 | |
例:
# mysql -uroot --prompt '\u@\h \d'
root@localhost (none)
//或
mysql> prompt \u@\h \d>
PROMPT set to '\u@\h \d>'
root@localhost (none)>
5.MySQL常用命令
显示当前服务器版本号:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.1.71 |
+-----------+
1 row in set (0.00 sec)
显示当前用户:
mysql> SELECT USER();
+----------------+
| USER() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
显示当前日期时间:
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2016-11-20 21:32:55 |
+---------------------+
1 row in set (0.00 sec)
7.MySQL语句规范:
***关键字与函数名称全部大写
***数据库名称,表名称,字段名称全部小写
***SQL语句必须以分号结尾
8.数据库基本操作:
数据库创建:
mysql> CREATE DATABASE test1;
加上 IF NOT EXISTS,若已存在该数据库,会提示警告信息:
mysql> CREATE DATABASE IF NOT EXISTS test1;
Query OK, 1 row affected, 1 warning (0.00 sec)
查看警告信息:.
mysql> SHOW WARNINGS;
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Error | 1064 | You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WARNING' at line 1 |
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
查看数据库列表:
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| test1 |
+--------------------+
4 rows in set (0.00 sec)
显示数据库创建的时候使用的指令:
mysql> SHOW CREATE DATABASE test1;
+----------+----------------------------------------------------------------+
| Database | Create Database |
+----------+----------------------------------------------------------------+
| test1 | CREATE DATABASE `test1` |
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)
创建与配置文件不一样的数据库编码方式(例GBK):
mysql> CREATE DATABASE IF NOT EXISTS test2 CHARACTER SET gbk;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW CREATE DATABASE test2;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| test2 | CREATE DATABASE `test2` |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
修改数据库编码方式修改:
mysql> ALTER DATABASE test2 CHARACTER SET utf8;
或
mysql> ALTER DATABASE test2 CHARACTER SET = utf8;
删除数据库:
mysql> DROP DATABASE test1;
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341