码迷,mamicode.com
首页 > 数据库 > 详细

SQL语句表操作

时间:2019-01-19 17:34:50      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:alter   varchar   har   删除   max   oid   数据类型   efault   rod   

创建表
create table student(
    id int not null auto_increment primary key comment ‘这是主键‘,
    sno varchar(10) comment ‘这是姓名‘,
    sex int default 1 comment ‘这是性别 1代表男 0代表女‘,
    brithday date
);
数据类型: int date datetime  timestemp  varchar(20)
查看表结构
desc student;
插入数据
insert into score(sno,cno,degree) values
(‘103‘,‘3-245‘,86);

delete from student where ...

update student set ssex="女" where...

select sno from student where ...
group  by  字段1
连接查询:扩充字段
Select * from student,teacher;
子查询:把查询结果当条件
Select sno,cno from score where degree=(select max(degree) from score);
联合查询
Select sno,sname from student union select tno,tname from teacher;

添加字段
ALTER TABLE order01 ADD COLUMN odesc VARCHAR(50);
删除字段
ALTER TABLE order01 DROP COLUMN money;
修改字段类型
ALTER TABLE order01 MODIFY COLUMN otime VARCHAR(20);
修改字段名称
ALTER TABLE order01 CHANGE COLUMN otime ordertime VARCHAR(20);
修改表名
ALTER TABLE order01 RENAME TO order02;
修改数据
UPDATE order02 SET ordertime=‘2018-12-12‘ WHERE oid=2;
删除数据
DELETE FROM order02 WHERE oid=2;
全表删除
DELETE FROM order02;
TRUNCATE TABLE order02;
查询所有字段
SELECT*FROM order02;
查询指定字段
SELECT oid,productname,odesc FROM order02;
查询时添加常量列
SELECT oid AS‘编号‘,productname FROM order02;

sql语句例子
新建成绩表
CREATE TABLE score(
    sid INT,
    sname VARCHAR(10),
    ssex CHAR(2),
    html DOUBLE,
    js DOUBLE,
    jquery DOUBLE
);
DROP TABLE score;
查询时合并列
SELECT sname,(html+js+jquery) AS ‘总成绩‘ FROM score;
查询时去除重复列
SELECT DISTINCT ssex FROM score;
SELECT DISTINCT(ssex) FROM score;
逻辑条件查询
SELECT * FROM score WHERE sid=1 AND ssex=‘男‘;
SELECT * FROM score WHERE sid=2 OR ssex=‘女‘;
比较条件查询
SELECT * FROM score WHERE html>=60 AND js<=80;
SELECT * FROM score WHERE html>=60 AND html<=100;
SELECT * FROM score WHERE html BETWEEN 60 AND 100;
SELECT * FROM score WHERE <>60 AND js>8;
判断是否为null
SELECT * FROM score WHERE js IS NOT NULL;
判断是否为空字符串
SELECT * FROM score WHERE ssex <> ‘‘;
SELECT * FROM score WHERE ssex = ‘‘;
模糊查询
SELECT * FROM score WHERE sname LIKE ‘张%‘;
SELECT * FROM score WHERE sname LIKE ‘__‘;
SELECT * FROM score WHERE sname LIKE ‘%张%‘;
SELECT SUM(js) AS ‘js成绩‘ FROM score;
SELECT AVG(html) AS ‘html平均成绩‘ FROM score;
SELECT MAX(jquery) FROM score;
SELECT MIN(js) FROM score;
求本班多少人
SELECT COUNT(*) FROM score;
第一页
SELECT * FROM score LIMIT 0,1;
第二页
SELECT * FROM score LIMIT 2,1;
查询排序
SELECT * FROM score ORDER BY html ASC;
SELECT * FROM score WHERE ssex=‘男‘ ORDER BY html DESC;
SELECT * FROM score ORDER BY js ASC,jquery DESC;
分组查询
SELECT ssex,COUNT(*) FROM score GROUP BY ssex;
查询总人数大于2的性别(HAVING)
按性别分组
SELECT ssex FROM score GROUP BY ssex
HAVING COUNT(*)>2;

SQL语句表操作

标签:alter   varchar   har   删除   max   oid   数据类型   efault   rod   

原文地址:https://www.cnblogs.com/wjwap/p/10292137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!