• 常用SQL语句集锦


    MySQL适用

    MS SQLServer适用

    Oracle适用

    MySQL适用

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

    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));

    2、解决MySQL远程连接1130错误问题

    GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; -- 授权任何主机连接MySQL服务器
    
    GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; --授权IP为192.168.1.3的主机连接MYSQL服务器

    3、根据原表创建新表 

    CREATE TABLE t_g_configitem_py LIKE t_g_configitem;   
    INSERT INTO t_g_configitem_py SELECT * FROM t_g_configitem; 

    或者

    CREATE TABLE t_g_configitem_bj AS ( SELECT * FROM t_g_configitem );

    4、删除表里所有数据

    DELETE FROM scwater.t_b_pipeleakage;

    或者

    TRUNCATE scwater.t_b_pipeleakage;--删除数据并使得自增ID恢复从1开始

    5、关联更新 

    UPDATE scwater.t_b_pipeleakage2 AS a SET a.RouteID = (SELECT b.RouteID FROM scwater.td_b_route AS b WHERE a.DefectCode = b.RouteName);

    6、字符串连接并更新

    UPDATE scwater.t_b_pipeleakage2 AS a SET a.DefectCode = CONCAT(a.DefectType, a.DefectLevel);

    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:appAdministratororadataORCLMMS.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、按时序间隔(10分钟)生成模拟数据

    INSERT INTO rwdb.st_waterlogging_r(stcd,tm,z)
      SELECT 'Z38',to_date(to_char(to_date('20190218', 'yyyymmdd') + rownum / 24 / 6, 'yyyy-mm-dd HH24:MI:SS'),'yyyy-mm-dd hh24:mi:ss'), dbms_random.value(0.0,200.0)
        FROM dual
      CONNECT BY rownum <= 24 * 60 * 60 - 1;

    19、解锁scott用户,并设置密码

    alter user scott account unlock;
    alter user scott identified by scott;

     20、在原有时间增加一小时、一分钟、一秒钟

    update scdata.tl_um_patrolpos t set t.updatetime = t.updatetime - 1/(24);
    update scdata.tl_um_patrolpos t set t.updatetime = t.updatetime - 1/(24*60);
    update scdata.tl_um_patrolpos t set t.updatetime = t.updatetime - 1/(24*60*60);

    ~

  • 相关阅读:
    程序由多个文件组成时、 头文件
    c++ 数组,变量,指针,引用, 初始化,赋值
    linux 远程装机
    linux中firewall与iptables防火墙服务
    linu samba服务
    linux 中iscsi服务
    Linux中apache服务
    linux dns高速缓存
    linux 网络配置
    linux 数据库管理
  • 原文地址:https://www.cnblogs.com/hans_gis/p/2274488.html
Copyright © 2020-2023  润新知