常用SQL语句集锦

MySQL适用

1、如图所示,根据Coord字段内容填充X/Y字段,并调整Coord字段格式(Coord字段原为[Latitude,Longitude]格式,需要将其调整为[Longitude,Latitude]格式)

常用SQL语句集锦

UPDATE test.e_temple t SET t.X = RIGHT(t.Coord, 10);

UPDATE test.e_temple t SET t.Y = LEFT(t.Coord, 9);

UPDATE test.e_temple t SET t.Coord = CONCAT(RIGHT(t.Coord, 10),',',LEFT(t.Coord, 9));

MS SQLServer适用

1、查询数据库中所有表的表名称

USE UrbanVA
GO
SELECT name FROM SYSOBJECTS WHERE xtype='U'

USE UrbanVA
GO
SELECT name FROM sys.tables

2、查询表中所有列的名称

USE UrbanVA
GO
SELECT name FROM syscolumns WHERE id=object_id('Sdzzd_P') ORDER BY colorder

Oracle适用

1、删除表中的字段

--一次删除一个字段

alter table mms.tcmmspoint drop column latitude;

--一次删除一个字段

alter table mms.tcmmspoint drop (longitude); 

--一次删除多个字段

alter table mms.tcmmspoint drop (longitude, latitude); 

2、向表中添加字段

--添加字段

alter table mms.tcmmspoint add (Longitude Number,Latitude Number);

--添加字段,并指定默认值

alter table mms.tcmmsadvtlabel add(deleteFlag number(38) default 0);

3、关联更新(用b表中的字段更新a表中的字段值)

update mms.tcmmspoint a set a.latitude = (select b.y from mms.mmspoint b WHERE b.pointid = a.pointid);

4、恢复drop操作删除的表

--查询被删除的表

select * from recyclebin;

--恢复tcMMSPointLink 表

flashback table tcMMSPointLink to before drop;

5、将a表的查询结果插入到b表

insert into mms.tcmmspointbek select * from mms.tcmmspoint t where t.routeid = 51 and t.pointid >= 232244 and t.pointid <= 232258;

6、a表和b表都存在某字段,查找存在于a表但不存在于b表的字段值

select distinct(routeid) from mms.tcmmspoint

minus

select distinct(routeid) from mms.tcmmsroute;
select distinct(a.routeid) from mms.tcmmspoint a

where not exists 

(select distinct(b.routeid) from  mms.tcmmsroute b where b.routeid = a.routeid);

7、删除表

drop table mms.tcmmspointlink;

8、修改表名称

alter table mms.tcmmspointlinkex rename to tcmmspointlink;

9、查询最小值、最大值

select min(a.pointid) from mms.tcmmspoint a where a.routeid = 15;

select max(a.pointid) from mms.tcmmspoint a where a.routeid = 15;

10、统计计数

select count(*) from mms.tcmmspoint;

11、根据实景点表查询当前项目的实景是由哪几辆采集车采集的?

select distinct(substr(a.imagename, 0, 6)) from mms.tcmmspoint a;
select * from mms.tcmmsvehicleconfig a where a.vehiclecode in 

(

       select distinct(substr(b.imagename, 0, 6)) from mms.tcmmspoint b

);

12、根据实景点表查询每条Route中的最大PointID

select t.routeid, max(t.pointid) from mms.tcmmspoint t group by t.routeid;

13、清除回收站

purge recyclebin;       --清除当前用户的回收站

purge dba_recyclebin;   --清除所有用户的回收站

14、根据原有表创建新表

create table mms.tcmmspointnew as select * from mms.tcmmspoint where 1=2; --复制表结构

create table mms.tcmmspointnew as select * from mms.tcmmspoint; --复制表结构及其数据

create table mms.tcmmslabelnew as select * from mms.tcmmslabel where mms.tcmmslabel.objectname = '上水井盖';

15、删除用户(system as sysdba)

drop user mms cascade;

15、删除表空间(system as sysdba)

drop tablespace mms including contents and datafiles;

16、创建表空间(system as sysdba)

CREATE TABLESPACE "MMS" 

    LOGGING 

    DATAFILE 'D:\app\Administrator\oradata\ORCL\MMS.DBF' SIZE 200M 

    AUTOEXTEND 

    ON NEXT  102400K MAXSIZE UNLIMITED EXTENT MANAGEMENT LOCAL SEGMENT

    SPACE MANAGEMENT  AUTO ;

17、创建用户并赋权限(system as sysdba)

CREATE USER MMS PROFILE "DEFAULT"

    IDENTIFIED BY "mms" DEFAULT TABLESPACE MMS

    ACCOUNT UNLOCK;

GRANT CONNECT TO MMS;

GRANT DBA TO MMS;

GRANT ALTER ANY TABLE TO MMS;

GRANT CREATE ANY INDEX TO MMS;

GRANT CREATE ANY PROCEDURE  TO MMS;

GRANT CREATE ANY TABLE TO MMS;

GRANT CREATE ANY TRIGGER TO MMS;

GRANT DELETE ANY TABLE TO MMS;

GRANT DROP ANY INDEX TO MMS;

GRANT DROP ANY PROCEDURE TO MMS;

GRANT DROP ANY TABLE TO MMS;

GRANT DROP ANY TRIGGER TO MMS;

GRANT EXECUTE ANY PROCEDURE TO MMS; 

GRANT INSERT ANY TABLE TO MMS;

GRANT SELECT ANY TABLE TO MMS;

GRANT UPDATE ANY TABLE TO MMS;

GRANT ALTER ANY PROCEDURE TO MMS;

GRANT CREATE ANY VIEW TO MMS;

18、

你可能感兴趣的:(sql语句)