无限星辰工作室-客户无限互联网动力之源

标题: Mysql语句实例+练习分享 [打印本页]

作者: crx349    时间: 2013-5-16 14:07
标题: Mysql语句实例+练习分享


  1. mysql> select 10;
  2. +----+
  3. | 10 |
  4. +----+
  5. | 10 |
  6. +----+
  7. 1 row in set (0.00 sec)

  8. mysql> select 10+20+30;
  9. +----------+
  10. | 10+20+30 |
  11. +----------+
  12. |       60 |
  13. +----------+
  14. 1 row in set (0.02 sec)

  15. mysql> select "aa"+"bb";
  16. +-----------+
  17. | "aa"+"bb" |
  18. +-----------+
  19. |         0 |
  20. +-----------+
  21. 1 row in set, 2 warnings (0.00 sec)

  22. mysql> select "10a"+"bb";
  23. +------------+
  24. | "10a"+"bb" |
  25. +------------+
  26. |         10 |
  27. +------------+
  28. 1 row in set, 2 warnings (0.00 sec)

  29. mysql> select "10a"+"bb20";
  30. +--------------+
  31. | "10a"+"bb20" |
  32. +--------------+
  33. |           10 |
  34. +--------------+
  35. 1 row in set, 2 warnings (0.00 sec)

  36. mysql>
  37. mysql> select 10*"2a";
  38. +---------+
  39. | 10*"2a" |
  40. +---------+
  41. |      20 |
  42. +---------+
  43. 1 row in set, 1 warning (0.00 sec)

  44. mysql> select "3a"+"4b";
  45. +-----------+
  46. | "3a"+"4b" |
  47. +-----------+
  48. |         7 |
  49. +-----------+
  50. 1 row in set, 2 warnings (0.00 sec)

  51. mysql> select "3a"+"4e2b";
  52. +-------------+
  53. | "3a"+"4e2b" |
  54. +-------------+
  55. |         403 |
  56. +-------------+
  57. 1 row in set, 2 warnings (0.00 sec)

  58. mysql> select "3a"+"4.23b";
  59. +--------------+
  60. | "3a"+"4.23b" |
  61. +--------------+
  62. |         7.23 |
  63. +--------------+
  64. 1 row in set, 2 warnings (0.00 sec)

  65. mysql> select 12=20;
  66. +-------+
  67. | 12=20 |
  68. +-------+
  69. |     0 |
  70. +-------+
  71. 1 row in set (0.00 sec)

  72. mysql> select 0="abc";
  73. +---------+
  74. | 0="abc" |
  75. +---------+
  76. |       1 |
  77. +---------+
  78. 1 row in set, 1 warning (0.00 sec)

  79. mysql> select "10"="10a";
  80. +------------+
  81. | "10"="10a" |
  82. +------------+
  83. |          0 |
  84. +------------+
  85. 1 row in set (0.00 sec)

  86. mysql> select 10>=1 and 10<=20;
  87. +------------------+
  88. | 10>=1 and 10<=20 |
  89. +------------------+
  90. |                1 |
  91. +------------------+
  92. 1 row in set (0.00 sec)

  93. mysql> select 10 between 1 and 20;
  94. +---------------------+
  95. | 10 between 1 and 20 |
  96. +---------------------+
  97. |                   1 |
  98. +---------------------+
  99. 1 row in set (0.00 sec)
复制代码

