背景:mysql已经正确安装! 1. 启动mysql服务器: net start mysql 2. 登录到mysql的服务器((可以是本地的服务器)): mysql -uroot -p -P3306 -h127.0.0.1 root //输入密码 3. 查看当前所有的存在mysql服务器的数据库: SHOW DATABASES; 4. 进入某一个数据库: USE test; 5. 查看当前数据库版本: SELECT VERSION(); 6. 查看当前登录的用户: SELECT USER(); 7. 查看当前时间: SELECT NOW(); 8. 创建带主键约束的数据表: (主键约束是指:主键的值不同相同,且一张表中只能有一个主键,且主键会自动创建索引) CREATE TABLE IF NOT EXISTS tb3( id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, //主键约束 username VARCHAR(20) NOT NULL, salary FLOAT(8,2) NOT NULL ); 9. 显示表结构: SHOW COLUMNS FROM tb3; 10. 向数据表中插入数据: INSERT INTO tb3 (username,salary) VALUES('Victor',4980.25); 11. 显示当前表中的数据: SELECT * FROM tb3; 12. 创建带唯一约束的表:(唯一约束则该列的值不能相同,可以是NULL) CREATE TABLE IF NOT EXISTS tb5( id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(20) NOT NULL UNIQUE KEY, //唯一约束 age TINYINT UNSIGNED ); 13. 创建带默认约束的表:(默认约束是指在没有赋值的情况下,显示默认值) CREATE TABLE IF NOT EXISTS tb6( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(20) NOT NULL UNIQUE KEY, sex ENUM('1','2','3') DEFAULT 3 ); 14. 查看当前的数据库: SELECT DATABASE(); | status 15. 查看当前数据库中的某一张表的创建信息: SHOW CREATE TABLE user; 16. 创建带外键约束的表:(1.表引擎必须是INNODB,2.外键与参照键数据类型必须相似,外键与参照键必须创建索引) CREATE TABLE IF NOT EXISTS provinces( //创建父表 id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, pname VARCHAR(20) NOT NULL UNIQUE KEY ); CREATE TABLE IF NOT EXISTS user( //创建带外键约束的子表 id SMALLINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, username VARCHAR(20) NOT NULL UNIQUE KEY, pid SMALLINT UNSIGNED, FOREIGN KEY (pid) REFERENCES provinces (id) //外键约束,参照列为provinces表的id列 ); 17. 查看某个数据表中的列是否创建了索引: SHOW INDEXES FROM provinces\G; //其中\G 是格式化输出,可以不要 18. 使用外键约束进行子表与父表操作的关联,如删除父表的参照值,则相应的删除子表对应的一行记录(也可能是多行) CREATE TABLE IF NOT EXISTS provinces( //创建父表 id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, pname VARCHAR(20) NOT NULL UNIQUE KEY ); CREATE TABLE IF NOT EXISTS user1( //创建带外键约束的子表 id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL, username VARCHAR(20) NOT NULL , pid SMALLINT UNSIGNED, FOREIGN KEY (pid) REFERENCES provinces (id) ON DELETE CASCADE //在删除父表的参照值的同时,删除子表(当前表)对应的行的记录 ); 19. 按条件删除数据: DELETE FROM provinces WHERE id = 2; 20. 增加指定数据表的列(增加单列) ALTER TABLE user1 ADD COLUMN password VARCHAR(32) NOT NULL AFTER username; //可以是after 某列,也可以是FIRST,或者不写条件,默认放到最后 21. 增加指定数据表的列(增加多列) ALTER TABLE user1 ADD COLUMN (salary FLOAT(8,2) UNSIGNED NOT NULL,sex ENUM('1','2','3') DEFAULT 3); 22. 删除一列数据 ALTER TABLE user1 DROP COLUMN password; //其中COLUMN 可以省略 22. 删除多列,并添加多列(逗号隔开即可操作) ALTER TABLE user1 DROP COLUMN salary,DROP COLUMN sex,ADD COLUMN height FLOAT(3,2) UNSIGNED NOT NULL DEFAULT 1.75; ALTER TABLE user1 DROP COLUMN height,DROP COLUMN salary,ADD COLUMN weight FLOAT(5,2) NOT NULL DEFAULT 120.85,ADD COLUMN hobby VARCHAR(100) NOT NULL; 23. 添加主键约束 ALTER TABLE user2 ADD CONSTRAINT PRIMARY KEY (id); 24. 添加唯一约束 ALTER TABLE user2 ADD CONSTRAINT UNIQUE KEY (username); 25. 添加外键约束 ALTER TABLE user2 ADD FOREIGN KEY (pid) REFERENCES provinces (id); 26. 添加默认约束 ALTER TABLE user2 ALTER COLUMN age SET DEFAULT 15; 27. 删除默认约束 ALTER TABLE user2 ALTER COLUMN age DROP DEFAULT 15; 28. 删除主键约束 ALTER TABLE user2 DROP PRIMARY KEY; 29. 删除唯一约束 //实际上是删除索引 ALTER TABLE user2 DROP INDEX username; 30. 删除外键约束 ALTER TABLE user2 DROP FOREIGN KEY user2_ibfk_1;(user2_ibfk_1是外键约束名,可以通过SHOW CREATE TABLE user2;查看到) 31. 修改列定义(修改某一列的数据类型,在表中的位置等) ALTER TABLE user2 MODIFY COLUMN age SMALLINT UNSIGNED NOT NULL after username; 32. 修改列名称以及列定义: ALTER TABLE user2 CHANGE COLUMN pid _pid TINYINT UNSIGNED NOT NULL; 33. 修改数据表的表名: ALTER TABLE user2 RENAME TO user2qq; // 或 RENAME TABLE user2qq TO user2; ///记录操作 34. 插入数据 一次插入两行记录 INSERT INTO user VALUES(DEFAULT,'Ann',21131,22,0),(DEFAULT,'Amy',yy255366,19,0);//自增长和有默认约束的都可以赋默认值 35. 插入数据 ,插入的数据中可以包含函数表达式(插入一个md5的函数) INSERT INTO user VALUES(DEFAULT,'Victor',md5('123'),DEFAULT,1); 36. 使用INSERT SET方式插入数据 INSERT INTO user SET username='Bob',password='md5(200)',sex=1;(不同列的数据插入使用逗号分隔) 37. 更新数据: UPDATE user SET age = age + 20; (更新所有行的一列的数据) UPDATE user SET age = age -id ,sex = 0, where id<3; (更新符合条件的行的多列的操作) 38. 删除指定行的数据: DELETE FROM user WHERE id=4; DELETE t1 FROM tb1 AS t1 WHERE id = 2;//删除表的同时使用别名 39. 查询某两列的数据: SELECT id,password FROM user; SELECT id,password FROM user WHERE id % 2 = 0; SELECT user.username,user.id FROM user; SELECT id AS uid,username AS name,password AS pw FROM user; (使用别名显示查询结果,AS可省略) SELECT id AS uid,username AS name,password AS pw,age,sex FROM user GROUP BY age DESC;(按照年龄降序排列查询结果) SELECT id AS uid,username AS name,password AS pw,age,sex FROM user GROUP BY age DESC HAVING id<5 having="" group="" by="" order="" by="" order="" by="" group="" by="" select="" from="" user="" order="" by="" age="" desc="" id="" desc="" order="" by="" select="" from="" user="" order="" by="" age="" desc="" id="" desc="" limit="" 0="" 3="" limit="" index="" select="" from="" user="" limit="" 2="" 3="" 40="" insert="" into="" tb1="" username="" select="" username="" from="" user="" where="" age="">27;//注意:这种插入不需要写VALUES()。直接插入 /子查询 准备工作: CREATE TABLE IF NOT EXISTS tdb_goods( goods_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, goods_name VARCHAR(150) NOT NULL, goods_cate VARCHAR(40) NOT NULL, brand_name VARCHAR(40) NOT NULL, goods_price DECIMAL(15,3) UNSIGNED NOT NULL DEFAULT 0, is_show BOOLEAN NOT NULL DEFAULT 1, is_saleoff BOOLEAN NOT NULL DEFAULT 0 ); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('R510VC 15.6英寸笔记本','笔记本','华硕','3399',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Y400N 14.0英寸笔记本电脑','笔记本','联想','4899',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('G150TH 15.6英寸游戏本','游戏本','雷神','8499',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('X550CC 15.6英寸笔记本','笔记本','华硕','2799',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('X240(20ALA0EYCD) 12.5英寸超极本','超级本','联想','4999',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('U330P 13.3英寸超极本','超级本','联想','4299',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('SVP13226SCB 13.3英寸触控超极本','超级本','索尼','7999',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('iPad mini MD531CH/A 7.9英寸平板电脑','平板电脑','苹果','1998',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)','平板电脑','苹果','3388',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(' iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版)','平板电脑','苹果','2788',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('IdeaCentre C340 20英寸一体电脑 ','台式机','联想','3499',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Vostro 3800-R1206 台式电脑','台式机','戴尔','2899',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('iMac ME086CH/A 21.5英寸一体电脑','台式机','苹果','9188',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )','台式机','宏碁','3699',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Z220SFF F4F06PA工作站','服务器/工作站','惠普','4288',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('PowerEdge T110 II服务器','服务器/工作站','戴尔','5388',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Mac Pro MD878CH/A 专业级台式电脑','服务器/工作站','苹果','28888',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(' HMZ-T3W 头戴显示设备','笔记本配件','索尼','6999',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('商务双肩背包','笔记本配件','索尼','99',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('X3250 M4机架式服务器 2583i14','服务器/工作站','IBM','6888',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('玄龙精英版 笔记本散热器','笔记本配件','九州风神','',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(' HMZ-T3W 头戴显示设备','笔记本配件','索尼','6999',DEFAULT,DEFAULT); INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('商务双肩背包','笔记本配件','索尼','99',DEFAULT,DEFAULT); 41. 子查询 SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price >= (SELECT ROUND(AVG(goods_price),2) FROM tdb_goods); (查询结果为商品价格大于平均值的商品的id,name,price) SELECT goods_id,goods_name,goods_cate,goods_price FROM tdb_goods WHERE goods_price > (SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本'); (查询结果为大于超级本价格的全部商品==返回错误,因为超级本的价格有多个) SELECT goods_id,goods_name,goods_cate,goods_price FROM tdb_goods WHERE goods_price > ANY (SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本'); (查询结果为大于 ANY/SOME 超级本价格的全部商品==大于超级本的最低价格即可满足) SELECT goods_id,goods_name,goods_cate,goods_price FROM tdb_goods WHERE goods_price > ALL (SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本'); (查询结果为大于 ALL 超级本价格的全部商品==大于超级本的全部价格即可满足) SELECT goods_id,goods_name,goods_cate,goods_price FROM tdb_goods WHERE goods_price != ALL (SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本'); (查询结果为 != ALL 超级本价格的全部商品==即得到除了超级本之外的所有商品) 42. 将查询结果插入到新表中 a. 创建一个数据表 CREATE TABLE IF NOT EXISTS tdb_goods_cates( cate_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT NOT NULL, cate_name VARCHAR(40) NOT NULL ); b. 将查询结果(所有的商品分类写入新表) INSERT INTO tdb_goods_cates (cate_name) SELECT tdb_goods.goods_cate FROM tdb_goods GROUP BY tdb_goods.goods_cate; 43. 参照表2的数据,更新表1 的数据 UPDATE tdb_goods SET goods_cate = (SELECT tdb_goods_cates.cate_id FROM tdb_goods_cates WHERE goods_cate = cate_name); (当goods_cate = cate_name的时候,前将表tdb_goods_cates的cate_id值给tdb_goods表的goods_cate) UPDATE tdb_goods INNER JOIN tdb_goods_cates ON goods_cate = cate_cate SET goods_cate = cate_id; (和上一条语句效果相同) 44. 在创建表的同时,将上一个表的数据插入到本表中 CREATE TABLE IF NOT EXISTS tdb_goods_brands( brand_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, brand_name VARCHAR(40) NOT NULL ) SELECT tdb_goods.brand_name FROM tdb_goods GROUP BY tdb_goods.brand_name; 45. 修改列名称以及列定义: ALTER TABLE tdb_goods CHANGE goods_cate cate_id SMALLINT UNSIGNED,CHANGE brand_name brand_id SMALLINT UNSIGNED; (将列goods_cate改为cate_id,列类型改为SMALLINT UNSIGNED) 46. 通过内联查询两张表的共同列所在的第一张表行的数据 SELECT goods_id,goods_name,tdb_goods.cate_id FROM tdb_goods INNER JOIN tdb_goods_cates ON tdb_goods.cate_id = tdb_goods_cates.cate_id; 48. 通过右外连接查询两张表的共同列所在的第一张表行的数据(以右边,也就是第二张表为基准) SELECT goods_id,goods_name,tdb_goods.cate_id FROM tdb_goods RIGHT JOIN tdb_goods_cates ON tdb_goods.cate_id = tdb_goods_cates.cate_id; 49. 通过多表的内联查询全部的数据 ###### SELECT goods_id,goods_name,brand_name,cate_name,goods_price FROM tdb_goods AS a INNER JOIN tdb_goods_brands AS b ON a.brand_id = b.brand_id INNER JOIN tdb_goods_cates AS c ON a.cate_id = c.cate_id ; (返回内联中的商品id,name,brand,cate,price) (连接==外键的逆向操作,将多个表中存储的数据,一次性返回出来) 自连接的查询 50. 无限级分类表的设计 -- 无限分类的数据表设计 CREATE TABLE tdb_goods_types( type_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, type_name VARCHAR(20) NOT NULL, parent_id SMALLINT UNSIGNED NOT NULL DEFAULT 0 ); INSERT tdb_goods_types(type_name,parent_id) VALUES('家用电器',DEFAULT); INSERT tdb_goods_types(type_name,parent_id) VALUES('电脑、办公',DEFAULT); INSERT tdb_goods_types(type_name,parent_id) VALUES('大家电',1); INSERT tdb_goods_types(type_name,parent_id) VALUES('生活电器',1); INSERT tdb_goods_types(type_name,parent_id) VALUES('平板电视',3); INSERT tdb_goods_types(type_name,parent_id) VALUES('空调',3); INSERT tdb_goods_types(type_name,parent_id) VALUES('电风扇',4); INSERT tdb_goods_types(type_name,parent_id) VALUES('饮水机',4); INSERT tdb_goods_types(type_name,parent_id) VALUES('电脑整机',2); INSERT tdb_goods_types(type_name,parent_id) VALUES('电脑配件',2); INSERT tdb_goods_types(type_name,parent_id) VALUES('笔记本',9); INSERT tdb_goods_types(type_name,parent_id) VALUES('超级本',9); INSERT tdb_goods_types(type_name,parent_id) VALUES('游戏本',9); INSERT tdb_goods_types(type_name,parent_id) VALUES('CPU',10); INSERT tdb_goods_types(type_name,parent_id) VALUES('主机',10); 51. 查询无限分类表中的所有子类对应的父类 SELECT s.type_id,s.type_name,p.type_name FROM tdb_goods_types AS s LEFT JOIN tdb_goods_types AS p ON s.parent_id = p.type_id; 52. 查询每个父类对应的子类 SELECT p.type_id,p.type_name,s.type_name as sub_name FROM tdb_goods_types AS p LEFT JOIN tdb_goods_types AS s ON p.type_id = s.parent_id; 53. 查询每个父类对应的子类的数量,并按照id升序排列: SELECT p.type_id,p.type_name,count(s.type_name) AS sub_count FROM tdb_goods_types AS p LEFT JOIN tdb_goods_types AS s ON p.type_id = s.parent_id GROUP BY p.type_name ORDER BY p.type_id ASC; 54. 删除表中的重复数据(goods)_name相同) //通过外联模拟两张表来操作 DELETE t1 FROM tdb_goods AS t1 LEFT JOIN (SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name HAVING count(goods_name) >= 2) AS t2 ON t1.goods_name = t2.goods_name WHERE t1.goods_id>t2.goods_id; 55. 插入表中的最后3行记录: INSERT INTO tdb_goods (goods_name,cate_id,brand_id,goods_price,is_show,is_saleoff) SELECT goods_name,cate_id,brand_id,goods_price,is_show,is_saleoff FROM tdb_goods ORDER BY goods_id DESC limit 3; //mysql函数的使用 56. 连接字符串 SELECT CONCAT('www.baidu','.com'); SELECT CONCAT_WS('.','www','baidu','com'); 57. 按千分位格式化数字 SELECT FORMAT(12345.678,2);//表示保留小数点后两位。 58. 将字符串转为小写字母 SELECT LOWER('ABCded333'); //大写 SELECT UPPER('XDEssqe'); 59. 获取左边或右边的字符 SELECT LEFT('ASDXDWW',3); //获取左边的3个字符 SELECT RIGHT('MySQL',3);//获取右边的3个字符 60. 获取字符串的长度 SELECT LENGTH('mysql aaa'); 61. 去除字符串的空格 SELECT LTRIM(' aa v ');// 去除左边的空格 SELECT RTRIM(' aa v ');//去除右边的空格 SELECT TRIM(' aa v ');//去除两边的空格 62. 删除前后指定的字符 SELECT TRIM('?' FROM '?fuck me ? ok????'); // fuck me ? ok 63. 删除指定的前面的字符 SELECT TRIM(LEADING '?' FROM '?fuck me ? ok????');// fuck me ? ok???? 64. 删除知道的后续的字符 SELECT TRIM(TRAILING '?' FROM '?fuck me ? ok????'); 65. 替换函数,将字符串中的?替换成空字符 SELECT REPLACE('??mysq??l????','?','');// mysql 66. 字符串的截取(从1开始编号的) SELECT SUBSTRING('imoocStudy',1,2);// im SELECT SUBSTRING('imoocStudy',-3,2);//从倒数第3位开始往后截取2位字符 67. 通配符% 的使用 SELECT * FROM tb_name WHERE firstname LIKE '%c%'; //查询firstname中含有c字符的全部数据 SELECT * FROM tb_name WHERE firstname LIKE '%1%%' ESCAPE '1'; //查询firstname中含有%字符的全部数据 //====数学运算符的使用 68. + - * / % POWER() SELECT POWER(2,8); // ==256 2的8次方 SELECT ROUND(2.75,1); // 四舍五入保留1位 = 2.8 SELECT TRUNCATE(124.56,1); //截取掉一位 == 124.5 mysql> SELECT DATE_ADD('2016-3-1',INTERVAL 365 DAY); -- 日期变化函数 +---------------------------------------+ | DATE_ADD('2016-3-1',INTERVAL 365 DAY) | +---------------------------------------+ | 2017-03-01 | +---------------------------------------+ mysql> SELECT DATEDIFF('2016-3-1','2017-3-1');//日期差值的计算 +---------------------------------+ | DATEDIFF('2016-3-1','2017-3-1') | +---------------------------------+ | -365 | +---------------------------------+ mysql> SELECT DATE_FORMAT('2016-3-1','%Y/%m/%d'); //日期的格式化函数 +------------------------------------+ | DATE_FORMAT('2016-3-1','%Y/%m/%d') | +------------------------------------+ | 2016/03/01 | +------------------------------------+ //=========信息函数 69. 获取当前连接mysql服务器的ID SELECT CONNECTION_ID(); 70. 获取当前打开的数据库 SELECT DATABASE(); 71. 获取当前连接的用户: SELECT USER(); 72. 获取最后插入的ID SELECT LAST_INSERT_ID(); 73. 聚合函数 AVG() COUNT() MAX() MIN() SUM() ///==============自定义函数 CREATE FUNCTION f1() RETURNS VARCHAR(30) -- 创建一个不带参数的函数 RETURN DATE_FORMAT(NOW(),'%Y/%m/%d %h:%m:%s'); 74. 删除函数 DROP FUNCTION f3; 75. 创建带参数的函数 CREATE FUNCTION f2(num1 SMALLINT UNSIGNED,num2 SMALLINT UNSIGNED) RETURNS FLOAT(8,2) RETURN (num1+num2)/2; 76. 调用自定义函数 SELECT f3(7,8); //=== 7.50 //=========================存储过程控制 77. 创建过程体 CREATE PRODURE sp1() SELECT VERSION(); //创建过程体--查询当前版本号 ---- CREATE PROCEDURE deleteUserById(IN p_id INT UNSIGNED) BEGIN DELETE FROM user WHERE id = p_id; END // 78. 调用过程体 CALL sp1(); CALL deleteUserById(3); 79. 创建带输出参数的过程体 delimiter // -- 修改分隔符 CREATE PROCEDURE deleteUserByAgeReturnInfos(IN pAge SMALLINT UNSIGNED, OUT rCount SMALLINT UNSIGNED,OUT userCount SMALLINT UNSIGNED) BEGIN DELETE FROM user WHERE age = pAge; SELECT ROW_COUNT() INTO rCount; SELECT COUNT(age) FROM user INTO userCount; END // ---- 创建一个带一个输入,两个输出的过程体 (通过age删除数据,并返回删除的数据条数,以及剩余的数据条数) 80. 调用刚才创建的过程体 CALL deleteUserByAgeReturnInfos(30,@a,@b); 81. 创建指定存储引擎的数据表: CREATE TABLE IF NOT EXISTS tp1( s1 VARCHAR(30) ) ENGINE = MyISAM; 82. 修改已创建表的存储引擎 ALTER TABLE tp1 ENGINE = InnoDB; // e. 退出mysql操作: exit; E. 停止mysql的服务器: (命令行)net stop mysql ***** 所有的mysql命令来自慕课网~
MySql的学习记录-持续更新
最新推荐文章于 2024-08-16 22:34:59 发布