Upgrade to Pro — share decks privately, control downloads, hide ads and more …

rails中mysql应用

Avatar for scottxu scottxu
July 29, 2014

 rails中mysql应用

地图汇内部培训,rails中的mysql、sql命令等用法。

Avatar for scottxu

scottxu

July 29, 2014

More Decks by scottxu

Other Decks in Technology

Transcript

  1. 4 database/table • mysql> create database dituhui; • create table

    <表名> (<字段名1> <类型1>, (< 字段名2> <类型2>); mysql> create table users(user_id int(4) not null primary key auto_increment,map_id char(20) not null);
  2. 5 record • INSERT INTO table_name (列1, 列2,...) VALUES (值1,

    值2,....) mysql> insert into users (user_id,map_id) values (10000,10000);
  3. 7 alter table • ALTER TABLE用来创建普通索引、UNIQUE索引或 PRIMARY KEY索引 • ALTER

    TABLE table_name ADD INDEX index_name (column_list) mysql>alter table users add index user_id_index(user_id);
  4. 8 alter table • ALTER TABLE table_name ADD UNIQUE (column_list)

    • ALTER TABLE table_name ADD PRIMARY KEY (column_list)
  5. 9 create index • CREATE INDEX index_name ON table_name (column_list)

    • CREATE UNIQUE INDEX index_name ON table_name (column_list)
  6. 13 table • ALTER TABLE `dituhui`.`mapbean_kinds` MODIFY COLUMN `description` VARCHAR(255)

    CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL; • ALTER TABLE users RENAME column map_id to maps_id ;
  7. 14 record • UPDATE 表名称 SET 列名称 = 新值 WHERE

    列名称 = 某值 mysql>update user set user_id=10001 where map_id=10000;
  8. 19 record(基础查询) • select * from users; • select user_id

    from users where map_id=10000; • select user_id from users where map_id >10000 order by id desc limit 10; • select count(*) from users where map_id>10000;
  9. 26 index • DROP INDEX index_name ON talbe_name • ALTER

    TABLE table_name DROP INDEX index_name • ALTER TABLE table_name DROP PRIMARY KEY(不需要指定索引名称,只用一个主键)
  10. 28 like和通配符 mysql>select * from users where email like “[email protected]”

    mysql>select * from users where email like “%@gmail.com” mysql>select * from users where email like “[email protected]”
  11. 32 join • INNER JOIN : 如果表中有至少一个匹配, 则返回行 • LEFT

    JOIN: 即使右表中没有匹配,也从左 表返回所有的行 • RIGHT JOIN: 即使左表中没有匹配,也从 右表返回所有的行 • FULL JOIN: 只要其中一个表中存在匹配, 就返回行
  12. 35 inner、left的区别 • INNER JOIN: 如果表中有至少一个匹配, 则返回行。(不匹配的不显示) • LEFT JOIN:

    即使右表中没有匹配,也从左 表返回所有的。(以左表作为基准,右表能匹配上 的就显示出来,不匹配的就显示成NULL,记录个数满足左 表)
  13. 36 alter • 1:删除列 ALTER TABLE 【表名字】 DROP 【列名称】 •

    2:增加列 ALTER TABLE 【表名字】 ADD 【列名称】 【类型等】 • 3:修改列 ALTER TABLE 【表名字】 CHANGE 【列名称】【新列名称】【类型等】 • 5:重命名表 ALTER TABLE 【表名字】 RENAME 【表新名字】 • 6:删除表中主键 ALTER TABLE 【表名字】 drop primary key; • 8:添加索引 ALTER TABLE 【表名字】ADD INDEX 【索引名】; • 10: 删除索引 ALTER table 【表名字】 DROP INDEX 【索引名】;
  14. 37 rails中的sql where select group order reorder reverse_order limit offset

    joins includes lock readonly from having find …
  15. 38 find/find_by_* • user=User.find(1000) mysql> SELECT * FROM usersWHERE (users.id

    = 10000) LIMIT 1 • user=User.find_by_nickname(“scottxu”) mysql> SELECT * FROM usersWHERE (users.nickname = ‘scottxu’) LIMIT 1
  16. 40 first/last • user=User.first mysql> select * from users limit

    1; • user=User.last mysql>select * from users order by id desc limit 1;
  17. 41 where • User.where(:created_at => (params[:start_date].to_date)..(params[:end_da te].to_date)) mysql> SELECT “users".*

    FROM “users" WHERE (“users"."created_at" BETWEEN '2014-06-29' AND '2014-07-10')
  18. 42 order • User.order(“created_at”) mysql>select * from users order by

    users.created_at asc; • User.order(“created_at desc”) mysql>select * from users order by users.created_at desc; • User.order(“created_at”). reverse_order
  19. 46 joins • Map.joins(:permission).where(“maps.rating>0 and permissions.name=‘pub’ ”) mysql>select * from

    maps inner join permissions where maps.rating>0 and permissions.name=‘pub’ and maps.permission_id=permissions.id
  20. 47 joins nested • Category.joins(:posts => [{:comments => :guest}, :tags])

    SELECT categories.* FROM categories INNER JOIN posts ON posts.category_id = categories.id INNER JOIN comments ON comments.post_id = posts.id INNER JOIN guests ON guests.comment_id = comments.id INNER JOIN tags ON tags.post_id = posts.id
  21. 53 rails migration create_table add_column add_index add_timestamps change_column change_table …

    drop_table remove_column remove_index rename_column remove_timestamps
  22. 54 create_table • rails generate model Map title:string description:text =>

    class CreateMaps < ActiveRecord::Migration def change create_table :mapsdo |t| t.string :title t.text :description t.timestamps end end end
  23. 56 add_column • rails generate migration add_description_to_geodata description:text => class

    AddDescriptionToGeodata < ActiveRecord::Migration def change add_column :geodata, :description, :text end end
  24. 59

  25. 60