作者: crx349    时间: 2013-5-16 14:08

  1. mysql> select 1<<3;
  2. +------+
  3. | 1<<3 |
  4. +------+
  5. |    8 |
  6. +------+
  7. 1 row in set (0.02 sec)

  8. mysql> select 1<<10;
  9. +-------+
  10. | 1<<10 |
  11. +-------+
  12. |  1024 |
  13. +-------+
  14. 1 row in set (0.00 sec)

  15. mysql> select 1<<20;
  16. +---------+
  17. | 1<<20   |
  18. +---------+
  19. | 1048576 |
  20. +---------+
  21. 1 row in set (0.00 sec)

  22. mysql> select 1|2;
  23. +-----+
  24. | 1|2 |
  25. +-----+
  26. |   3 |
  27. +-----+
  28. 1 row in set (0.00 sec)

  29. mysql> select 1&2;
  30. +-----+
  31. | 1&2 |
  32. +-----+
  33. |   0 |
  34. +-----+
  35. 1 row in set (0.00 sec)

  36. mysql> select 5&6;
  37. +-----+
  38. | 5&6 |
  39. +-----+
  40. |   4 |
  41. +-----+
  42. 1 row in set (0.00 sec)



  43. --创建数据库
  44. mysql> create database fooknet;
  45. Query OK, 1 row affected (0.00 sec)

  46. --查看当前用户可以看到所有数据库
  47. mysql> show databases;
  48. +----------------------+
  49. | Database             |
  50. +----------------------+
  51. | information_schema   |
  52. | test                 |
  53. | mysql                |
  54. | fooknet               |
  55. +----------------------+
  56. 83 rows in set (0.75 sec)

  57. --删除数据库
  58. mysql> drop database fooknet;
  59. Query OK, 0 rows affected (0.03 sec)

  60. mysql> create database fooknet;
  61. Query OK, 1 row affected (0.00 sec)

  62. mysql>

  63. -- 选择进入fooknet数据库
  64. mysql> use fooknet;
  65. Database changed

  66. --查看当前你在哪个数据库下
  67. mysql> select database();
  68. +------------+
  69. | database() |
  70. +------------+
  71. | fooknet     |
  72. +------------+
  73. 1 row in set (0.00 sec)

  74. --查看当前数据库中的所有表格。
  75. mysql> show tables;
  76. Empty set (0.02 sec)

  77. mysql>
  78. --创建留言表格
  79. mysql> create table liuyan(
  80.     -> id int unsigned not null auto_increment primary key,
  81.     -> title varchar(32) not null,
  82.     -> author varchar(32) not null default 'wu',
  83.     -> addtime datetime,
  84.     -> content text not null);
  85. Query OK, 0 rows affected (0.08 sec)
  86. --id 整型 无负号 非空 自增 主键(非空,唯一性、索引列)
  87. --title 字串(长度32位) 非空
  88. --author 字串(长度32位) 非空 默认值 “wu”


  89. --查看留言表结构
  90. mysql> desc liuyan;
  91. +---------+------------------+------+-----+---------+----------------+
  92. | Field   | Type             | Null | Key | Default | Extra          |
  93. +---------+------------------+------+-----+---------+----------------+
  94. | id      | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
  95. | title   | varchar(32)      | NO   |     | NULL    |                |
  96. | author  | varchar(32)      | NO   |     | wu      |                |
  97. | addtime | datetime         | YES  |     | NULL    |                |
  98. | content | text             | NO   |     | NULL    |                |
  99. +---------+------------------+------+-----+---------+----------------+
  100. 5 rows in set (0.02 sec)

  101. --以文本方式输出留言表的建表语句
  102. mysql> show create table liuyan \G;
  103. *************************** 1. row ***************************
  104.        Table: liuyan
  105. Create Table: CREATE TABLE `liuyan` (
  106.   `id` int(10) unsigned NOT NULL auto_increment,
  107.   `title` varchar(32) NOT NULL,
  108.   `author` varchar(32) NOT NULL default 'wu',
  109.   `addtime` datetime default NULL,
  110.   `content` text NOT NULL,
  111.   PRIMARY KEY  (`id`)
  112. ) ENGINE=MyISAM DEFAULT CHARSET=utf8
  113. 1 row in set (0.00 sec)

  114. ERROR:
  115. No query specified

  116. mysql>
  117. mysql>
  118. --查看当前数据库中表格
  119. mysql> show tables;
  120. +------------------+
  121. | Tables_in_fooknet |
  122. +------------------+
  123. | liuyan           |
  124. +------------------+
  125. 1 row in set (0.00 sec)

  126. mysql>
  127. --删除表格liuyan
  128. mysql> drop table liuyan;
  129. Query OK, 0 rows affected (0.00 sec)

  130. mysql>  CREATE TABLE `liuyan` (
  131.     ->   `id` int(10) unsigned NOT NULL auto_increment,
  132.     ->   `title` varchar(32) NOT NULL,
  133.     ->   `author` varchar(32) NOT NULL default 'wu',
  134.     ->   `addtime` datetime default NULL,
  135.     ->   `content` text NOT NULL,
  136.     ->   PRIMARY KEY  (`id`)
  137.     -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  138. Query OK, 0 rows affected (0.05 sec)

  139. mysql>

  140. --添加一个测试表格
  141. mysql> create table aa(id int not null);
  142. Query OK, 0 rows affected (0.05 sec)

  143. --查看当前数据库下的表格
  144. mysql> show tables;
  145. +------------------+
  146. | Tables_in_fooknet |
  147. +------------------+
  148. | aa               |
  149. | liuyan           |
  150. +------------------+
  151. 2 rows in set (0.00 sec)

  152. --删除一个不存在的表格
  153. mysql> drop table bb;
  154. ERROR 1051 (42S02): Unknown table 'bb'
  155. --删除不存在的bb表,不报错
  156. mysql> drop table if exists bb;
  157. Query OK, 0 rows affected, 1 warning (0.00 sec)

  158. mysql> drop table if exists aa;
  159. Query OK, 0 rows affected (0.00 sec)

  160. mysql> drop table if exists aa;
  161. Query OK, 0 rows affected, 1 warning (0.00 sec)

  162. mysql>
  163. mysql>
  164. --查看表结构
  165. mysql> desc liuyan;
  166. +---------+------------------+------+-----+---------+----------------+
  167. | Field   | Type             | Null | Key | Default | Extra          |
  168. +---------+------------------+------+-----+---------+----------------+
  169. | id      | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
  170. | title   | varchar(32)      | NO   |     | NULL    |                |
  171. | author  | varchar(32)      | NO   |     | wu      |                |
  172. | addtime | datetime         | YES  |     | NULL    |                |
  173. | content | text             | NO   |     | NULL    |                |
  174. +---------+------------------+------+-----+---------+----------------+
  175. 5 rows in set (0.02 sec)

  176. --采用标准方式添加一条数据
  177. mysql> insert into liuyan(id,title,author,addtime,content)
  178.     -> values(1,'news','zhangsan','2012-05-22 12:32:50','hello mysql');
  179. Query OK, 1 row affected (0.01 sec)

  180. --查看留言表中的所有数据。(*表示所有列)
  181. mysql> select * from liuyan;
  182. +----+-------+----------+---------------------+-------------+
  183. | id | title | author   | addtime             | content     |
  184. +----+-------+----------+---------------------+-------------+
  185. |  1 | news  | zhangsan | 2012-05-22 12:32:50 | hello mysql |
  186. +----+-------+----------+---------------------+-------------+
  187. 1 row in set (0.00 sec)

  188. mysql>
  189. --添加数据(字段名的顺序可以颠倒)
  190. mysql> insert into liuyan(id,content,addtime,title,author)
  191.     -> values(2,'aaaaa','2012-12-24 12:12:12','info','lisi');
  192. Query OK, 1 row affected (0.00 sec)

  193. --只指定部分字段名来添加数据
  194. mysql> insert into liuyan(title,addtime,content)
  195.     -> values('test','2012-12-22','hello');
  196. Query OK, 1 row affected (0.00 sec)

  197. mysql> desc liuyan;
  198. +---------+------------------+------+-----+---------+----------------+
  199. | Field   | Type             | Null | Key | Default | Extra          |
  200. +---------+------------------+------+-----+---------+----------------+
  201. | id      | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
  202. | title   | varchar(32)      | NO   |     | NULL    |                |
  203. | author  | varchar(32)      | NO   |     | wu      |                |
  204. | addtime | datetime         | YES  |     | NULL    |                |
  205. | content | text             | NO   |     | NULL    |                |
  206. +---------+------------------+------+-----+---------+----------------+
  207. 5 rows in set (0.00 sec)

  208. --不指定字段名添加数据(values中要给所有字段赋值,并顺序一致)
  209. mysql> insert into liuyan
  210.         -> values(null,'demo','wangwu','2012-09-10 12:34:56','ddddd');
  211. Query OK, 1 row affected (0.00 sec)

  212. mysql> select * from liuyan;
  213. +----+-------+----------+---------------------+-------------+
  214. | id | title | author   | addtime             | content     |
  215. +----+-------+----------+---------------------+-------------+
  216. |  1 | news  | zhangsan | 2012-05-22 12:32:50 | hello mysql |
  217. |  2 | info  | lisi     | 2012-12-24 12:12:12 | aaaaa       |
  218. |  3 | test  | wu       | 2012-12-22 00:00:00 | hello       |
  219. |  4 | demo  | wangwu   | 2012-09-10 12:34:56 | ddddd       |
  220. +----+-------+----------+---------------------+-------------+
  221. 4 rows in set (0.00 sec)

  222. mysql>
  223. ---批量添加3条数据
  224. mysql> insert into liuyan
  225.     -> values(null,'tt1','lisi1','2012-02-03 12:21:34','dddd'),
  226.     -> (null,'tt2','lisi2','2012-02-03 12:21:34','eeee'),
  227.     -> (null,'tt3','lisi3','2012-02-03 12:21:34','ffff');
  228. Query OK, 3 rows affected (0.00 sec)
  229. Records: 3  Duplicates: 0  Warnings: 0

  230. mysql> select * from liuyan;
  231. +----+-------+----------+---------------------+-------------+
  232. | id | title | author   | addtime             | content     |
  233. +----+-------+----------+---------------------+-------------+
  234. |  1 | news  | zhangsan | 2012-05-22 12:32:50 | hello mysql |
  235. |  2 | info  | lisi     | 2012-12-24 12:12:12 | aaaaa       |
  236. |  3 | test  | wu       | 2012-12-22 00:00:00 | hello       |
  237. |  4 | demo  | wangwu   | 2012-09-10 12:34:56 | ddddd       |
  238. |  5 | tt1   | lisi1    | 2012-02-03 12:21:34 | dddd        |
  239. |  6 | tt2   | lisi2    | 2012-02-03 12:21:34 | eeee        |
  240. |  7 | tt3   | lisi3    | 2012-02-03 12:21:34 | ffff        |
  241. +----+-------+----------+---------------------+-------------+
  242. 7 rows in set (0.00 sec)

  243. mysql>


  244. mysql> create table demo(
  245.     -> d1 tinyint not null,
  246.     -> d2 int(4) zerofill not null,
  247.     -> d3 double(4,2) not null,
  248.     -> d4 varchar(4) not null);
  249. Query OK, 0 rows affected (0.05 sec)

  250. mysql> desc demo;
  251. +-------+--------------------------+------+-----+---------+-------+
  252. | Field | Type                     | Null | Key | Default | Extra |
  253. +-------+--------------------------+------+-----+---------+-------+
  254. | d1    | tinyint(4)               | NO   |     | NULL    |       |
  255. | d2    | int(4) unsigned zerofill | NO   |     | NULL    |       |
  256. | d3    | double(4,2)              | NO   |     | NULL    |       |
  257. | d4    | varchar(4)               | NO   |     | NULL    |       |
  258. +-------+--------------------------+------+-----+---------+-------+
  259. 4 rows in set (0.00 sec)

  260. mysql> show create table demo \G;
  261. *************************** 1. row ***************************
  262.        Table: demo
  263. Create Table: CREATE TABLE `demo` (
  264.   `d1` tinyint(4) NOT NULL,
  265.   `d2` int(4) unsigned zerofill NOT NULL,
  266.   `d3` double(4,2) NOT NULL,
  267.   `d4` varchar(4) NOT NULL
  268. ) ENGINE=MyISAM DEFAULT CHARSET=utf8
  269. 1 row in set (0.00 sec)

  270. ERROR:
  271. No query specified

  272. mysql>mysql> desc demo;
  273. +-------+--------------------------+------+-----+---------+-------+
  274. | Field | Type                     | Null | Key | Default | Extra |
  275. +-------+--------------------------+------+-----+---------+-------+
  276. | d1    | tinyint(4)               | NO   |     | NULL    |       |
  277. | d2    | int(4) unsigned zerofill | NO   |     | NULL    |       |
  278. | d3    | double(4,2)              | NO   |     | NULL    |       |
  279. | d4    | varchar(4)               | NO   |     | NULL    |       |
  280. +-------+--------------------------+------+-----+---------+-------+
  281. 4 rows in set (0.00 sec)

  282. mysql> insert into demo values(11,22,123.456,"abcdef");
  283. Query OK, 1 row affected, 2 warnings (0.00 sec)

  284. mysql> select * from demo;
  285. +----+------+-------+------+
  286. | d1 | d2   | d3    | d4   |
  287. +----+------+-------+------+
  288. | 11 | 0022 | 99.99 | abcd |
  289. +----+------+-------+------+
  290. 1 row in set (0.00 sec)

  291. mysql> insert into demo values(11,12345,12.3456,"abc");
  292. Query OK, 1 row affected (0.00 sec)

  293. mysql> select * from demo;
  294. +----+-------+-------+------+
  295. | d1 | d2    | d3    | d4   |
  296. +----+-------+-------+------+
  297. | 11 |  0022 | 99.99 | abcd |
  298. | 11 | 12345 | 12.35 | abc  |
  299. +----+-------+-------+------+
  300. 2 rows in set (0.00 sec)

  301. mysql> insert into demo values(128,22,123.456,"abcdef");
  302. Query OK, 1 row affected, 3 warnings (0.00 sec)

  303. mysql> select * from demo;
  304. +-----+-------+-------+------+
  305. | d1  | d2    | d3    | d4   |
  306. +-----+-------+-------+------+
  307. |  11 |  0022 | 99.99 | abcd |
  308. |  11 | 12345 | 12.35 | abc  |
  309. | 127 |  0022 | 99.99 | abcd |
  310. +-----+-------+-------+------+
  311. 3 rows in set (0.00 sec)

  312. mysql>

  313. ================================================================
  314.         表结构的修改
  315. ================================================================
  316. --创建一个测试表格
  317. mysql> create table news(
  318.     -> title varchar(32) not null);
  319. Query OK, 0 rows affected (0.08 sec)

  320. --显示表结构
  321. mysql> desc news;
  322. +-------+-------------+------+-----+---------+-------+
  323. | Field | Type        | Null | Key | Default | Extra |
  324. +-------+-------------+------+-----+---------+-------+
  325. | title | varchar(32) | NO   |     | NULL    |       |
  326. +-------+-------------+------+-----+---------+-------+
  327. 1 row in set (0.00 sec)

  328. --为表添加一个主键列
  329. mysql> alter table news add id int unsigned not null auto_increment primary key
  330. first;
  331. Query OK, 0 rows affected (0.09 sec)
  332. Records: 0  Duplicates: 0  Warnings: 0

  333. --显示表结构
  334. mysql> desc news;
  335. +-------+------------------+------+-----+---------+----------------+
  336. | Field | Type             | Null | Key | Default | Extra          |
  337. +-------+------------------+------+-----+---------+----------------+
  338. | id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
  339. | title | varchar(32)      | NO   |     | NULL    |                |
  340. +-------+------------------+------+-----+---------+----------------+
  341. 2 rows in set (0.01 sec)

  342. mysql> alter table news add content text not null;
  343. Query OK, 0 rows affected (0.06 sec)
  344. Records: 0  Duplicates: 0  Warnings: 0

  345. --显示表结构
  346. mysql> desc news;
  347. +---------+------------------+------+-----+---------+----------------+
  348. | Field   | Type             | Null | Key | Default | Extra          |
  349. +---------+------------------+------+-----+---------+----------------+
  350. | id      | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
  351. | title   | varchar(32)      | NO   |     | NULL    |                |
  352. | content | text             | NO   |     | NULL    |                |
  353. +---------+------------------+------+-----+---------+----------------+
  354. 3 rows in set (0.02 sec)

  355. --为表添加一个作者字段 varchar32位,在标题后面
  356. mysql> alter table news add author varchar(32) not null after title;
  357. Query OK, 0 rows affected (0.08 sec)
  358. Records: 0  Duplicates: 0  Warnings: 0

  359. --显示表结构
  360. mysql> desc news;
  361. +---------+------------------+------+-----+---------+----------------+
  362. | Field   | Type             | Null | Key | Default | Extra          |
  363. +---------+------------------+------+-----+---------+----------------+
  364. | id      | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
  365. | title   | varchar(32)      | NO   |     | NULL    |                |
  366. | author  | varchar(32)      | NO   |     | NULL    |                |
  367. | content | text             | NO   |     | NULL    |                |
  368. +---------+------------------+------+-----+---------+----------------+
  369. 4 rows in set (0.02 sec)

  370. --将字段author修改为username类型varchar(16)
  371. mysql> alter table news change author username varchar(16) not null;
  372. Query OK, 0 rows affected (0.08 sec)
  373. Records: 0  Duplicates: 0  Warnings: 0

  374. --显示表结构
  375. mysql> desc news;
  376. +----------+------------------+------+-----+---------+----------------+
  377. | Field    | Type             | Null | Key | Default | Extra          |
  378. +----------+------------------+------+-----+---------+----------------+
  379. | id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
  380. | title    | varchar(32)      | NO   |     | NULL    |                |
  381. | username | varchar(16)      | NO   |     | NULL    |                |
  382. | content  | text             | NO   |     | NULL    |                |
  383. +----------+------------------+------+-----+---------+----------------+
  384. 4 rows in set (0.01 sec)

  385. --删除一个字段
  386. mysql> alter table news drop content;
  387. Query OK, 0 rows affected (0.08 sec)
  388. Records: 0  Duplicates: 0  Warnings: 0

  389. --显示表结构
  390. mysql> desc news;
  391. +----------+------------------+------+-----+---------+----------------+
  392. | Field    | Type             | Null | Key | Default | Extra          |
  393. +----------+------------------+------+-----+---------+----------------+
  394. | id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
  395. | title    | varchar(32)      | NO   |     | NULL    |                |
  396. | username | varchar(16)      | NO   |     | NULL    |                |
  397. +----------+------------------+------+-----+---------+----------------+
  398. 3 rows in set (0.01 sec)

  399. mysql>


  400. ================================================================
  401.         修改表数据
  402. ================================================================

  403. mysql>
  404. mysql> select * from liuyan;
  405. +----+-------+----------+---------------------+-------------+
  406. | id | title | author   | addtime             | content     |
  407. +----+-------+----------+---------------------+-------------+
  408. |  1 | news  | zhangsan | 2012-05-22 12:32:50 | hello mysql |
  409. |  2 | info  | lisi     | 2012-12-24 12:12:12 | aaaaa       |
  410. |  3 | test  | wu       | 2012-12-22 00:00:00 | hello       |
  411. |  4 | demo  | wangwu   | 2012-09-10 12:34:56 | ddddd       |
  412. |  5 | tt1   | lisi1    | 2012-02-03 12:21:34 | dddd        |
  413. |  6 | tt2   | lisi2    | 2012-02-03 12:21:34 | eeee        |
  414. |  7 | tt3   | lisi3    | 2012-02-03 12:21:34 | ffff        |
  415. +----+-------+----------+---------------------+-------------+
  416. 7 rows in set (0.00 sec)

  417. --修改留言表中author字段值为qq(所有数据)
  418. mysql> update liuyan set author='qq';
  419. Query OK, 7 rows affected (0.00 sec)
  420. Rows matched: 7  Changed: 7  Warnings: 0

  421. mysql> select * from liuyan;
  422. +----+-------+--------+---------------------+-------------+
  423. | id | title | author | addtime             | content     |
  424. +----+-------+--------+---------------------+-------------+
  425. |  1 | news  | qq     | 2012-05-22 12:32:50 | hello mysql |
  426. |  2 | info  | qq     | 2012-12-24 12:12:12 | aaaaa       |
  427. |  3 | test  | qq     | 2012-12-22 00:00:00 | hello       |
  428. |  4 | demo  | qq     | 2012-09-10 12:34:56 | ddddd       |
  429. |  5 | tt1   | qq     | 2012-02-03 12:21:34 | dddd        |
  430. |  6 | tt2   | qq     | 2012-02-03 12:21:34 | eeee        |
  431. |  7 | tt3   | qq     | 2012-02-03 12:21:34 | ffff        |
  432. +----+-------+--------+---------------------+-------------+
  433. 7 rows in set (0.00 sec)

  434. --修改留言表中某列的某几条数据。
  435. mysql> update liuyan set author='zhangsan' where  id in(1,3,5);
  436. Query OK, 3 rows affected (0.03 sec)
  437. Rows matched: 3  Changed: 3  Warnings: 0

  438. mysql> select * from liuyan;
  439. +----+-------+----------+---------------------+-------------+
  440. | id | title | author   | addtime             | content     |
  441. +----+-------+----------+---------------------+-------------+
  442. |  1 | news  | zhangsan | 2012-05-22 12:32:50 | hello mysql |
  443. |  2 | info  | qq       | 2012-12-24 12:12:12 | aaaaa       |
  444. |  3 | test  | zhangsan | 2012-12-22 00:00:00 | hello       |
  445. |  4 | demo  | qq       | 2012-09-10 12:34:56 | ddddd       |
  446. |  5 | tt1   | zhangsan | 2012-02-03 12:21:34 | dddd        |
  447. |  6 | tt2   | qq       | 2012-02-03 12:21:34 | eeee        |
  448. |  7 | tt3   | qq       | 2012-02-03 12:21:34 | ffff        |
  449. +----+-------+----------+---------------------+-------------+
  450. 7 rows in set (0.00 sec)

  451. --修改某条数据的某几列。
  452. mysql> update liuyan set title='llllll',author="lisi",content="qwertyuio"
  453.     -> where id=7;
  454. Query OK, 1 row affected (0.00 sec)
  455. Rows matched: 1  Changed: 1  Warnings: 0

  456. mysql> select * from liuyan;
  457. +----+--------+----------+---------------------+-------------+
  458. | id | title  | author   | addtime             | content     |
  459. +----+--------+----------+---------------------+-------------+
  460. |  1 | news   | zhangsan | 2012-05-22 12:32:50 | hello mysql |
  461. |  2 | info   | qq       | 2012-12-24 12:12:12 | aaaaa       |
  462. |  3 | test   | zhangsan | 2012-12-22 00:00:00 | hello       |
  463. |  4 | demo   | qq       | 2012-09-10 12:34:56 | ddddd       |
  464. |  5 | tt1    | zhangsan | 2012-02-03 12:21:34 | dddd        |
  465. |  6 | tt2    | qq       | 2012-02-03 12:21:34 | eeee        |
  466. |  7 | llllll | lisi     | 2012-02-03 12:21:34 | qwertyuio   |
  467. +----+--------+----------+---------------------+-------------+
  468. 7 rows in set (0.00 sec)

  469. --将6号信息中的作者改为王五
  470. mysql>update liuyan set author="wangwu" where id=6;


  471. mysql> select * from demo;
  472. +-----+-------+-------+------+
  473. | d1  | d2    | d3    | d4   |
  474. +-----+-------+-------+------+
  475. |  11 |  0022 | 99.99 | abcd |
  476. |  11 | 12345 | 12.35 | abc  |
  477. | 127 |  0022 | 99.99 | abcd |
  478. +-----+-------+-------+------+
  479. 3 rows in set (0.00 sec)
  480. --将demo表中d1字段的所有值都减一。
  481. mysql> update demo set d1=d1-1;
  482. Query OK, 3 rows affected (0.00 sec)
  483. Rows matched: 3  Changed: 3  Warnings: 0

  484. mysql> select * from demo;
  485. +-----+-------+-------+------+
  486. | d1  | d2    | d3    | d4   |
  487. +-----+-------+-------+------+
  488. |  10 |  0022 | 99.99 | abcd |
  489. |  10 | 12345 | 12.35 | abc  |
  490. | 126 |  0022 | 99.99 | abcd |
  491. +-----+-------+-------+------+
  492. 3 rows in set (0.00 sec)








复制代码

作者: crx349    时间: 2013-5-16 14:08


  1. mysql> use fooknet
  2. Database changed
  3. mysql> select database();
  4. +------------+
  5. | database() |
  6. +------------+
  7. | fooknet     |
  8. +------------+
  9. 1 row in set (0.00 sec)

  10. mysql> show tables;
  11. +------------------+
  12. | Tables_in_fooknet |
  13. +------------------+
  14. | demo             |
  15. | liuyan           |
  16. | news             |
  17. +------------------+
  18. 3 rows in set (0.06 sec)

  19. mysql> create table stu(
  20.     -> id int unsigned not null auto_increment primary key,
  21.     -> name varchar(16) unique not null,
  22.     -> sex enum('m','w') not null default 'm',
  23.     -> age tinyint unsigned ,
  24.     -> classid char(6) not null);
  25. Query OK, 0 rows affected (0.13 sec)

  26. mysql> create table grade(
  27.     -> id int unsigned not null auto_increment primary key,
  28.     -> sid int unsigned not null,
  29.     -> php tinyint unsigned not null,
  30.     -> mysql tinyint unsigned not null);
  31. Query OK, 0 rows affected (0.06 sec)

  32. mysql> desc stu;
  33. +---------+---------------------+------+-----+---------+----------------+
  34. | Field   | Type                | Null | Key | Default | Extra          |
  35. +---------+---------------------+------+-----+---------+----------------+
  36. | id      | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
  37. | name    | varchar(16)         | NO   | UNI | NULL    |                |
  38. | sex     | enum('m','w')       | NO   |     | m       |                |
  39. | age     | tinyint(3) unsigned | YES  |     | NULL    |                |
  40. | classid | char(6)             | NO   |     | NULL    |                |
  41. +---------+---------------------+------+-----+---------+----------------+
  42. 5 rows in set (0.08 sec)

  43. mysql> desc grade;
  44. +-------+---------------------+------+-----+---------+----------------+
  45. | Field | Type                | Null | Key | Default | Extra          |
  46. +-------+---------------------+------+-----+---------+----------------+
  47. | id    | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
  48. | sid   | int(10) unsigned    | NO   |     | NULL    |                |
  49. | php   | tinyint(3) unsigned | NO   |     | NULL    |                |
  50. | mysql | tinyint(3) unsigned | NO   |     | NULL    |                |
  51. +-------+---------------------+------+-----+---------+----------------+
  52. 4 rows in set (0.00 sec)

  53. mysql> insert into stu values(null,'zhangsan','m',20,'fooknet');
  54. Query OK, 1 row affected (0.03 sec)

  55. mysql> insert into stu values(null,'lisi','w',21,'fooknet'),
  56.     -> (null,'wangwu','m',20,'lamp46'),
  57.     -> (null,'zhaoliu','w',19,'lamp47'),
  58.     -> (null,'tianqi','m',22,'lamp47'),
  59.     -> (null,'xiaowang','w',24,'lamp46'),
  60.     -> (null,'aa','w',25,'fooknet'),
  61.     -> (null,'qq','w',27,'fooknet'),
  62.     -> (null,'dd','m',20,'lamp46');
  63. Query OK, 8 rows affected (0.00 sec)
  64. Records: 8  Duplicates: 0  Warnings: 0

  65. mysql> insert into stu values(null,'pp','w',25,'fooknet');
  66. Query OK, 1 row affected (0.00 sec)

  67. mysql> select * from stu;
  68. +----+----------+-----+------+---------+
  69. | id | name     | sex | age  | classid |
  70. +----+----------+-----+------+---------+
  71. |  1 | zhangsan | m   |   20 | fooknet  |
  72. |  2 | lisi     | w   |   21 | fooknet  |
  73. |  3 | wangwu   | m   |   20 | lamp46  |
  74. |  4 | zhaoliu  | w   |   19 | lamp47  |
  75. |  5 | tianqi   | m   |   22 | lamp47  |
  76. |  6 | xiaowang | w   |   24 | lamp46  |
  77. |  7 | aa       | w   |   25 | fooknet  |
  78. |  8 | qq       | w   |   27 | fooknet  |
  79. |  9 | dd       | m   |   20 | lamp46  |
  80. | 10 | pp       | w   |   25 | fooknet  |
  81. +----+----------+-----+------+---------+
  82. 10 rows in set (0.00 sec)

  83. mysql> desc grade;
  84. +-------+---------------------+------+-----+---------+----------------+
  85. | Field | Type                | Null | Key | Default | Extra          |
  86. +-------+---------------------+------+-----+---------+----------------+
  87. | id    | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
  88. | sid   | int(10) unsigned    | NO   |     | NULL    |                |
  89. | php   | tinyint(3) unsigned | NO   |     | NULL    |                |
  90. | mysql | tinyint(3) unsigned | NO   |     | NULL    |                |
  91. +-------+---------------------+------+-----+---------+----------------+
  92. 4 rows in set (0.00 sec)

  93. mysql> insert into grade values(null,5,80,86),
  94.     -> (null,6,58,62),(null,4,90,98),
  95.     -> (null,1,87,90),(null,2,90,69),
  96.     -> (null,7,52,58),(null,3,67,86);
  97. Query OK, 7 rows affected (0.00 sec)
  98. Records: 7  Duplicates: 0  Warnings: 0

  99. mysql> select * from stu;
  100. +----+----------+-----+------+---------+
  101. | id | name     | sex | age  | classid |
  102. +----+----------+-----+------+---------+
  103. |  1 | zhangsan | m   |   20 | fooknet  |
  104. |  2 | lisi     | w   |   21 | fooknet  |
  105. |  3 | wangwu   | m   |   20 | lamp46  |
  106. |  4 | zhaoliu  | w   |   19 | lamp47  |
  107. |  5 | tianqi   | m   |   22 | lamp47  |
  108. |  6 | xiaowang | w   |   24 | lamp46  |
  109. |  7 | aa       | w   |   25 | fooknet  |
  110. |  8 | qq       | w   |   27 | fooknet  |
  111. |  9 | dd       | m   |   20 | lamp46  |
  112. | 10 | pp       | w   |   25 | fooknet  |
  113. +----+----------+-----+------+---------+
  114. 10 rows in set (0.00 sec)

  115. mysql> select * from grade;
  116. +----+-----+-----+-------+
  117. | id | sid | php | mysql |
  118. +----+-----+-----+-------+
  119. |  1 |   5 |  80 |    86 |
  120. |  2 |   6 |  58 |    62 |
  121. |  3 |   4 |  90 |    98 |
  122. |  4 |   1 |  87 |    90 |
  123. |  5 |   2 |  90 |    69 |
  124. |  6 |   7 |  52 |    58 |
  125. |  7 |   3 |  67 |    86 |
  126. +----+-----+-----+-------+
  127. 7 rows in set (0.00 sec)

  128. mysql> update grade set mysql=mysql+1;
  129. Query OK, 7 rows affected (0.05 sec)
  130. Rows matched: 7  Changed: 7  Warnings: 0

  131. mysql> select * from grade;
  132. +----+-----+-----+-------+
  133. | id | sid | php | mysql |
  134. +----+-----+-----+-------+
  135. |  1 |   5 |  80 |    87 |
  136. |  2 |   6 |  58 |    63 |
  137. |  3 |   4 |  90 |    99 |
  138. |  4 |   1 |  87 |    91 |
  139. |  5 |   2 |  90 |    70 |
  140. |  6 |   7 |  52 |    59 |
  141. |  7 |   3 |  67 |    87 |
  142. +----+-----+-----+-------+
  143. 7 rows in set (0.00 sec)

  144. mysql> quit
  145. Bye

  146. C:\Documents and Settings\admin>mysqldump
  147. Usage: mysqldump [OPTIONS] database [tables]
  148. OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
  149. OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
  150. For more options, use mysqldump --help

  151. C:\Documents and Settings\admin>d:

  152. D:\>cd D:\AppServ\MySQL\bin

  153. D:\AppServ\MySQL\bin>dir
  154. 驱动器 D 中的卷没有标签。
  155. 卷的序列号是 000E-9EA3

  156. D:\AppServ\MySQL\bin 的目录

  157. 2012-04-10  11:13    <DIR>          .
  158. 2012-04-10  11:13    <DIR>          ..
  159. 2008-04-17  20:13         2,052,736 mysql.exe
  160. 2005-10-25  18:23               894 mysql.ico
  161. 2008-04-17  20:13         2,003,584 mysqladmin.exe
  162. 2008-04-17  20:13         5,750,784 mysqld-nt.exe
  163. 2008-04-17  20:13         2,048,640 mysqldump.exe
  164.                5 个文件     11,856,638 字节
  165.                2 个目录  7,268,278,272 可用字节

  166.     数据的备份与恢复
  167. =====================================================
  168. 切记是在DOS命令行下执行:
  169. --备份(导出)数据库fooknet
  170. D:\AppServ\MySQL\bin>mysqldump -u root -p fooknet>fooknet_20120523.sql
  171. Enter password: ****

  172. --备份(导出)数据库fooknet中的一个stu表。
  173. D:\AppServ\MySQL\bin>mysqldump -u root -p fooknet stu>fooknet_stu_20120523.sql
  174. Enter password: ****

  175. --数据的恢复(导入)
  176. D:\AppServ\www\fooknet\mysql>mysql -u root -p fooknet <fooknet_20120523.sql
  177. Enter password: ****

复制代码





欢迎光临 无限星辰工作室-客户无限互联网动力之源 (https://xmspace.net/) Powered by Discuz! X3.4