sql命令二

简单SQL命令

重定向执行SQL脚本

//1、编写SQL脚本
//2、在SQL脚本当前目录下执行SQL命令
mysql -D database -u userName -p < createTable.sql

重命名table

alter table tableName rename newTableName;

备份数据库

mysqldump -h localhost -u root -p DBName > DBName_backup.sql

修改数据库表结构

//表 增加列test
alter table tableName add(test char(10));

//表 test列后增加age列
alter table tableName add age tinyint(2) not null after test;

//表 修改列test
alter table tableName modify test char(20) not null;

//表 修改列test名为test1
alter table tableName Change test test1 test1Type;

//表 修改列test的默认值
alter table tableName alter test set default 'system';

//表 去掉test默认值
alter table tableName alter test drop default;

//表 去掉列test
alter table tableName drop column test;

//表 删除主键
alter table tableName drop primary key;

//表 增加主键
alter table tableName add primary key DoubleKey(id,name);

数据插入

//将开发部 插入表 department
insert into department(name,content) values('开发部','开发部');

//

删除数据

//删除指定的name='开发部' 行
delete from department where name='开发部';

更新操作

//将开发部更新为前端开发部
update department set name='前端开发部' where name='开发部';

查询操作

//1、显示开发部的人员和职位
select a.name,b.name,department_name,c.name,position_name 
    from staffer a,department b,s_position c
    where a.department_id = b.id and a.position_id = c.id and b.name = '开发部';

//2、显示开发部的人数
select count(*) 
    from staffer a,department b
    where a.department_id = b.id and b.name = '开发部';

//3、显示各部门人数
select count(*),b.name
    from staffer a,department b
    where a.department_id = b.id
    group by b.name;

//4、显示第3到7行(结果集标号从0开始)
//limit 方法的第一个参数表示从第几个结果开始
//第二个参数表示显示几个结果

//order by 用于排序(asc:升序,desc:降序)
select * 
    from department
    order by id
    limit 2,5;

//5、查询mysql.user表中用户
//Distinct 表示结果项不重复
//Concat 用户联合多个字符或者列
select Distinct Concat('User:',user,'@',host,';') as Query from mysql.user;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值