【java开发系列】—— 常用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

 

查询条数

SELECT COUNT(DISTINCT Name) FROM EMP

SELECT COUNT(*) FROM EMP

 

【java开发系列】—— 常用SQL语句

上一篇:配置Spring JDBC的事务回滚失效


下一篇:MySQL主机127.0.0.1与localhost区别总结