您的位置:首页 > 数据库 > MySQL

Mysql常用命令

2013-06-21 07:32 429 查看
Mysql常用命令

放弃正在输入的命令:\c

退出sql程序:\q

查看mysql服务器状态信息:\s

查看数据库表的列:show tables;

域完整性:主键primary key;
外键foreign key;

建立一个数据库:create database
库名;

创建表及列名:create table
表名(

列名1
数据类型
约束,

列名2
数据类型
约束,

列名3
数据类型
约束,

例:

->id int(10) not null primary key,

->name varchar(20) not null);

在已创建的表中添加列:alter table
表名 add
列名
属性;

在已创建的表中修改列的属性:alter table
表名 change (原)列名

(改后)列名 (改后)属性;

主键及自增:id int(10) not null primary key
auto_increment

删除表:drop table
表名;

删除数据库:drop database
库名;

向表中添加一条记录:insert into
表名(列名1,列名2)values(**,**);

向表中添加多条记录:insert into
表名(列名1,列名2)

values(**,**),

(**,**),

(**,**),

(**,**);

更改记录操作:update
表名 set
列名=更新值 where
更新条件;

例如:update student set sname="Tom" where sname="Alex";

删除记录操作:delete from
表名 where
删除条件

例如:delete from student where sage<18;

查询记录操作:select
列名 from
表名 where
查询条件 order by
列名 asc/desc;

例如:select * from student;

使用集函数:count(列名)
计算元素的个数

sun(列名)
对某一列值求和,但必须是整数

avg(列名)
对某一列的值计算平均值

max(列名)
找出某一列的最大值

min(列名)
找出某一列的最小值

例如:1.查询学生总数

select count(*) from student;

2.查询选修课程的学生人数

select count(distinct studentid) from sc;

3.查询1号课程的学生平均成绩

select avg(grade) from sc where courseid=1;

4.查询1号课程的学生最高分和最低分

select max(grade) as '最高分',min(grade) as '最低分

from sc where courseid=1;

5.查询每个学生的平均成绩

select studentid,avg(grade) as '平均成绩' from sc

group by studentid ;

6.查询学生的平均成绩在70分以上的

select studentid,avg(grade) as '平均成绩' from sc

group by studentid having avg(grade)>70;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: