标签:
mysql数据库的基本操作: 1, 数据库的登录:mysql -u 用户名(root) -p密码 -P (端口) -h服务器名(本地表示:127.0.0.1) 2. 更改数据库显示:mysql -u root -p --prompt \u@\h \d> --prompt: 使用--prompt命令进行修改; \h:表示本机(localhost) \u:当前登录用户(用户名) \d:当前操作数据库(database name) 3. 数据库的命令操作: 1),查询所有数据库命令: root@localhost (none)>SHOW DATABASES; 2) ,使用某个数据库: root@localhost (none)>USE table2 Database changed root@localhost table2> 3),创建一个数据库:CREATE DATABASE 数据库名称: root@localhost table2>SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | t2 | | table1 | | table2 | | tables3 | | test | +--------------------+ 8 rows in set (0.00 sec) root@localhost table2>SHOW CREATE DATABASE table3; ERROR 1049 (42000): Unknown database ‘table3‘ 查看某一个数据库: root@localhost table2>SHOW CREATE DATABASE tables3; +----------+------------------------------------------------------------------+ | Database | Create Database | +----------+------------------------------------------------------------------+ | tables3 | CREATE DATABASE `tables3` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+------------------------------------------------------------------+ 1 row in set (0.00 sec) 更新一个数据库:使用ALTER命令: root@localhost table2>SHOW CREATE DATABASE tables3; +----------+------------------------------------------------------------------+ | Database | Create Database | +----------+------------------------------------------------------------------+ | tables3 | CREATE DATABASE `tables3` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+------------------------------------------------------------------+ 1 row in set (0.00 sec) 更改数据库的编码格式: root@localhost table2>ALTER DATABASE tables3 CHARACTER SET = gbk; Query OK, 1 row affected (0.00 sec) root@localhost table2>show create database tables3; +----------+-----------------------------------------------------------------+ | Database | Create Database | +----------+-----------------------------------------------------------------+ | tables3 | CREATE DATABASE `tables3` /*!40100 DEFAULT CHARACTER SET gbk */ | +----------+-----------------------------------------------------------------+ 1 row in set (0.00 sec) root@localhost table2> 删除数据库:DROP TABLENAME; root@localhost table2>SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | t2 | | table1 | | table2 | | tables3 | | test | +--------------------+ 8 rows in set (0.00 sec) root@localhost table2>DROP DATABASE tables3; Query OK, 0 rows affected (0.00 sec) root@localhost table2>SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | t2 | | table1 | | table2 | | test | +--------------------+ 7 rows in set (0.00 sec) root@localhost table2> 一, 数据库的数据类型: 整形: 1. YINYINT 2. SMALLINT 3, MEDIUMINT 4. INT 5. BIGINT 浮点型: 1, FLOAT[M,D] 2,DOUBLE[M,D] 日期: 1. YEAR 2. TIME 3, DATE 4, DATETIME 5, TIMESTAMP 字符型: 1, CHAR(M) 2, VARCHAR(M) 3, TINYTEXT 4, TEXT 5, DEDIUMTEXT 6, LONGTEXT 7, ENUM 枚举值 8. SET 二, 创建数据库表:CREATE TABLE(行和数据类型) root@localhost test>CREATE TABLE tb3( -> username VARCHAR(20), -> age TINYINT UNSIGNED, -> salary FLOAT(8,2) UNSIGNED -> ); Query OK, 0 rows affected (0.09 sec) 查看数据表是否存在: root@localhost test>SHOW TABLES; +----------------+ | Tables_in_test | +----------------+ | tb3 | +----------------+ 1 row in set (0.00 sec) 查看当前数据库中的某一个库的表: root@localhost test>SHOW TABLES FROM mysql; +---------------------------+ | Tables_in_mysql | +---------------------------+ | columns_priv | | db | | event | | func | | general_log | | help_category | | help_keyword | | help_relation | | help_topic | | host | | ndb_binlog_index | | plugin | | proc | | procs_priv | | proxies_priv | | servers | | slow_log | | tables_priv | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ 24 rows in set (0.00 sec) 查看某一张表的结构: root@localhost test>SHOW COLUMNS FROM tb3; +----------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+-------+ | username | varchar(20) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | | salary | float(8,2) unsigned | YES | | NULL | | +----------+---------------------+------+-----+---------+-------+ 3 rows in set (0.04 sec) root@localhost test> 为表中添加数据:INSERT [INTO] TABLE_NAME(字段信息) VALUE(字段值); root@localhost test>INSERT tb3 VALUES(‘WUYANLONG‘); ERROR 1136 (21S01): Column count doesn‘t match value count at row 1 root@localhost test>insert tb3(username,age) VALUES(‘luoting‘,21); Query OK, 1 row affected (0.04 sec) root@localhost test>insert tb3(username,age) VALUES(‘tom‘,22); Query OK, 1 row affected (0.03 sec) root@localhost test>SHOW TABLSE; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresp onds to your MySQL server version for the right syntax to use near ‘TABLSE‘ at line 1 root@localhost test>SHOW TABLES; +----------------+ | Tables_in_test | +----------------+ | tb1 | | tb3 | +----------------+ 2 rows in set (0.00 sec) root@localhost test>SELECT * FROM tb3; +-----------+------+----------+ | username | age | salary | +-----------+------+----------+ | WUYANLONG | 25 | 78563.21 | | luoting | 21 | NULL | | tom | 22 | NULL | +-----------+------+----------+ 3 rows in set (0.00 sec) 数据库表中的字段允许为空和非空(NULL, NOT NULL); root@localhost test>CREATE TABLE tb2( -> username VARCHAR(20) not null, -> age tinyint unsigned NULL -> ); 1. 数据库中的自动编号: AUTO_INCREMENT属性:必须和主键组合使用。起始值为1 一张数据表中只存在一个主键,主键约束。 主键保证记录的唯一性。 主键自动为NOT NULL root@localhost test>CREATE TABLE tb4( -> id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, -> username VARCHAR(20) NOT NULL -> ); Query OK, 0 rows affected (0.17 sec) root@localhost test> root@localhost test>show columns from tb4; +----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | +----------+----------------------+------+-----+---------+----------------+ 2 rows in set (0.02 sec) AUTO_INCREMENT 必须与主键一起使用,但是主键不一定与AUTO_INCREMENT一起使用; 2. 主键(PRIMARY KEY)是数据库表中的一种约束,另外一种约束是(UNIQUE KEY),主键是非空且唯一的,当同一张表中 要建立多个魏一建约束的时候UNIQUE KEY就有勇武之地了。 唯一约束(一张数据表可以存在多个) 唯一约束可以保证记录的唯一性。 唯一性约束的字段可以为空值(NULL) 3. 默认约束(DEFAULT),如果在插入记录是没有为该字段插入值,则系统插入默认值。 root@localhost test> root@localhost test>CREATE TABLE tb7( -> id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, -> username VARCHAR(20) NOT NULL UNIQUE KEY, -> sex ENUM(‘1‘,‘2‘,‘3‘) DEFAULT ‘3‘ -> ); Query OK, 0 rows affected (0.05 sec) root@localhost test> root@localhost test> root@localhost test>SHOW COLUMNS FROM tb7; +----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | UNI | NULL | | | sex | enum(‘1‘,‘2‘,‘3‘) | YES | | 3 | | +----------+----------------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec) 1. 约束: 约束保证了数据的完整性和一致性; 约束分为表级约束和列级约束; 约束类型包括: NUT NULL(非空约束) PRIMARY KEY(主键约束) UNIQUE KEY(唯一约束) DEFAULT(默认约束) FOREIGN KEY(外键约束) 保证数据的一致性,完整性,实现一对一,多对多的关系。 外键约束的要求: 1. 父表和子表必须使用相同的存储引擎,而且禁止使用临时表; 2. 数据表的存储引擎必须为InoDB;//在配置文件中修改default-storage-engine=INNODB 3 外键列和参照列必须具有相似的数据类型其中数据的长度或是否有符号位必须相同;而字符的长度可以不同 4. 外间列和参照列必须创建索引。如果外键列不存在索引的话MySQL将自动创建索引。 测试: 首先创建一张“province”表: mysql> CREATE TABLE province( -> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -> pname VARCHAR(20) NOT NULL -> ); Query OK, 0 rows affected (0.10 sec) 查看主表province表的信息: mysql> SHOW CREATE TABLE province;//查看主表的存储类型是不是INNODB +----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | province | CREATE TABLE `province` ( `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, `pname` varchar(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> CREATE TABLE users( -> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -> username VARCHAR(20) UNSIGNED NOT NULL, -> p_id BIGINT, -> FOREIGN KEY (p_id) REFERENCES province(id) -> ); 创建子表:user表 ERROR 1072 (42000): Key column ‘p_id‘ doesn‘t exist in table mysql> CREATE TABLE users( -> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, -> username VARCHAR(20) NOT NULL, -> pid SMALLINT UNSIGNED, -> FOREIGN KEY (pid) REFERENCES province (id) -> ); Query OK, 0 rows affected (0.06 sec) mysql> 分析如下:user表称为子表(有外键列的表),外键表(province表,称之为主表。) 外键列和参照列必须穿件索引: 查看主表的索引: mysql> SHOW INDEXES FROM province\G;//查看表的索引 *************************** 1. row *************************** Table: province Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: 1 row in set (0.00 sec) ERROR: No query specified mysql> 外键约束的参照操作: 1. CASCADE : 从父表删除或更新且自动删除或自动更新子表中匹配的行; 2. SET NULL : 从父表删除或更新行,并设置子表中的外键列为NULL。如果使用该选项,必须保证子表列没有指定NOT NULL 3. RESTRICT : 拒绝对父表的删除或更新操作。 4. NOT ACTION : 标准SQL的关键字,在mysql中与RESTRICT相同。 必须现在父表中先添加记录,然后才能添加子表记录; 在实际的开发过程中一般很少使用物理的外键约束,一般都是使用逻辑的外键约束,因为物理外键约束只有INNODB存储引擎才支持。 表级约束和列级约束: 对一个数据列建立的约束,称为列级约束; 对多个数据列建立的约束,称为表级约束; 列级约束既可以在列定义时声明,也可以在列定以后声明。 表级约束只能在列定义之后声明。 在实际开发过程中表级约束用的比较多。(DEFAULT,FOREGIN KEY ,PRIMARY KEY CHECK......); 修改数据表:ALTER TABLE tbl_name ADD[COLUMN] col_name column_definition[FIRST | AFTER col_name] 例子:(添加一列) mysql> SHOW COLUMNS FROM user1; +----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | pid | smallint(5) unsigned | YES | MUL | NULL | | +----------+----------------------+------+-----+---------+----------------+ 3 rows in set (0.04 sec) mysql> ALTER TABLE user1 ADD age TINYINT unsigned NOT NULL DEFAULT 10; Query OK, 3 rows affected (0.19 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> SHOW COLUMNS FROM user1; +----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | pid | smallint(5) unsigned | YES | MUL | NULL | | | age | tinyint(3) unsigned | NO | | 10 | | +----------+----------------------+------+-----+---------+----------------+ 4 rows in set (0.06 sec) mysql> ALTER TABLE user1 ADD password VARCHAR(32) NOT NULL AFTER username; Query OK, 3 rows affected (0.17 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> SHOW COLUMNS FROM user1; +----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | password | varchar(32) | NO | | NULL | | | pid | smallint(5) unsigned | YES | MUL | NULL | | | age | tinyint(3) unsigned | NO | | 10 | | +----------+----------------------+------+-----+---------+----------------+ 5 rows in set (0.05 sec) mysql> ALTER TABLE user1 ADD truename VARCHAR(20) NOT NULL FIRST; Query OK, 3 rows affected (0.18 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> SHOW COLUMNS FROM user1; +----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+----------------+ | truename | varchar(20) | NO | | NULL | | | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | password | varchar(32) | NO | | NULL | | | pid | smallint(5) unsigned | YES | MUL | NULL | | | age | tinyint(3) unsigned | NO | | 10 | | +----------+----------------------+------+-----+---------+----------------+ 6 rows in set (0.05 sec) 添加多列: ALTER TABLE tbl_name ADD [COLUUMN](col_namecolumn_definition,‘.....) 修改数据表: ALTER TABLE tbl_nameDROP [COLUMN] col_name; mysql> SHOW COLUMNS FROM user1; +----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+----------------+ | truename | varchar(20) | NO | | NULL | | | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | password | varchar(32) | NO | | NULL | | | pid | smallint(5) unsigned | YES | MUL | NULL | | | age | tinyint(3) unsigned | NO | | 10 | | +----------+----------------------+------+-----+---------+----------------+ 6 rows in set (0.05 sec) mysql> LTER TABLE user1 DROP truename; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘LTER TABLE user1 DROP truename‘ a t line 1 mysql> ALTER TABLE user1 DROP truename; Query OK, 3 rows affected (0.19 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> SHOW COLUMNS FROM user1; +----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | password | varchar(32) | NO | | NULL | | | pid | smallint(5) unsigned | YES | MUL | NULL | | | age | tinyint(3) unsigned | NO | | 10 | | +----------+----------------------+------+-----+---------+----------------+ 5 rows in set (0.04 sec) mysql> ALTER TABLE user1 DROP password, DROP age; Query OK, 3 rows affected (0.15 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> SHOW COLUMNS FROM user1; +----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+----------------+ | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | pid | smallint(5) unsigned | YES | MUL | NULL | | +----------+----------------------+------+-----+---------+----------------+ 3 rows in set (0.06 sec) mysql> ALTER TABLE user1 DROP password, DROP age; ERROR 1091 (42000): Can‘t DROP ‘password‘; check that column/key exists mysql> 可以添加主键约束,唯一约束,外键约束,也可以删除这些约束。 删除主键约束: ALTER TABLE tbl_name DROP PRIMARY KEY;(一张表只有一个主键) 删除唯一约束: ALTER TABLE tbl_name DROP {INDEX | KEY} username; 删除外键约束: ALTER TABLE tbl_name DROP foreign key key_info; 修改列定义: ALTER TABLE tbl_name MODIFY[COLUMN] col_name column_definition [FIRST|AFTER col_name] mysql> ALTER TABLE user1 DROP PRIMARY KEY; ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key mysql> ALTER TABLE user1 MODIFY pid SMALLINT UNSIGNED NOT NULL first; Query OK, 3 rows affected (0.18 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> SHOW COLUMNS FROM user1; +----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+----------------+ | pid | smallint(5) unsigned | NO | MUL | NULL | | | id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | +----------+----------------------+------+-----+---------+----------------+ 3 rows in set (0.03 sec) (修改pid在第一个位置) 修改数据表的名字:(可以使用ALTER TABLE也可以使用RENAME): mysql> ALTER TABLE user1 RENAME user3; Query OK, 0 rows affected (0.04 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | province | | school | | student | | tb1 | | tb2 | | tb3 | | tb4 | | tb5 | | tb6 | | tb7 | | user3 | | users | +----------------+ 12 rows in set (0.00 sec) 少使用数据列和表的更名; 约束: 按照功能划分为: NOT NULL ,PRIMARY KEY, UNIQUE KEY, DEFAULT, FOREIGN KEY; 按照数据列的数目划分为: 表级约束,列级约束; 修改数据表: 针对字段的操作: 添加/删除字段,修改列定义,修改列名称等。 针对约束的操作: 添加/删除各种约束 针对数据表的操作:数据表更名(两种方式) 要为自动增长的字段赋值的话可以将其赋值为“DEFAULT”或“NULL”; 可以一次性插入多条记录。 1. INSERT INTO tbl_name VALUE(),(),(); 2. INSERT [INTO] tbl_name SET col_name={expr | DEFAULE}... 3. INSERT [INTO] tbl_name [column_name] SELECT....... mysql> INSERT INTO user2 SET username="BEn",password=‘123‘; 更改数据表信息(UPDATE): UPDATE tbl_name SET column_name = ‘value‘...... 删除记录: DELETE fromtbl_name [WHERE where_conndition].... 当删除一条记录之后,再往表中添加一条记录,此时的id号还是当前最大的id号递增增长。 GROUP BY :查询结构分组 HAVING子句:分组条件 子查询: 1.对查询的结构四舍五入:mysql> SELECT ROUND(AVG(goods_price),2) FROM tdb_goods; ANY SOME ALL修饰比较运算符 2. Mysql的连接查询: 1. 内链接; 2. 左外连接; 3, 右外连接; 使用Mysql实现自定义函数: //定义函数 mysql> CREATE FUNCTION f1() RETURNS VARCHAR(30) -> RETURN DATE_FORMAT(NOW(),‘%Y年%m月,%d日:%H时:%i分:%s秒‘); Query OK, 0 rows affected (0.04 sec) //调用函数 mysql> SELECT f1(); +--------------------------------+ | f1() | +--------------------------------+ | 2015年08月,17日:11时:36分:13秒 | +--------------------------------+ 1 row in set (0.00 sec) mysql> SELECT f1(); //定义带参数的函数: mysql> CREATE FUNCTION f2(num1 SMALLINT UNSIGNED,num2 SMALLINT UNSIGNED) -> RETURNS FLOAT(10,2) UNSIGNED -> RETURN (num1+num2)/2; Query OK, 0 rows affected (0.00 sec) mysql> //调用带参数的函数 mysql> SELECT f2(22,425); +------------+ | f2(22,425) | +------------+ | 223.50 | +------------+ 1 row in set (0.00 sec) mysql> //指定结束分隔符: mysql> DELIMITER // 数据库的存储过程:存储过程是SQl语句和控制欲绝的预编译集合,以一个名称存储并作为一个单元处理。 存储过程的优点: 1. 增强了SQL语句的功能和灵活性; 2. 实现较快的执行速度; 3. 减少了网络流量; 创建存储过程的语法结构: CREATE [DEFINER = {user| CURRENT_USER}] PROCEDURE sp_name([proc_parameter[,......]]) [characteristic ...] routine_body proc_parameter: [IN|OUT|INOUT] param_name type 参数: IN:表示该参数的值必须在调用存储过程时指定。 OUT:表示该参数的值可以被存储过程改变。 INOUT:表示该参数在调用时指定,并且可以被改变和返回。 特性: COMMENT ‘string‘ | {CONTAINS SQL | NO SQL |READS SQL DATA | MODIFIES SQL DATA} | SQL SECURITY {DEFINER | INVOKER} 过程体: 1. 有合法的SQL语句构成 2. 可以是任意的SQL语句 3. 过程体如果为符合结构则使用BEGIN...END语句。 4. 符合结构可以包含声明,循环,控制结构。 创建不带参数的存储过程: mysql> CREATE PROCEDURE sp1() SELECT VERSION(); 存储过程的调用: CALL sp_name([parameter[,....]])//带参数 CALL sp_name[()]//不带参数 //带有IN类型参数的存储过程: mysql> DELIMITER // mysql> CREATE PROCEDURE removeUserById(in id INT UNSIGNED) -> BEGIN -> DELETE FROM users WHERE id=id; -> END -> // Query OK, 0 rows affected (0.00 sec) mysql> 存储过程是没有办法修改的,只能删除存储过程之后再重建。 删除存储过程:DROP PROCEDURE [IF EXISTS] sp_name; mysql> CREATE PROCEDURE removeUserById(IN p_id INT UNSIGNED) -> BEGIN -> DELETE FROM users WHERE id = p_id -> ; -> END -> // Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> SELECT * FROM users; +----+----------------+------+ | id | username | pid | +----+----------------+------+ | 8 | WUYANLONG | NULL | | 9 | DACHENG | NULL | | 10 | CC | NULL | | 11 | DWWUYANLONG | NULL | | 12 | DACHENGD | NULL | | 13 | CCWD | NULL | | 14 | DWWUYANLWWONG | NULL | | 15 | DDWACHENGD | NULL | | 16 | CCWDD | NULL | | 17 | DWWUYANLWXWONG | NULL | | 18 | DDWACHENEGD | NULL | | 19 | CCWWDD | NULL | +----+----------------+------+ 12 rows in set (0.00 sec) mysql> CALL removeUserById(19); Query OK, 1 row affected (0.04 sec) mysql> SELECT * FROM users; +----+----------------+------+ | id | username | pid | +----+----------------+------+ | 8 | WUYANLONG | NULL | | 9 | DACHENG | NULL | | 10 | CC | NULL | | 11 | DWWUYANLONG | NULL | | 12 | DACHENGD | NULL | | 13 | CCWD | NULL | | 14 | DWWUYANLWWONG | NULL | | 15 | DDWACHENGD | NULL | | 16 | CCWDD | NULL | | 17 | DWWUYANLWXWONG | NULL | | 18 | DDWACHENEGD | NULL | +----+----------------+------+ 11 rows in set (0.00 sec) mysql> mysql> DELIMITER // mysql> CREATE PROCEDURE removeUserAndReturnNums(IN p_id INT UNSIGNED,OUT userNums INT UNSIGNED) -> BEGIN -> DELETE FROM users WHERE id = p_id -> ; -> SELECT COUNT(id) FROM users INTO userNums; -> END -> // Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> SELECT * FROM users ; +----+----------------+------+ | id | username | pid | +----+----------------+------+ | 8 | WUYANLONG | NULL | | 9 | DACHENG | NULL | | 10 | CC | NULL | | 11 | DWWUYANLONG | NULL | | 12 | DACHENGD | NULL | | 13 | CCWD | NULL | | 14 | DWWUYANLWWONG | NULL | | 15 | DDWACHENGD | NULL | | 16 | CCWDD | NULL | | 17 | DWWUYANLWXWONG | NULL | | 18 | DDWACHENEGD | NULL | +----+----------------+------+ 11 rows in set (0.00 sec) //传值,有返回值 mysql> CALL removeUserAndReturnNums(18,@nums); Query OK, 1 row affected (0.04 sec) mysql> @nums接受剩余用户数 局部变量只能在BEGIN END之间有效。 通过方法: SET @i= 7;得到的变量称之为用户变量。 存储引擎: Mysql可以将数据以不同的技术存储在文件(内存)中,这种技术就成为存储引擎; 每一种存储引擎都使用了不同的存储机制,索引技巧,锁定水平,最终提供广泛且不同的功能。 Mysql支持的存储引擎: 1. MyISAM 2. INNODB 3. Memory 4. CSV 5. Archive; 相关知识: 并发控制:当多个连接对记录进行修改时保证数据的一致性和完整性。 锁: 共享锁(读锁):在同一时间段内多个用户可以读取同一资源,读取过程中数据不会发生任何变化。 排他锁(写锁):在任何时候只能有一个用户写入资源,当进行写锁时会阻塞其他的读锁或者写锁操作。 锁的颗粒(粒度):加锁只能增加最对的,不增加最大的。 1.表锁:是一种开销最小的锁策略。 吧整张表锁起来之后其他用户就不可以操作。 2.行锁:是一种开销最大的锁策略。 事务:事务用于保证数据库的完整性; -原子性(Atomicity) -一致性(Consistenity) -隔离性(Isolation) -持久性(Durability) 外键是为了保证数据的一致性; 索引是对数据表中的一列或者多列进行排序的一种结构; 普通索引 唯一索引 全文索引 btree索引 hash索引
标签:
原文地址:http://www.cnblogs.com/blogofwyl/p/4742685.html