mysql的學習記錄

來源:互聯網
上載者:User

標籤:

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的學習記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.