展开

常用的sql语句

发布于 2021-08-02 11:23:37     浏览 340

常用的sql语句

问题解析:

【】

1、 1.数据库操作 2、 1、说明:创建数据库 3、 CREATE DATABASE 数据库名 4、 2、说明:删除数据库 5、 drop database 数据库名 6、 3、选择数据库 7、 USE 数据库名 8、 二、表操作 9、 1、说明:创建新表 10、 create table 表名(列名 类型 [not null:非空] [primary key:主键] [auto_increment:自增] [comment:备注],col2 type2 [not null],..) 11、 2、根据已有的表创建新表: 12、 create table 新表名 as select 列名1,列名2… from 旧表名 13、 3、说明:删除新表 14、 drop table 表名 15、 4、说明:增加一个列 16、 Alter table 表名 add column 列名 类型 17、 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 18、 5、说明:添加主键: Alter table 表名 add primary key(列名) 19、 说明:删除主键: Alter table 表名 drop primary key 20、 6、外键约束 21、 alter table 外键表名 add constraint 约束名称 foreign key (外键字段) references 主键表名(约束列名) 22、 7、说明:创建索引:create [unique] index 索引名 on 表名(列名) 23、 删除索引:ALTER TABLE 表名 DROP INDEX 索引名 24、 8、说明:创建视图: 25、 CREATE VIEW 视图名 AS 26、 SELECT 列名 27、 FROM 表名 28、 删除视图:DROP VIEW 视图名 29、 三、说明:几个简单的基本的sql语句 30、 选择:select * from 表名 where 范围 31、 插入:insert into 表名(列名,列名) values(value1,value2) 32、 删除:delete from 表名 where 范围 33、 更新:update 表名 set 列名=value1 where 范围 34、 查找:select * from 表名 where 列名 like ’%value1%’ 35、 总数:select count(0) as totalcount from 表名 36、 求和:select sum(列名) as sumvalue from 表名 37、 平均:select avg(列名) as avgvalue from 表名 38、 最大:select max(列名) as maxvalue from 表名 39、 最小:select min(列名) as minvalue from 表名 40、 升降序 41、 默认情况下,它是按升序排列。 42、 升序 SELECT * FROM 表名 ORDER BY field ASC 43、 降序 SELECT * FROM 表名 ORDER BY field DESC 44、 分组 select 列名 from 表名 group by 列名 45、 分页 select 列名 from 表名 limit Index(起始页数索引),page(显示几条数据) 46、 子查询(表名1:a 表名2:b) 47、 select 列名 from a where a IN (select 列名 from b ) 或者: select a,b,c from a where a IN (1,2,3) 48、 外连接查询(表名1:a 表名2:b) 49、 select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c 50、 内连接查询(表名1:a 表名2:b) 51、 select a.a, a.b, a.c, b.c, b.d, b.f from a inner JOIN b ON a.a = b.c 52、 2.添加约束 53、 alter table 添加约束表名 add constraint 约束名称 约束类型 (约束字段) 54、 删除约束 55、 alter table 表名 dropconstraint 约束名 56、 所有约束通用格式。

相关推荐

猜你可能喜欢

点击加载更多