FROM:实验楼
索引:
当表中有大量记录时,若要对表进行查询,没有索引的情况是全表搜索。而如果在表中已建立索引,在索引中找到符合查询条件的索引值,通过索引值就可以快速找到表中的数据。
建立索引:
$ ALTER TABLE t_name ADD INDEX index_name(col_name);
or $ CREATE INDEX index_name ON t_name(col_name);
E.G:
$ ALTER TABLE employee ADD INDEX idx_id(id);#在employee表的id列上建立名为idx_id的索引
查看索引:
$ SHOW INDEX FROM t_name;
=======================================================
视图:
视图是从一个或多个表中导出来的表,是一种虚拟存在的表。
数据库只存放了视图的定义,而没有存放视图中的数据,数据还放在原来的表;
创建视图:
$ CREATE VIEW view_name(col_a,col_b...) AS SELECT col1,col2... FROM t_name;
在SELECT语句中使用子查询或连接查询就可以建立多张表的视图
=======================================================
导入:把文件里的数据保存进一个表。
导入语句:
$ LOAD DATA INFILE 'file_path' INTO TABLE t_name;
导出:
$ SELECT col_1,col_2... INTO OUTFILE 'path/file_name' FROM t_name;
备份:将结构全部存为另一个文件,导出只是将文件内容进行另存。
$ mysqldump -u root db_name > dump_file_name; #备份整个数据库
$ mysqldump -u root db_name t_name > dump_file_name; # 备份整个表
用备份文件恢复数据库:
$ source /path/file_name
or $ CREATE DATABASE db_name;
$ mysql -u root db_name < back_file_name
SQL_file:
1 CREATE DATABASE mysql_shiyan; 2 3 use mysql_shiyan; 4 5 CREATE TABLE department 6 ( 7 dpt_name CHAR(20) NOT NULL, 8 people_num INT(10) DEFAULT '10', 9 CONSTRAINT dpt_pk PRIMARY KEY (dpt_name) 10 ); 11 12 CREATE TABLE employee 13 ( 14 id INT(10) PRIMARY KEY, 15 name CHAR(20), 16 age INT(10), 17 salary INT(10) NOT NULL, 18 phone INT(12) NOT NULL, 19 in_dpt CHAR(20) NOT NULL, 20 UNIQUE (phone), 21 CONSTRAINT emp_fk FOREIGN KEY (in_dpt) REFERENCES department(dpt_name) 22 ); 23 24 CREATE TABLE project 25 ( 26 proj_num INT(10) NOT NULL, 27 proj_name CHAR(20) NOT NULL, 28 start_date DATE NOT NULL, 29 end_date DATE DEFAULT '2015-04-01', 30 of_dpt CHAR(20) REFERENCES department(dpt_name), 31 CONSTRAINT proj_pk PRIMARY KEY (proj_num,proj_name) 32 ); 33 34 CREATE TABLE table_1 35 ( 36 l_1 INT(10) PRIMARY KEY, 37 l_2 INT(10), 38 l_3 INT(10) 39 ); 40 41 42 43 #INSERT INTO department(dpt_name,people_num) VALUES('u90e8u95e8',u4ebau6570); 44 45 INSERT INTO department(dpt_name,people_num) VALUES('dpt1',11); 46 INSERT INTO department(dpt_name,people_num) VALUES('dpt2',12); 47 INSERT INTO department(dpt_name,people_num) VALUES('dpt3',10); 48 INSERT INTO department(dpt_name,people_num) VALUES('dpt4',15); 49 50 51 #INSERT INTO employee(id,name,age,salary,phone,in_dpt) VALUES(u7f16u53f7,'u540du5b57',u5e74u9f84,u5de5u8d44,u7535u8bdd,'u90e8u95e8'); 52 53 INSERT INTO employee(id,name,age,salary,phone,in_dpt) VALUES(01,'Tom',26,2500,119119,'dpt4'); 54 INSERT INTO employee(id,name,age,salary,phone,in_dpt) VALUES(02,'Jack',24,2500,120120,'dpt2'); 55 INSERT INTO employee(id,name,salary,phone,in_dpt) VALUES(03,'Jobs',3600,019283,'dpt2'); 56 INSERT INTO employee(id,name,salary,phone,in_dpt) VALUES(04,'Tony',3400,102938,'dpt3'); 57 INSERT INTO employee(id,name,age,salary,phone,in_dpt) VALUES(05,'Rose',22,2800,114114,'dpt3'); 58 59 60 61 #INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(u7f16u53f7,'u5de5u7a0bu540d','u5f00u59cbu65f6u95f4','u7ed3u675fu65f6u95f4','u90e8u95e8u540d'); 62 63 INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(01,'proj_a','2015-01-15','2015-01-31','dpt2'); 64 INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(02,'proj_b','2015-01-15','2015-02-15','dpt1'); 65 INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(03,'proj_c','2015-02-01','2015-03-01','dpt4'); 66 INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(04,'proj_d','2015-02-15','2015-04-01','dpt3'); 67 INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(05,'proj_e','2015-02-25','2015-03-01','dpt4'); 68 INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(06,'proj_f','2015-02-26','2015-03-01','dpt2'); 69 70 71 72 73 74