正常访问状态! 设为首页 | 加入收藏夹 | 浏览历史  
  http://www.guosp.com
 碧海澜涛居
  海纳百川,有容乃大。壁立千刃,无欲则刚!
 
 
关键词:
  网站首页 | 关于本站 | 技术资料 | 美文日志 | 读书收藏 | 影视收藏 | 软件收藏 | 摄影相册| 留言板 
  技术资料 >> 数据库类 关闭(快捷键alt+C)
搜索标签: 常用命令
MYSql 常用指令集
[阅读次数:1497次]  [发布时间:2010年11月12日]

常用mysql命令行命令

 

1 .mysql的启动与停止
启动MYSQL服务 net start mysql
停止MYSQL服务 net stop mysql
2 . netstat –na | findstr 3306 查看被监听的端口 , findstr用于查找后面的端口是否存在
3 . 在命令行中登陆MYSQL控制台 , 即使用 MYSQL COMMEND LINE TOOL
语法格式 mysql –user=root –password=123456 db_name
或 mysql –uroot –p123456 db_name
4 . 进入MYSQL命令行工具后 , 使用status; 或\s 查看运行环境信息
5 . 切换连接数据库的语法 : use new_dbname;
6 . 显示所有数据库 : show databases;
7 . 显示数据库中的所有表 : show tables;
8 . 显示某个表创建时的全部信息 : show create table table_name;
9 . 查看表的具体属性信息及表中各字段的描述
Describe table_name; 缩写形式 : desc table_name;

MySql中的SQL语句

 

1 . 数据库创建 : Create database db_name;
数据库删除 : Drop database db_name; 删除时可先判断是否存在,写成 : drop database if exits db_name
2 . 建表 : 创建数据表的语法 : create table table_name (字段1 数据类型 , 字段2 数据类型);
例 : create table mytable (id int , username char(20));
删表 : drop table table_name; 例 : drop table mytable;
8 . 添加数据 : Insert into 表名 [(字段1 , 字段2 , ….)] values (值1 , 值2 , …..);
如果向表中的每个字段都插入一个值,那么前面 [ ] 括号内字段名可写也可不写
例 : insert into mytable (id,username) values (1,’zhangsan’);
9 . 查询 : 查询所有数据 : select * from table_name;
查询指定字段的数据 : select 字段1 , 字段2 from table_name;
例 : select id,username from mytable where id=1 order by desc;多表查询语句------------参照第17条实例
10 . 更新指定数据 , 更新某一个字段的数据(注意,不是更新字段的名字)
Update table_name set 字段名=’新值’ [, 字段2 =’新值’ , …..][where id=id_num] [order by 字段 顺序]
例 : update mytable set username=’lisi’ where id=1;
Order语句是查询的顺序 , 如 : order by id desc(或asc) , 顺序有两种 : desc倒序(100—1,即从最新数据往后查询),asc(从1-100),Where和order语句也可用于查询select 与删除delete
11 . 删除表中的信息 :
删除整个表中的信息 : delete from table_name;
删除表中指定条件的语句 : delete from table_name where 条件语句 ; 条件语句如 : id=3;
12 . 创建数据库用户
一次可以创建多个数据库用户如:
CREATE USER username1 identified BY ‘password’ , username2 IDENTIFIED BY ‘password’….
13 . 用户的权限控制:grant
库,表级的权限控制 : 将某个库中的某个表的控制权赋予某个用户
Grant all ON db_name.table_name TO user_name [ indentified by ‘password’ ];
14 . 表结构的修改
(1)增加一个字段格式:
alter table table_name add column (字段名 字段类型); ----此方法带括号
(2)指定字段插入的位置:
alter table table_name add column 字段名 字段类型 after 某字段;
删除一个字段:
alter table table_name drop字段名;
(3)修改字段名称/类型
alter table table_name change 旧字段名 新字段名 新字段的类型;
(4)改表的名字
alter table table_name rename to new_table_name;
(5)一次性清空表中的所有数据
truncate table table_name; 此方法也会使表中的取号器(ID)从1开始
15 . 增加主键,外键,约束,索引。。。。(使用方法见17实例)
① 约束(主键Primary key、唯一性Unique、非空Not Null)
② 自动增张 auto_increment
③外键Foreign key-----与reference table_name(col_name列名)配合使用,建表时单独使用
④ 删除多个表中有关联的数据----设置foreign key 为set null ---具体设置参考帮助文档
16 . 查看数据库当前引擎
SHOW CREATE TABLE table_name;
修改数据库引擎
ALTER TABLE table_name ENGINE=MyISAM | InnoDB;
17 . SQL语句运用实例:
--1 建users表
create table users (id int primary key auto_increment,nikename varchar(20) not null unique,password varchar(100) not null,address varchar(200), reg_date timestamp not null default CURRENT_TIMESTAMP);
--2 建articles表,在建表时设置外键
create table articles (id int primary key auto_increment,content longtext not null,userid int,constraint foreign key (userid) references users(id) on delete set null);
-----------------------------------------------------------------------
--2.1 建articles表,建表时不设置外键
create table articles (id int primary key auto_increment,content longtext not null,userid int);
--2.2 给articles表设置外键
alter table articles add constraint foreign key (userid) references users(id) on delete set null;
------------------------------------------------------------------------
--3. 向users表中插入数据,同时插入多条
insert into users (id,nikename,password,address) values (1,'lyh1','1234',null),(10,'lyh22','4321','湖北武汉'),(null,'lyh333','5678','北京海淀');
--4. 向article中插入三条数据
insert into articles (id,content,userid) values (2,'hahahahahaha',11),(null,'xixixixixix',10),(13,'aiaiaiaiaiaiaiaiaiaiaiaia',1),(14,'hohoahaoaoooooooooo',10);
--5. 进行多表查询,选择users表中ID=10的用户发布的所有留言及该用户的所有信息
select articles.id,articles.content,users.* from users,articles where users.id=10 and articles.userid=users.id order by articles.id desc;
--6. 查看数据库引擎类型
show create table users;
--7. 修改数据库引擎类型
alter table users engine=MyISAM; ---因为users表中ID被设置成外键,执行此句会出错
--8. 同表查询,已知一个条件的情况下.查询ID号大于用户lyh1的ID号的所有用户
select a.id,a.nikename,a.address from users a,users b where b.nikename='lyh1' and a.id>b.id;
------也可写成
select id,nikename,address from users where id>(select id from users where nikename='lyh1');
9. 显示年龄比领导还大的员工:
select a.name from users a,users b where a.managerid=b.id and a.age>b.age;
查询编号为2的发帖人: 先查articles表,得到发帖人的编号,再根据编号查users得到的用户名。
接着用关联查询.
select * from articles,users得到笛卡儿积,再加order by articles.id以便观察
使用select * from articles,users where articles.id=2 筛选出2号帖子与每个用户的组合记录
再使用select * from articles,users where articles.id=2 and articles.userid=users.id选出users.id等于2号帖的发帖人id的记录.
只取用户名:select user where user.id=(select userid from articles where article.id =2)
找出年龄比小王还大的人:假设小王是28岁,先想找出年龄大于28的人
select * from users where age>(select age from users where name='xiaowang');
*****要查询的记录需要参照表里面的其他记录:
select a.name from users a,users b where b.name='xiaowang' and a.age>b.age
表里的每个用户都想pk一下.select a.nickname,b.nickname from users a,users b where a.id>b.id ;
更保险的语句:select a.nickname,b.nickname from (select * from users order by id) a,(se
lect * from users order by id) b where a.id>b.id ;
再查询某个人发的所有帖子.
select b.* from articles a , articles b where a.id=2 and a.userid=b.userid
说明: 表之间存在着关系,ER概念的解释,用access中的示例数据库演示表之间的关系.只有innodb引擎才支持foreign key,mysql的任何引擎目前都不支持check约束。

