`
frand_feng
  • 浏览: 18937 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

mysql中的命令总结

    博客分类:
  • db
 
阅读更多

    用了一段时间的mysql数据库了,现在对mysql数据库中经常用的命令小总结一下,以免以后忘记:

    1,连接数据库:

mysql -u root -p root

    2,退出数据库:

quit;或者exit; 

     3,显示数据库:

show databases; 

     4,在使用某个数据库时,一定要先切换的该数据库,命令为:

use databasename;

     5,查看某个数据库里面的有哪些表时:

show tables;

     6,查看某个表的结构:

desc tablename;

     7,查看当前时间和数据库版本:

select now(),version(); 

     8,创建删除数据库:

create database student;
drop database student;

     9,创建删除表:

create table falcatys(falcaty_id int not null auto_increment primary key,falcaty_name char(30) not null);
drop table falcatys;

    10,显示创建表的信息:

show create table falcatys;

     11,修改表中某一列的信息:

alter table falcatys modify column falcaty_name char(30) CHARACTER SET utf8 not null;

     12,  修改表的创建信息:

alter table falcatys DEFAULT CHARSET=utf8;

     13,插入删除数据(一次可以插入一条或多条数据):

insert into falcatys(falcaty_name,falcaty_students_num) values('资源与环境学院',0);
delete * from falcatys where fallacy_id = 1;

     14,查询表中数据:

select * from falcatys;  --查询表中的所有数据  
select fallacy_id,fallacy_name from falcatys;  --查询给定的列

     15,添加列删除列:

alter table professions add column falcaty_id int;
alter table professions drop column falcaty_id;

    16,添加删除主键约束:

alter table professions add constraint profession_id primary key professions(profession_id);
alter table professions drop primary key;

    17,添加外键约束:

alter table professions add constraint falcaty_profession_class foreign key(falcaty_id) references falcatys(falcaty_id);
alter table professions drop foreign key fallacy_profession_class;

     18,添加删除唯一约束:

alter table t_stu add constraint uk_stucode unique(stucode);
alter table t_stu drop index uk_stucode;

     19, 重命名表:

rename table t_stu to t_user;

     20,导入数据库:


     21,更新表中的数据:

update t_stu set stuname='jack'[where id=1] ;--修改id为1的名字为jack,  如果不写中括号里面的where条件,则会把所有的姓名都改为jack  
update t_stu set stuage=stuage+1;   --将表中stuage这一列的数都加一  
update t_stu set stuage=23 where id id(1,2,4,5); --将id号为1,2,4,5的        stuage改为23   
update t_stu set stuage=27 where id between 1 and 4;--将id号在1和4之间的stuage改为27,相当于where id>=1 and id<=4;  
update t_stu set stuage=24 where stuage is null;  
update t_stu set stuage=21 where stuage is not null; 

     22,删除表中所有数据:

truncate table t_stu;  --删除t_stu表中的所有数据 

     23,去除重复的查询:

select distinct vend_id from products;

     24,分页查询:

select id,stuname,stuage,stuaddress from t_stu limit 0,5;--第一页从0开始往后数5个显示出来  
select id,stuname,stuage,stuaddress from t_stu limit 5,5;--第二页从5开始往后数5个显示出来

     25,排序查询:

select prod_id,prod_name,vend_id,prod_price from products order by prod_price asc,prod_name asc;--先按prod_price升序排列,当prod_price相同时再按prod_name排序
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics