MySQL与一些简单查询命令

1.MySQL服务的启动、停止

启动: net start MySQL  
停止:  net stop MySQL

2. 登录到MySQL 
   mysql -h 主机名 -u 用户名 -p
   如:登陆本机的MySQL数据库命令:
     mysql -u root -p 
3.Mysql基本操作命令
show databases;                      // 显示数据库列表
use db名                                 //使用一个数据库
show tables;                            //列出所有的表格
desc table名;                         //列出哪个表属性
show create table   table名     //显示表格创建的所有属性

mysqldump - u 用户名 -p 密码 数据库名  >路径名     //备份数据库
source  路径名             //还原 ,需要指定恢复到的数据库(use 哪个数据库)

4SQL操作:
     1.创建一个数据库 :          create  database db名 character set 字符集 collate 校对规则
     2.删除一个数据库 :          drop    database db名
     3.创建一个新表:              create  table  table名(col1 type1 [not null] [primary key][auto_increment],                    col2 type2 [not null],..)   
          3.1.根据已有的表创建新表:   create table  tab_new like  tab_old (使用旧表创建新表)
          3.2.create table  tab_new as select col1,col2… from tab_old definition only
     4.删除表:                        drop table  table名
     5.增加列:                        Alter table  table名  add  列名   coltype
                                             Alter table  table名  modify 列名 coltype 
                                             Alter table  table名  drop 列名

      6修改表名               alter table 表名 rename to 新表名
     7修改列名               alter table 表名 change column 旧列名 新名 [其他列的属性可选] ;


二 。创建一个带外键的表:
create table user(
id int(11)
);
create table weibo(
weiboID int(11),
id int(11),
foreign key (id) references user(id)
);
几个简单的基本的sql语句
选择:select * from 表名 where 范围
插入:insert into 表名 (field1,field2) values(value1,value2)
删除:delete from 表名 where 范围
更新:update table1 set  属性1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!
排序:select * from 表名 order by属性1,属性2 [desc]
总数:select count  as totalcount  from 表名
求和:select sum(属性1) as sumvalue from 表名
平均:select avg(属性1) as avgvalue from  表名
最大:select max(属性1) as maxvalue from 表名
最小:select min(属性1) as minvalue from 表名

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值