【PHP基础】常用mySQL语句以及WampServer2.2设置数据库默认编码

时间:2022-02-17 05:09:20

一、WampServer2.2设置数据库默认编码(此部分转自http://www.cnsecer.com/5984.html

wamp下MySQL的默认编码是Latin1,不支持中文,要支持中文的话需要把数据库的默认编码修改为gbk或者utf8。

这里推荐设置为UTF-8…国际统一编码

WAMP下面设置mysql编码的方法和手动安装的mysql设置方法稍微不一样

1.在[client]下添加

  1. default-character-set=utf8

【PHP基础】常用mySQL语句以及WampServer2.2设置数据库默认编码

2.在[wampmysqld]下添加

  1. character_set_server=utf8
  2. init_connect='SET NAMES utf8'

【PHP基础】常用mySQL语句以及WampServer2.2设置数据库默认编码

这就是wamp和手工安装的mysql设置不一样的地方…如果你的不是WAMP,是手动安装的mysql,  那么在[mysqld]下添加

  1. character_set_server=utf8
  2. init_connect='SET NAMES utf8'

即可

二、常用mysql语句

1. 显示所有数据库
show databases;

+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.03 sec)

2. 创建数据库(create database 数据库名;)
create database itcast;

Query OK, 1 row affected (0.01 sec)

如果是想创建一个自己设置编码的数据库,则
create database itcast_2 default character set GBK; (GBK表示字符集,可更换)

3. 查看已经创建的数据库的信息()
show create database itcast;

+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| itcast | CREATE DATABASE `itcast` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)

本来这里默认不是utf-8的,是我经过文章开头的步骤,把它的默认编码设置了下。

4. 删除数据库
drop database itcast_2;

Query OK, 0 rows affected (0.01 sec)

5. 使用数据库(use 数据库名;)
use itcast;

Database changed

6. 创建一个数据表( create table 表名(字段名1 类型1,字段名2 类型2,字段名n 类型n) )
create table student(
id int,
name varchar(10),
sex varchar(2),
age int,
create_dt date
);

Query OK, 0 rows affected (0.24 sec)

后期补充..
create table book(
isbn varchar(30),
book_name varchar(20),
price float,
author varchar(50),
date date,
sell_count int,
description text
);
7. 查看当前数据库中的所有数据表
show tables;

+------------------+
| Tables_in_itcast |
+------------------+
| student |
+------------------+
1 row in set (0.03 sec)

8. 查看某个数据表的字段信息(show columns from 数据表名)
show columns from student;

+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| sex | varchar(2) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| create_dt | date | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.18 sec)

9. 查看数据表的定义(show create table 数据表的名称)
show create table student;

+---------+---------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------------------------------------------------------------------+
| Table | Create Table

|
+---------+---------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
`sex` varchar(2) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`create_dt` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+---------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------------------------------------------------------------------+
1 row in set (0.00 sec)

10. 向表中插入数据( insert into 表名 (字段名1,字段名2,...字段名n) values (值1,值2,...值n) )
INSERT INTO book
(isbn,book_name,price,author,date,sell_count,description)
values
("7-111-205-123","MYSQL book",23.5,"jack","2011-08-27",5,"i am a book");

Query OK, 1 row affected (0.05 sec)

11. 查询表中数据 select (字段1,字段2,..字段n) from 表名 where 表达式;
select * from book;

+---------------+------------+-------+--------+------------+------------+-------------+
| isbn | book_name | price | author | date | sell_count | description |
+---------------+------------+-------+--------+------------+------------+-------------+
| 7-111-205-123 | MYSQL book | 23.5 | jack | 2011-08-27 | 5 | i am a book |
+---------------+------------+-------+--------+------------+------------+-------------+
1 row in set (0.00 sec)

12. 删除数据表(drop table 表名)
drop table student;

Query OK, 0 rows affected (0.05 sec)