/*创建 shop 数据库*/
CREATE DATABASE IF NOT EXISTS `shop`;
/* 创建 账户表 */
CREATE TABLE IF NOT EXISTS `account` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(32) NOT NULL,
`cash` DECIMAL(9,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB;
INSERT INTO `account` (`name`,`cash`) VALUES ('A',2000.00);
INSERT INTO `account` (`name`,`cash`) VALUES ('B',10000.00);
/* 事务处理*/
SELECT * FROM account;
SET autocommit= 0;
START TRANSACTION;
UPDATE account SET cash = cash - 500 WHERE NAME = 'A';
SELECT * FROM account;
ROLLBACK;
SET autocommit = 1;
SELECT * FROM account;
/* 索引 */
#添加索引方式一:创建表,声明列属性时添加上
CREATE TABLE test1(
id INT(4) PRIMARY KEY,
testno VARCHAR(10) UNIQUE,
c VARCHAR(50),
d VARCHAR(20),
e TEXT,
INDEX `index_c`(c,d),
FULLTEXT(e) #全文索引
)ENGINE=MYISAM;
#添加索引方式二:创建表,将所有列都声明完毕后,再添加索引
CREATE TABLE test2(
id INT(4),
testno VARCHAR(10),
c VARCHAR(50),
d VARCHAR(20),
e TEXT,
PRIMARY KEY(id),
UNIQUE(testno),
INDEX `index_c`(c,d),
FULLTEXT(e)
)ENGINE=MYISAM;
#添加索引方式三:创建表,将所有列都声明完毕后,修改表,再添加索引
CREATE TABLE test2(
id INT(4),
testno VARCHAR(10),
c VARCHAR(50),
d VARCHAR(20),
e TEXT,
)ENGINE=MYISAM;
ALTER TABLE test3 ADD PRIMARY KEY (id);
ALTER TABLE test3 ADD UNIQUE KEY(testno);
ALTER TABLE test3 ADD INDEX(c,d);
ALTER TABLE test3 ADD FULLTEXT(e);
EXPLAIN SELECT * FROM student WHERE studentno='1000';
ALTER TABLE student ENGINE=MYISAM;
ALTER TABLE student ADD FULLTEXT(studentname);
EXPLAIN SELECT * FROM student WHERE studentname LIKE '李%';
CREATE TABLE `student` (
`StudentNo` INT(4) NOT NULL COMMENT '学号',
`LoginPwd` VARCHAR(20) DEFAULT NULL,
`StudentName` VARCHAR(20) DEFAULT NULL COMMENT '学生姓名',
`Sex` TINYINT(1) DEFAULT NULL COMMENT '性别,取值0或1',
`GradeId` INT(11) DEFAULT NULL COMMENT '年级编号',
`Phone` VARCHAR(50) NOT NULL COMMENT '联系电话,允许为空,即可选输入',
`Address` VARCHAR(255) NOT NULL COMMENT '地址,允许为空,即可选输入',
`BornDate` DATETIME DEFAULT NULL COMMENT '出生时间',
`Email` VARCHAR(50) NOT NULL COMMENT '邮箱账号,允许为空,即可选输入',
`IdentityCard` VARCHAR(18) DEFAULT NULL COMMENT '身份证号',
PRIMARY KEY (`StudentNo`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
SHOW INDEX FROM sutdent;
ALTER TABLE student ADD PRIMARY KEY(`StudentNo`);
ALTER TABLE student ADD UNIQUE INDEX(IdentityCard);
ALTER TABLE student ADD INDEX (Email);
/*先修改为MyISAM类型数据表*/
ALTER TABLE student ENGINE=MYISAM;
ALTER TABLE student ADD FULLTEXT(StudentName);
#显示索引信息
SHOW INDEX FROM student;
#删除索引
DROP INDEX testno ON test3;
ALTER TABLE test3 DROP INDEX c;
ALTER TABLE test3 DROP INDEX e;
#删除主键索引时,特殊直接删除主键
ALTER TABLE test3 DROP PRIMARY KEY;
#不考虑结构只备份数据可以使用命令操作比较简单
#注意:备份出去的文件不可以提前存在
SELECT studentno, studentname INTO OUTFILE 'c:/test.sql' FROM student;
#将备份出去的数据恢复到test数据库的stutab表中
USE test;
CREATE TABLE stutab(
id INT(4),
sname VARCHAR(20)
);
LOAD DATA INFILE 'c:/test.sql' INTO TABLE stutab(sid,sname);
MySQL数据库之简单的事务处理、索引、备份了解练习
最新推荐文章于 2024-06-20 08:45:00 发布