`
andy136566
  • 浏览: 285847 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

MySql基本操作总结

阅读更多

启动和停止服务:net start mysql

                        net stop mysql

 

进入交互式命令行:mysql -u root -p

                           mysql -u root -h db.imainary.com -p

 

数据库操作: show databases;

                    create database andy;

                   use andy;

                   drop database andy;

 

表管理:use andy;

            show tables;

             describe tablename;

            create table user(id int not null primary key,name  varchar(22)  not null);

            drop tablename;

 

索引:create index index_name on tablename(colum1,colum2,...);

 

 

 

管理数据

 

插入:insert into table_name(column1) values (value1);

         insert into table_name set name='andy';

 

序列的生成:定义表时:auto_increment

                  update table_name set id=LAST_INSERT_ID(id+1);

 

更新:update table_name set column1=value1 [where clause];

 

删除:delete author from Author where Author.id=1;

 

查询:select column1 from table1 [where clause];

         select databse();   #返回当前数据库的名称

 

内连接:select book.title,author.name from author, book where book.asuthor=author.id;

 

外连接:

           select book.title,author.name from author left join on book  book.asuthor=author.id;

           将包括left join左边表的所有行,right join同理。

 

自然外连接:

           select my_prod.name from my_prod natural left join their_prod

           这个自然连接将列出在my_prod和their_prod表中具有相同项的所以产品名称。

 

联合:select fname,lname from author

         union select name,nname from editor;

 

别名:as

 

基本排序:select column1 from table order by column1;

               desc反序排列。

 

分组:select rank from people group by rank;

         利用分组可以求得每类fg:工资的平均值

         select rand,avg(salary) from people group by rank;

 

限定结果:where子句会扫描数据库中的表;having查看取出后的行!

              select rank,avg(salary) from people where rank <> 'Private' group by rank having avg(salary)>10000;

 

              select * from people order by name limit 19,30;

             #查看第20行到49行记录

 

逻辑运算符:and、or、not

 

成员测试:in、between..and..

 

模式匹配:select name from people where name like 'andy%'

               %匹配任意数量的字符;_匹配单个字符。

正则表达式匹配:like是精确匹配

    select name from people where name regexp 'andy' #任意位置andy

 

 

 

 

高级特性:

全文搜索:全文搜索的关键之处在于FULLTEST索引。

               create table document(

                    url varchar(25) not null primary key,

                    title varchar(100) not null,

                    page_text text not null,

                    fulltext(title,page_text)               

               );

基于此表的结构,可以搜索在页面标题或页面主体中的任何位置包含单词“Mysql”的文档。必须保证根据索引来构造查询,而不是根据列。换句话说,可以同时匹配表中的title和page_text,而不能查找只存在于标题中的单词,除非只根据title创建单独的fulltext索引。

      select url from document where match (title,page_text) against ('java');

 

如果短语java在超过半数的行都有出现,因此被认为是一个没有检索意义的词(stopword)而被忽略。对于文本匹配,常见的stopword是“the”、“but”。

 

Boolean模式:

      执行更为复杂的全文搜索功能,它使用的语法与internet搜索引擎所用的语法相同。

      select url,title from document where match (title,page_text) against ('+mysql' -'java' in boolean mode);

 

一次添加大量数据时,应该去掉fulltext索引,插入数据,然后再重新创建索引。向带有fulltext索引的表中插入数据将付出昂贵的代价,而一次建立索引则能更好地工作。

 

事务处理;

           建表时指定type=InnoDB或者BDB

           set autocommit=0;

           begin;

           ...

           commit;

           在commit之前可以发出rollback;

 

表锁定:

          表锁定是低水平的事务处理。Mysql允许锁定一组表,使得只有一个客户可以使用。

           支持三种锁定:读、本地读和写;

           读锁定时将表锁定以供该客户及所有其他客户读取。只要在锁定状态,就不能对被锁定的表进行写入。读锁定和本地读锁定的区别在于,本地读锁定允许一个客户执行非冲突的insert语句,只要在锁定时没有发生来自mysql外部,对mysql文件的改变即可。否则,则需读锁定。

          写锁定将指定的表锁定,不允许任何其他客户对它进行任何访问、包括读和写。

      lock table account write;

      ...

      unlock tables;

         

 

批处理:

 

     命令行装载:mysql -h somehost -u uid -p < filename;

   

      Load命令:load data local infile 'books.dat' into table book;

                     

          load data local infile 'books.dat' into table book  fields terminated by ',';

 

     从mysql中取出数据:select * into outfile 'books.dat' fields enclosed by '"' terminated by ',' from book;

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics