MySql入门学习笔记

 

1.在PATH中添加了环境变量值:D:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;%JAVA_HOME%\bin ;D:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin;D:\Program Files\Microsoft Visual Studio\Common\Tools;D:\Program Files\Microsoft Visual Studio\VC98\bin;D:\软件中心\JDK安装位置\bin;C:\Windows\System32;D:\Program Files\MySQL\MySQL Server 5.6\bin;D:\Program Files\MySQL\MySQL Server 5.5\bin
 
2.命令
 
net start MySQL      --启动MySQL服务的命令行方式
net stop MySQL      --关闭本地数据库服务
 
mysql -uroot -p(mysql -hlocalhost -uroot -p)      --以root权限连接数据库,不需用户名
 
 
quit ;(或exit;)                        --用户退出
 
 
mysql --user root --password               --另外一种登录方式
或者:mysql --host localhost --user root --password              --        mysql --host [主机] --user root --password,主机默认在本地,因此不加主机地址就变成了上面的形式
3.sql语句:
show databases;  (show databases\g )                --输出已经建立的数据库
desc 【表名】;                        --显示表结构
 
为新建的数据库建立一个账号(用户)拥有对该数据苦的所有权限:
grant all on dbtest.* to "neo"@"localhost" identified by "truman";                      ---用户名:neo 密码:truman
注:如果用户已经存在那么红色部分就应该去掉,因为会覆盖原来用户的密码;蓝色部分值该数据库所有的表
 
 
mysqldump -uroot -p dbtest>e:/a.sql
备份失败!暂时不知道原因!
 
 mysql -uroot -p【数据库名】<【绝对路径】             ----在控制台调入外部sql文件到指定数据库下执行
 
 
source e:/a.sql                            ----在mysql下读取执行外部sql脚本
 
show tables;                              --显示当前数据库中表的信息
 
create database 【数据库名】default character set utf8;                     ----------创建库的同时设置默认的字符集(红色的部分设置字符集,也可以在建表或设置字段时设置表的默认字符集)
注:字符集的应用情况采用就近原则!
 
 
alter table 【表名】 modigy 【列名】【类型】;                 ---------------------------如alter table g modify price decimal(6,2);
               
 
insert into g(price) values('300.89');                    -------------插入数据
 
select version();                       ---------------显示版本信息
select database();                 --------------------显示当前数据库
set @x=2;                     -------------设置一个变量值为2
select @a*100;            --------------输出一个变量*100的值
 
select * from stu where sname='李%';                              --------------检索出姓李的人的信息(模糊查询)
 
select *from stu where sname like '%李%';               ---------------------查询sname字段中含有李的信息
 
select if(sex,"男","女"),sname from stu;                       -----------------------sql中的过程判断语言
 
 
select if(sex,'男','女') as stusex,sname from stu;                      ---------取别名,as可省略
 
select * from stu where sname like "%李%" and sex="0";                --------------and 且
 
 
 
select concat("姓名:",sname," 性别:",if(sex,"男","女")," qq:",qq) as 连接字符串 from stu;             -----------------连接字符串
 
alter table stu add birthday date;                   -----------追加一个字段到表中
update stu set birthday="1990/2/23";           -----------为所有stu的barth初始化一个值
select * from stu limit 2;                                 ------------限制拿到两条天数据
 
select * from stu order by id desc limit 3;                  ----------按照id由高到低排序显示(asc有低到高升序)
 
练习:
1.查找年龄最大的人
select * from stu order by birthday asc limit 1;
2.查找年龄第二大的学生信息
select * from stu order by birthday asc limit 1,1;           -------------从第2条数据取,包含其实位置那一条共取1条(第n条记录小时的标号是n-1)
3.查找年龄最大的两个人可以有重复
select *from stu  where birthday <=(select birthday from stu order by birthday asc limit 1,1);        --------用子查询(asc可以不写,默认的)
4.查找学生都是哪一年生的
select  distinct year(birthday) from stu;              -----------------distinct 完成去除重复的工作
show character set;                       -----显示mysql支持的字符集
 
show create table demo1;                     ----------------------显示表字符集情况
 
select length(name),length(name1) from demo1;                     ------------------length()读字节数
 
select char_length(name),char_length(name2) from demo1;    -------------------char_length()读字符数
 
show collation;                            ---------显示字符集校对规则
 
create table demo2(name1 varchar(30) character set utf8 collate utf_bin,name2 varchar(30) character set utf8 collate utf8_general_ci);        ----校对规则不同
 
 
create table demo3(name1 binary(3),name2 char(3)) default character set utf8;
show create table demo3;
insert into demo3(name1,name2)values('a','a'),('中','中'),('中r','中r'),('中国','中国'),('中国人民','中国人民');
select *from demo3;
 
 
 
show variables like "%character%" ;                ----输出数据库跟当前跟字符集相关的环境变量
 
 
有关字符集的一些问题:
1.客户端(命令行客户端、可视化界面客户端、jdbc方式等)发送脚本
2.字符集转换:
客户端发送sql脚本的字符集->连接字符集(和脚本字符集一致)->数据库想用字段字符集
3.进行匹配。
 
以命令行客户端为例:
1.原来是gbk,我们改成utf8:
set names utf8;               ----相当于设置了下图高亮的三个环境变量,但实际上面客户端的属性中的字符集并没有变化,结果是服务器会被欺骗!
注:这种设置方法是不被推荐的!建议分开来设置!
set character_set_client='utf8';
set character_set_connection='utf8';
set character_set_results='utf8';
 
 
修改前:
修改后:
 
PL/SQL部分
 
posted @ 2014-02-21 16:52  Neuromancer  阅读(277)  评论(0编辑  收藏  举报