Chinaunix首页 | 论坛 | 博客
  • 博客访问: 492841
  • 博文数量: 71
  • 博客积分: 1332
  • 博客等级: 少尉
  • 技术积分: 772
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-10 16:25
个人简介

文章分类

全部博文(71)

文章存档

2013年(19)

2012年(9)

2011年(43)

分类: Mysql/postgreSQL

2012-03-03 21:32:32

参考书:《mysql开发者 sql权威指南》

# 创建用户和设置密码

  1. create user 'booksql'@'localhost' identified by 'booksql';

  2. create user 'redhat'@'%' identified by 'redhat';

# 授权 所有权限

  1. grant all privileges on *.* to 'redhat'@'%' with grant option;

  2. grant all privileges on *.* to 'booksql'@'localhost' with grant option;

  3. grant select,update on number_sets to booksql@'localhost';

  4. show grants for booksql@'localhost';

# 产看用户权限

  1. show grants;

  2. show grants for redhat;

  3. show grants for booksql@localhost;
# 简单命令

  1. show databases;
  2. use mysql;
  3. show tables;
  4. select * from user;
  5. create database tennis;
  6. use tennis;
  7. show tables;

# 创建数据库

  1. create table if not exists players(
  2.     playerno integer not null,
  3.     name char(15) not null,
  4.     initials char(3) not null,
  5.     birth_date date ,
  6.     *** char(1) not null,
  7.     joined smallint not null,
  8.     street varchar(30) not null,
  9.     houseno char(4) ,
  10.     postcode char(6) ,
  11.     town varchar(30) not null,
  12.     phoneno char(13) ,
  13.     leagueno char(4) ,
  14.     primary key (playerno)
  15. );

  16. drop table if exists team;

  17. create table if not exists team(
  18.     teamno integer not null,
  19.     playerno integer not null,
  20.     division char(6) not null,
  21.     primary key (teamno)
  22. );

  23. alter table team rename as teams;

  24. create table if not exists matches(
  25.     matchno integer not null,
  26.     teamno integer not null,
  27.     playerno integer not null,
  28.     won smallint not null,
  29.     lost smallint not null,
  30.     primary key (matchno)
  31. );

  32. create table if not exists penalties(
  33.     paymentno integer not null,
  34.     playerno integer not null,
  35.     payment_date date not null,
  36.     amount decimal(7,2) not null,
  37.     primary key (paymentno)
  38. );

  39. create table if not exists committee_members(
  40.     playerno integer not null,
  41.     begin_date date not null,
  42.     end_date date ,
  43.     position char(20) ,
  44.     primary key (playerno, begin_date)
  45. );

  46. insert into players values(
  47.     6,'Parmenter','R','1964-06-25','M',1977,
  48.     'Haseltine Line','80','1234KK','Stratford',
  49.     '070-476537','8467'
  50. );

  51. insert into players values(
  52.     7,'Wise','GWS','1963-05-11','M',1981,
  53.     'Edgecombe Way','39','9758VB','Stratford',
  54.     '070-346837',Null
  55. );

  56. insert into teams values(
  57.     1,6,'first'
  58. );

  59. insert into teams values(
  60.     2,27,'second'
  61. );


  62. insert into matches values(
  63.     1,1,6,3,1
  64. );
  65. insert into matches values(
  66.     4,1,44,3,2
  67. );



  68. insert into penalties values(
  69.     1,6,'1980-12-08',100
  70. );
  71. insert into penalties values(
  72.     2,44,'1981-05-05',75
  73. );

  74. insert into committee_members values(
  75.     6,'1990-01-01','1990-12-31','Secretary'
  76. );
  77. insert into committee_members values(
  78.     6,'1991-01-01','1992-12-31','Member'
  79. );
  80. use tennis;

  81. show tables;
  82. select * from committee_members;
  83. select * from matches;
  84. select * from penalties;
  85. select * from players;
  86. select * from teams;


  87. select playerno,name,birth_date
  88. from players
  89. where town='Stratford'
  90. order by name;

  91. select playerno
  92. from players
  93. where joined > 1970
  94. order by playerno;

  95. select *
  96. from penalties
  97. order by paymentno,amount,playerno;

  98. insert into penalties values(
  99.     2,44,'1981-05-05',166
  100. );

  101. update penalties
  102. set amount = 77
  103. where playerno = 44 and paymentno = 2;

  104. select *
  105. from penalties
  106. where playerno = 44
  107. order by paymentno,amount;

  108. update penalties
  109. set amount = 110
  110. #where playerno = 44; 执行不成功,需要关闭安全选项
  111. # Edit --> Preferences --> SQL Edit ( Safe updates(选项)) --> reconnect
  112. where playerno = 44 and paymentno = 2;

  113. delete
  114. from penalties
  115. where amount > 100;

# 比较索引,通过查看时间;如果数据内容很多,可以看出效率

  1. select * from penalties where amount > 100;

  2. create index penalties_amount on penalties(amount);

  3. select * from penalties where amount > 100;


# 创建视图,并没有包含真正的数据,不占用空间;

  1. select * from matches;

  2. select matchno,abs(won - lost) from matches;

  3. create view number_sets(matchno,difference) as
  4. select matchno,abs( won - lost )
  5. from matches;

  6. select * from number_sets;

# 删除database、table、view、index

  1. drop database db_name;

  2. drop table tb_name;

  3. drop view view_name;

  4. drop index index_name;

# 系统变量

  1. select @@version;
  2. '5.5.13'

  1. select @@sql_mode;
  2. 'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

# 设置系统变量 set
# set @@sql_mode = 'pipes_as_concat';
# 使用一条常规的set语句会覆盖掉所有的设置;
# 如果想要添加设置,可以使用如下语句:

  1. set @@sql_mode = concat(@@sql_mode,
  2.                     CASE @@sql_mode WHEN '"' then '"' ELSE ',' end,
  3.                     'no_zero_in_date'
  4.                     );
# MySQL中concat函数
# http://axislover.blog.163.com/blog/static/10776515200891551236219/
# 使用方法:
# CONCAT(str1,str2,…)  
# 返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。
# 注意:
# 如果所有参数均为非二进制字符串,则结果为非二进制字符串。
# 如果自变量中含有任一二进制字符串,则结果为一个二进制字符串。
# 一个数字参数被转化为与之相等的二进制字符串格式;若要避免这种情况,可使用显式类型 cast, 例如:
# SELECT CONCAT(CAST(int_col AS CHAR), char_col)
# 举例:
  1. select concat('11','22','33');
  2. 112233

  3. select concat('11','22','null');
  4. 1122null

  5. select concat('11','22',null);
  6. null

# case when 使用方法举例

  1. SELECT CASE
  2. WHEN 10*2=30 THEN '30 correct'
  3. WHEN 10*2=40 THEN '40 correct'
  4. ELSE 'Should be 10*2=20'
  5. END;

  6. SELECT CASE 10*2
  7. WHEN 20 THEN '20 correct'
  8. WHEN 30 THEN '30 correct'
  9. WHEN 40 THEN '40 correct'
  10. END;

  11. select case @@sql_mode
  12. when '"' then '"'
  13. when '\'' then '.


# 查看表结构

  1. show columns from players;

  2. desc players;

# 查看索引

  1. show index from penalties;

阅读(1872) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~