字符集出现错误解决办法

 

出现的问题:
mysql> update users
-> set username='关羽'
-> where userid=2;
ERROR 1366 (HY000): Incorrect string value: '\xB9\xD8\xD3\xF0' for column 'usern
ame' at row 1
向表中插入中文字符时,出现错误。
mysql> select * from users;
+--------+----------+
| userid | username |
+--------+----------+
| 2 | ???? |
| 3 | ???? |
| 4 | ?í?ù |
+--------+----------+
3 rows in set (0.00 sec)
表中的中文字符位乱码。
解决办法:
使用命令:
mysql> status;
--------------
mysql Ver 14.12 Distrib 5.0.45, for Win32 (ia32)
Connection id: 8
Current database: test
Current user: root@localhost
SSL: Not in use
Using delimiter: ;
Server version: 5.0.45-community-nt MySQL Community Edition (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Uptime: 7 hours 39 min 19 sec
Threads: 2 Questions: 174 Slow queries: 0 Opens: 57 Flush tables: 1 Open ta
bles: 1 Queries per second avg: 0.006
--------------
查看mysql发现Server characterset,Db characterset的字符集设成了latin1,所以出现中文乱码。
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| users |
+----------------+
1 row in set (0.00 sec)
更改表的字符集。
mysql> alter table users character set GBK;
Query OK, 3 rows affected (0.08 sec)
Records: 3 Duplicates: 0 Warnings: 0
查看表的结构:

mysql> show create table users;
+-------+-----------------------------------------------------------------------
------------------------------------------------------------------------------+
| Table | Create Table
|
+-------+-----------------------------------------------------------------------
------------------------------------------------------------------------------+
| users | CREATE TABLE `users` (
`userid` int(11) default NULL,
`username` char(20) character set latin1 default NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+-----------------------------------------------------------------------
------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> desc users;
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| userid | int(11) | YES | | NULL | |
| username | char(20) | YES | | NULL | |
+----------+----------+------+-----+---------+-------+
2 rows in set (0.02 sec)
这时向表中插入中文然后有错误。
mysql> insert into users values(88,'中文');
ERROR 1366 (HY000): Incorrect string value: '\xD6\xD0\xCE\xC4' for column 'usern
ame' at row 1
mysql> insert into users values(88,'中文');
ERROR 1366 (HY000): Incorrect string value: '\xD6\xD0\xCE\xC4' for column 'usern
ame' at row 1
还要更改users表的username的字符集。
mysql> alter table users modify username char(20) character set gbk;
ERROR 1366 (HY000): Incorrect string value: '\xC0\xEE\xCB\xC4' for column 'usern
ame' at row 1
因为表中已经有数据,所以更改username字符集的操作没有成***
清空users表中的数据
mysql> truncate table users;
Query OK, 3 rows affected (0.01 sec)
从新更改user表中username的字符集
mysql> alter table users modify username char(20) character set gbk;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
这时再插入中文字符,插入成***。
mysql> insert into users values(88,'中文');
Query OK, 1 row affected (0.01 sec)
mysql> select * from users;
+--------+----------+
| userid | username |
+--------+----------+
| 88 | 中文 |
+--------+----------+
1 row in set (0.00 sec)
mysql>

插入中文报错


进入安装目录下的 my.ini修改配置,
有两个地方需要修改,一个是client端的还有一个是server端的
Sql代码
# CLIENT SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by MySQL client applications.
# Note that only client applications shipped by MySQL are guaranteed
# to read this section. If you want your own MySQL client program to
# honor these values, you need to specify it as an option during the
# MySQL client library initialization.
#
[client]
port=3306
[mysql]
default-character-set=gbk//这个地方
# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
#
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
#Path to installation directory. All paths are usually resolved relative to this.
basedir="C:/Program Files/MySQL/MySQL Server 6.0/"
#Path to the database root
datadir="C:/Program Files/MySQL/MySQL Server 6.0/Data/"
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=gbk//这个地方
# CLIENT SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by MySQL client applications.
# Note that only client applications shipped by MySQL are guaranteed
# to read this section. If you want your own MySQL client program to
# honor these values, you need to specify it as an option during the
# MySQL client library initialization.
#
[client]
port=3306
[mysql]
default-character-set=gbk//这个地方
# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
#
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
#Path to installation directory. All paths are usually resolved relative to this.
basedir="C:/Program Files/MySQL/MySQL Server 6.0/"
#Path to the database root
datadir="C:/Program Files/MySQL/MySQL Server 6.0/Data/"
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=gbk//这个地方
其实这两个地方弄好了,未必就能插入中文,
还要在创建数据库的时候 指定 default charset=gbk
创建表的时候最好也加上。这样保证你百分百不会在
报错了。





本页地址: [复制地址]
返回顶部 关闭(快捷键alt+C)
评论统计(0条)| 我要评论
暂无评论内容!
我要评论 
我要评论: 带*部分需要填写
 姓名称呼: * 请填写您的姓名或呢称
联系方式: QQ,MSN,Email都可以,方便交流 (仅管理员可见)
 评论内容: * 不超过100字符,50汉字
验证码:
    
  推荐链接
  最近更新  
·Host 'XXX' is not allowed...
·Win2008或IIS7的文件上传大...
·IIS7.0上传文件限制的解决方...
·测试信息2015-03-11
·asp.net中处理图片
·ASP.NET之Web打印-终极解决...
·Asp.net下C#调用Word模版实...
·asp.net下将页面内容导入到...
·asp.net导出为pdf文件
·asp.net生成pdf文件
·FCKeditor 文本编辑器的使用...
·ASP.NET 将数据生成PDF
·asp.net2.0导出pdf文件完美...
·AspJpeg的安装与测试
·JS验证浏览器版本对IE11的支...
  热门浏览  
·IE8和IE9出现“此网页上的问...
·无线路由器密码破解,教你断...
·js替换所有回车换行符
·QQ/MSN在线交流代码
·IE弹出“中国工商银行防钓鱼...
·如何取消键盘上的一些快捷键...
·win7声音小的解决方法
·webdav漏洞的利用
·强制两端对齐的函数或者CSS...
·win7下成功安装sql server ...
·显示器分辨率调的过高导致电...
·天诺时空技术技术论坛
·js验证手机号码格式
·JS展开和收缩效果(二)
·本地计算机上的 MSSQLSERVE...
  碧海澜涛居
网站首页关于本站站长简介开发案例技术资料美文日志摄影相册读书收藏影视收藏留言板
版权所有:碧海澜涛 QQ:410436434 Email:shaopo_guo@163.com 苏ICP备15000526号
免责声明:本站为个人网站,站内所有文字、图片等各类资料均为个人兴趣爱好所收集,不用作任何商业用途,亦不保证资料的真实性,若有因浏览本站内容而导致的各类纠纷,本站也不承担任何责任。本站部分内容来自互联网,如有涉及到您的权益或隐私请联系站长解决。