MYSQL数据库-其他

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:

 CREATE DATABASE mysql_shiyan;

 use mysql_shiyan;

 CREATE TABLE department
(
dpt_name CHAR(20) NOT NULL,
people_num INT(10) DEFAULT '',
CONSTRAINT dpt_pk PRIMARY KEY (dpt_name)
); CREATE TABLE employee
(
id INT(10) PRIMARY KEY,
name CHAR(20),
age INT(10),
salary INT(10) NOT NULL,
phone INT(12) NOT NULL,
in_dpt CHAR(20) NOT NULL,
UNIQUE (phone),
CONSTRAINT emp_fk FOREIGN KEY (in_dpt) REFERENCES department(dpt_name)
); CREATE TABLE project
(
proj_num INT(10) NOT NULL,
proj_name CHAR(20) NOT NULL,
start_date DATE NOT NULL,
end_date DATE DEFAULT '2015-04-01',
of_dpt CHAR(20) REFERENCES department(dpt_name),
CONSTRAINT proj_pk PRIMARY KEY (proj_num,proj_name)
); CREATE TABLE table_1
(
l_1 INT(10) PRIMARY KEY,
l_2 INT(10),
l_3 INT(10)
); #INSERT INTO department(dpt_name,people_num) VALUES('\u90e8\u95e8',\u4eba\u6570); INSERT INTO department(dpt_name,people_num) VALUES('dpt1',11);
INSERT INTO department(dpt_name,people_num) VALUES('dpt2',12);
INSERT INTO department(dpt_name,people_num) VALUES('dpt3',10);
INSERT INTO department(dpt_name,people_num) VALUES('dpt4',15); #INSERT INTO employee(id,name,age,salary,phone,in_dpt) VALUES(\u7f16\u53f7,'\u540d\u5b57',\u5e74\u9f84,\u5de5\u8d44,\u7535\u8bdd,'\u90e8\u95e8'); INSERT INTO employee(id,name,age,salary,phone,in_dpt) VALUES(01,'Tom',26,2500,119119,'dpt4');
INSERT INTO employee(id,name,age,salary,phone,in_dpt) VALUES(02,'Jack',24,2500,120120,'dpt2');
INSERT INTO employee(id,name,salary,phone,in_dpt) VALUES(03,'Jobs',3600,019283,'dpt2');
INSERT INTO employee(id,name,salary,phone,in_dpt) VALUES(04,'Tony',3400,102938,'dpt3');
INSERT INTO employee(id,name,age,salary,phone,in_dpt) VALUES(05,'Rose',22,2800,114114,'dpt3'); #INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(\u7f16\u53f7,'\u5de5\u7a0b\u540d','\u5f00\u59cb\u65f6\u95f4','\u7ed3\u675f\u65f6\u95f4','\u90e8\u95e8\u540d'); INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(01,'proj_a','2015-01-15','2015-01-31','dpt2');
INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(02,'proj_b','2015-01-15','2015-02-15','dpt1');
INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(03,'proj_c','2015-02-01','2015-03-01','dpt4');
INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(04,'proj_d','2015-02-15','2015-04-01','dpt3');
INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(05,'proj_e','2015-02-25','2015-03-01','dpt4');
INSERT INTO project(proj_num,proj_name,start_date,end_date,of_dpt) VALUES(06,'proj_f','2015-02-26','2015-03-01','dpt2');
上一篇:ios weibo 第二天 设置导航栏属性,添加加号按钮


下一篇:Windows Thin PC(7月2日发布)下载+激活+汉化