1,MySQL建库语句比较简单,一句话:
1 create database tppamltest3
2,创建用户及授权:
1 insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject) values("localhost","用户名",password("密码"),"","","");2 flush privileges; 3 grant all privileges on tppamltest3.* to 用户名@localhost identified by '密码';4 flush privileges;
其中授权语句給用户分配了全部权限,具体其他看百度。
3,建表脚本:
1 use tppamltest3; 2 3 SET FOREIGN_KEY_CHECKS=0; 4 5 -- ---------------------------- 6 -- Table structure for cfg_auto_mend 7 -- ---------------------------- 8 DROP TABLE IF EXISTS `cfg_auto_mend`; 9 CREATE TABLE `cfg_auto_mend` (10 `ID` varchar(32) NOT NULL,11 `MEND_SQL` text,12 PRIMARY KEY (`ID`)13 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
注意最后有默认编码设置。
---------------------------完成-----------------------------
=================================
一些异常情况的处理
=================================
乱码问题:
首先,shell里可以查看MYSQL目前编码
show variables like '%character%';
+--------------------------+-------------------------------------+| Variable_name| Value |+--------------------------+-------------------------------------+| character_set_client | utf8|| character_set_connection | utf8|| character_set_database | gbk || character_set_filesystem | binary|| character_set_results| utf8|| character_set_server | utf8|| character_set_system | utf8|| character_sets_dir | E:/anzhuan/MySQL5.5/share/charsets/ |+--------------------------+-------------------------------------+8 rows in set (0.00 sec)
可以更改
set character_set_client = 字符集
只要编码保持一致就不会出现乱码。
================================一下内容为摘抄========================================
1.系统编码
>show variables like '%character%';
mysql> show variables like '%collation%';
改变系统编码:修改my.cnf(/etc/my.cnf)中默认的编码选项[mysqld]下添加default-charcter-set=utf8 mysql 5.5以上版本换成了character-set-server=utf8 重新启动mysql
命令形式 mysql> SET NAMES 'utf8'; 重新启动mysql的时候所有的设置将失效
2.数据库编码
查看数据库编码: mysql> show create database db_name;
修改数据库编码: mysql> ALTER DATABASE db_name ####这里修改整个数据库的编码
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci;
在在建数据库的时候指定编码:
mysql> CREATE DATABASE db_name
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci ;
3.数据库表和字段编码
查看数据库表和字段编码: mysql> show create table table_name;
>ALTER TABLE table_name DEFAULT CHARACTER SET utf8;
修改字段编码: mysql> ALTER TABLE `table_name` CHANGE `dd` `dd` VARCHAR( 45 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL 该命令就是将MYSQL数据库table_name表中 dd的字段编码改为utf8
4.命令行下插入汉字时指定编码:mysql> set names utf8; 有时候这一句很关键!
mysql> insert into test(name) values('王东伟');
总之,不管采用那一种编码方式,只要做到完全统一将能达到相应的效果。
-------------------------------------------------------------------分隔-----------------------------------------------------------------------
在查询结果中可以看到mysql 数据库系统中客户端、数据库连接、数据库、文件系统、查询结果、服务器、系统的字符集设置在这里,文件系统字符集是固定的,系统、服务器的字符集在安装时 确定,与乱码问题无关。乱码的问题与客户端、数据库连接、数据库、查询结果的字符集设置有关。
*注:客户端是看访问mysql 数据库的方式,通过命令行访问,命令行窗口就是客户端,通过JDBC 等连接访问,程序就是客户端我们在向mysql 写入中文数据时,在客户端、数据库连接、写入数据库时分别要进行编码转换。在执行查询时,在返回结果、数据库连接、客户端分别进行编码转换。现在我们应该 清楚,乱码发生在数据库、客户端、查询结果以及数据库连接这其中一个或多
个环节
接下来我们来解决这个问题
在登录数据库时,我们用mysql --default-character-set=字符集-u root -p 进行连接,这时我们
再用show variables like '%char%';命令查看字符集设置情况,可以发现客户端、数据库连接、
查询结果的字符集已经设置成登录时选择的字符集了
如果是已经登录了,可以使用set names 字符集;命令来实现上述效果,等同于下面的命令:
set character_set_client = 字符集
set character_set_connection = 字符集
set character_set_results = 字符集
如果是通过JDBC 连接数据库,可以这样写URL:
URL=jdbc:mysql://localhost:3306/abs?useUnicode=true&characterEncoding=字符集
JSP 页面等终端也要设置相应的字符集
数据库的字符集可以修改mysql 的启动配置来指定字符集,也可以在create database 时加上
default character set 字符集来强制设置database 的字符集
通过这样的设置,整个数据写入读出流程中都统一了字符集,就不会出现乱码了
为什么从命令行直接写入中文不设置也不会出现乱码?
可以明确的是从命令行下,客户端、数据库连接、查询结果的字符集设置没有变化
输入的中文经过一系列转码又转回初始的字符集,我们查看到的当然不是乱码
但这并不代表中文在数据库里被正确作为中文字符存储
举例来说,现在有一个utf8 编码数据库,客户端连接使用GBK 编码,connection 使用默认
的ISO8859-1(也就是mysql 中的latin1),我们在客户端发送"中文"这个字符串,客户端
将发送一串GBK 格式的二进制码给connection 层,connection 层以ISO8859-1 格式将这段
二进制码发送给数据库,数据库将这段编码以utf8 格式存储下来,我们将这个字段以utf8
格式读取出来,肯定是得到乱码,也就是说中文数据在写入数据库时是以乱码形式存储的,
在同一个客户端进行查询操作时,做了一套和写入时相反的操作,错误的utf8 格式二进制
码又被转换成正确的GBK 码并正确显示出来。

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.


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 Linux new version
SublimeText3 Linux latest version

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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