常用SQL语句

创建表

复制代码
create table emp(

id decimal,

name char(10),

age decimal)
复制代码

 

删除表

DROP TABLE EMP

 

插入默认值

复制代码
CREATE TABLE EMP(

  Id decimal NOT NULL,

  Name char(10) NOT NULL,

  Age decimal NOT NULL DEFAULT 18)
复制代码

 

添加列

ALTER TABLE EMP ADD COLUMN Comments char(50) NOT NULL DEFAULT 'COMMENTS'

 

删除列

ALTER TABLE EMP DROP COLUMN Comments

 

修改列

ALTER TABLE EMP MODIFY COLUMN Comments char(30)

 

创建索引

CREATE INDEX Name_index ON EMP (Name)

 

查询数据

SELECT Name FROM EMP

 

查询不重复的数据

SELECT DISTINCT Name FROM EMP

 

对查询结果排序

SELECT Name FROM EMP ORDER BY Name

 

条件查询

SELECT Name FROM EMP WHERE Age < 20

SELECT Name FROM EMP WHERE Age BETWEEN 20 AND 30

 

插入数据

INSERT INTO EMP VALUES(123,'xingoo',18)

 

修改数据

UPDATE EMP SET Name='xingoo1111' WHERE Name='xingoo'

 

删除数据

DELETE FROM EMP WHERE Age<20

 

求平均数

SELECT AVG(Age) FROM EMP

 

取排序的结果的前3条数据mysql和Oracle

select name from emp order by age desc limit 3 (这是mysql的语法)

select name from emp order by age desc limit (3,3) (这是mysql的语法,从第3条开始获取,去3条数据)

select age , count(*) as count forn emp group by age order by count desc limit 3 (这是mysql的语法,获取最重复次数最多的记录)

Oracle特有的伪列 RowNum 相当于行号,有1开始递增,先获取行,才命名为第一行 (表示查询结果后的行数)

select name from (select * from emp where age is not null order by age desc )empwhere rownum <= 3 (oracle的语法) 排序问题,null值为最大

select * from (select a.*,rownum rn from emp a ) where rn>=10 and rn <=20 (oracle的语法 去第10条到第20条数据)



 

查询条数

SELECT COUNT(DISTINCT Name) FROM EMP

SELECT COUNT(*) FROM EMP

posted @ 2015-10-23 10:15  douding1828  阅读(147)  评论(0编辑  收藏  举报