码迷,mamicode.com
首页 > 数据库 > 详细

mysql的学习记录

时间:2016-02-20 19:03:11      阅读:409      评论:0      收藏:0      [点我收藏+]

标签:

1 MySQL -h localhost -u UserName -p Password
-h不写,默认为localhost
注意:最好先MySQL -h localhost -u UserName -p 然后按enter建

2查看有哪些数据库
 show databases;

3创建数据库
create database php;
(再通过show databases就可以查看了)

4.选中数据库
user php(可以不用加分号,)

5.查看多少表
show tables;

6.删除php数据库
drop database php;

7能否修改数据库的名字,答案是不可以的
8 create table class(stu int,name varchar(20),age int);

create table class(
id int primary key auto_increment,
name varchar(10),
age tinyint
) charset utf-8;

9删除表
 drop table class

10修改表名字(注意数据库名字是不可以修改的)
rename table class to newclass;

11.查看所有的列信息
desc class;

12.当输错的的时候,可以用\c退出,也可以按enter建报错后退出

13 .插入表中
insert into msg(id,title,name,content)
values
(1,‘初来乍到‘,‘张三‘,‘刚来能不能当老大‘);

报错,字符集问题导致的错误
解决方法:默认建表一般用utf8,而我们在windows窗口是gbk的,因此要声明字符集
只要在插入前面加 set names gbk就可以插入成功你了

注意:刻意多条一次性插入,只要在values后面多加几个,之间用逗号分隔

13修改表内容
update msg set id=2 ,content=‘偏要当老大‘ where name=‘李四‘;

14删除表内容
delete from msg where id=2;

15:表的列类型学习

mysql三大列类型
   数值型
    整型
        Tinyint/smallint/mediumint/int/bigint
    小数型
        Float(D,M),decimal(D,M)
   字符串型
    char(M)
    varchar(M)
    Text文本类型
   日期时间类型
    date 日期
        time 时间
        datetime 时间类型
        year 年类型

Tinyint 微小的列类型,1字节(8个位) 0->2^8-1=255
       如果表示负数,可以用最高位来标志符号位
    这时,表示范围是-128->127
smallint 2字节
mediumint 3字节
int 4字节
bigint 8字节

用tinyint做列子:
Tinyint 默认是有符号的存储
    tinyint(m) unsigned zerofill
    其中 m:宽度(在0填充即zerofill的时候才有意义),如果插入的值是1而且m是5,则显示的时候000001;
            (注意:m影响的只是显示效果,没有超过指定的宽度,前面就用0填充,超过的话,就不起作用了)
         unsigned:无符号类型(非负),影响存储范围
         zerofill:0 填充,(默认无符号)
alter table class add age2 tinyint unsigned;
alter table class add age3 tinyint(1);
alter table class add age3 tinyint(5) unsigned zerofill;(其中unsigned 默认有,可省去)
alter table class add age4 notnull default 0;

16 字符串型
    char:定长字符串,char(m),m表示宽度,可以容纳的字符数(当utf-8时,一个中文字也表示一个字符)
    varchar 变长字符串 varchar(m),同上
     text 文本串
    区别在哪里?
    a:char定长m,   实存i个字符,实占空间 M字符(当然i<=m)
    varchar定长m,实存i个字符,实占空间 i字符+(1或2)字节

     b:  char(M)   0<=M<=255
       varchar(M)   utf-8时,0<=M<=22000 ,scii  ,0<=M<=65536

     c:char(m) 当实际存储内容不足m个是,则后面加空格补齐,但是取出来的时候,再把后面的空格去掉

    char和varchar型的选择原则    
      a:空间利用率
         四字成语表则用char(4),个人简介、微博 140字,则用varchar(140)
      b: 速度
         用户名用char,char的速度快,而且字符浪费不会很多
    create table stu(name char(8) not null default ‘‘,
    waihao varchar(10) not null default)charset utf8;

        
alter table stu add intro text not null default ‘my info‘;报错:text、blog没有默认值的设置
alter table stu add intro text not null ;就对了

17日期类型(四种类型)
年->year
年-月-日->date
09:00:00 ->time
年-月-日 hh:mm:ss->datetime


年->year  1个字节 表示 1901-2155,
create table  y(ya year(4));
如果输入2位:‘00-69 表示2000-2069  70->99表示1970-1999 最简单的是,4位全部输入

年-月-日->date 典型 1992-08-12
    范围:‘1000-010-01‘->‘9999-12-31‘

hh:mm:ss ->time  
    范围:‘-838:59:59‘->‘838:59:59‘

年-月-日 hh:mm:ss->datetime
    范围:‘1000-01-01 00:00:00‘->‘9999-12-31 23:59:59‘

注意:在开发中,很少用日期时间类型来表示一个需要精确到秒的列,
       原因是:虽然日期时间类型能精确到秒,而且方便查看,但是计算不方便
但是都用时间戳来表示精确到秒

时间戳 :是1970-01-01 00:00:00 到当前的秒数
    一般存注册时间,商品发布时间等,并不是用datetime存储,而是用时间戳,因为datetime计算不方便
    而且时间戳的表示可以方便格式的选择显示

create table t2(
    gender tinyint,--可以填很多值,而且只有一个字节
    gender enum(‘男‘,‘女‘) --枚举类型,缺点只能填男或者女,字节比tinyint用的多,所以建议用tinyint,
)charset utf8;


18:建表练习
姓名:char(3)
年龄:tinyint unsigned
email:varchar(30)
tel:char(11)
intro(简介):varchar(1000)
alary:decimal(7,2)
入学日期:date类型

create table stu(
    id int primary key auto_increment,
    name char(3) not null default ‘‘,
    age tinyint unsigned not null default 0,
    email varchar(30) not null default ‘‘,
    tel char(11) not null default ‘‘,
    salary decimal(7,2) not null default ‘1800.68‘,
    riqi date not null default ‘2012-03-13‘    
)charset utf8;

19 select 5种子句介绍
    where 条件查询
    group by 分组
    having 刷选
    order by 排序
    limit 限制结果条数




mysql的学习记录

标签:

原文地址:http://www.cnblogs.com/ChineseMoonGod/p/5203704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!