在Oracle数据库中将1:M关系更改为M:M

时间:2016-10-18 14:43:18

标签: oracle

我有一个问题,关于如何在保留现有数据的同时更改Oracle数据库中的故事之间的关系。

假设我想代表人和雇主,以便每个人都为一个雇主工作。我使用PERSON表和EMPLOYER表执行此操作,该表与EMPLOYER与PERSON的1:M关系。 PERSON表具有列ID,NAME和EMPLOYER_ID,EMPLOYER表具有列ID,NAME和AND LOCATION。

如果我想更新此架构,以便PERSON可以为多个EMPLOYER工作,我可以添加一个PERSON_EMPLOYER表,其中包含每个ID的列。

有没有人可以指出最合理的方法来移动我现有的数据呢?我想我可以添加一个联接表,但我不确定如何用现有雇主填充它:雇主数据。之后我猜我从PERSON中删除了EMPLOYER_ID列。

我应该只是备份数据库并在脚本中执行此操作吗?

非常感谢你。

1 个答案:

答案 0 :(得分:0)

备份总是一个好主意。 但在我看来,将数据从一个表传输到另一个表是非常可靠的操作。您没有必要制作脚本,只需一步一步地检查更改。

  1. 创建新的PERSON_EMPLOYER表
  2. 将现有数据复制到PERSON_EMPLOYER表。
  3. COMMIT数据更改。
  4. 检查PERSON_EMPLOYER表中的数据
  5. 从PERSON表中删除EMPLOYER_ID列。 (无需立即删除该列,以后可以确保您的数据一切正常。)
  6. 要将数据从PERSON表传输到PERSON_EMPLOYER表,您可以使用简单的INSERT

     public class MapActivity extends FragmentActivity implements      OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener,GoogleMap.OnInfoWindowClickListener{
    
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
       // int x =_st.jsonArray.length();
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            checkLocationPermission();
        }
    
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    
        fragmentOnMap = (FragmentOnMap)getFragmentManager().findFragmentById(R.id.fragment_bottom);
        getFragmentManager().beginTransaction().hide(fragmentOnMap);
    
    }
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
    
    
        //Get Last Known Location and convert into Current Longitute and Latitude
        final LocationManager mlocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        final Location currentGeoLocation = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    
        double currentLat = currentGeoLocation.getLatitude();
       double currentLon = currentGeoLocation.getLongitude();
    
        LatLng currentLatLng = new LatLng(currentLat,currentLon);
        //Toast.makeText(this,"Cur Lat" +currentLat+"Lon"+currentLon,Toast.LENGTH_LONG).show();
    
        // Get JSON String and Break it down into individual nodes
        //double x=_location.getLatitude();
        //double y=_location.getLongitude();
        json_string = getIntent().getExtras().getString("JSON_DTA");
        LatLng latLng = null;
        try {
            int count = 0;
            //jsonObject = new JSONObject(json_string);
            jsonArray = new JSONArray(json_string);
    
            while (count < jsonArray.length()) {
                JSONObject JO = jsonArray.getJSONObject(count);
                _id = JO.getInt("id");
                _doctorname = JO.getString("doctorname");
                _doclat = JO.getString("latitude");
                _doclong = JO.getString("longitude");
    
                count++;
    
                Double d = Double.parseDouble(_doclat);
                Double e = Double.parseDouble(_doclong);
                latLng = new LatLng(d, e);
    
                DecimalFormat df = new DecimalFormat("####0.0");
    
                DistanceBetweenPoints distanceBetweenPoints = new DistanceBetweenPoints();
                double  distance = distanceBetweenPoints.CalculationByDistance(currentLatLng,latLng);
    
                double distanceThreshHold = 7 ;
                if(distance < distanceThreshHold  )
                {
                    mMap = googleMap;
                    mMap.setOnInfoWindowClickListener(this);
                    MarkerOptions _markeroptions = new MarkerOptions()
                            .position(latLng)
                            .title(_doctorname+":  " + df.format(distance) + " KM" )
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.smallest_logo));
                    mMap.addMarker(_markeroptions);
    
                    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    
    
                }
                else
                {
                    mMap = googleMap;
                }
    
         }
    
    
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
        //Initialize Google Play Services
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                buildGoogleApiClient();
    
                mMap.setMyLocationEnabled(true);
    
            }
        } else {
            buildGoogleApiClient();
            mMap.setMyLocationEnabled(true);
        }
    }
    
    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
    }
    @Override
    public void onConnected(@Nullable Bundle bundle) {
    
        mLocationRequest = new LocationRequest();
    
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }
    
    }
    
    @Override
    public void onConnectionSuspended(int i) {
    
    }
    
    
    @Override
    public void onLocationChanged(Location location) {
    
            mLastLocation = location;
            if (mCurrLocationMarker != null) {
                mCurrLocationMarker.remove();
            }
    
    
            //Place current location marker
            LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title("Current Position");
            markerOptions.draggable(true);
    
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            mCurrLocationMarker = mMap.addMarker(markerOptions);
    
    
          //  Toast.makeText(this, "Current" + latLng, Toast.LENGTH_LONG).show();
            String tag = null;
            Log.d(tag, "lon: " + location.getLongitude() + " ----- lat: " + location.getLatitude());
    
    
            //move map camera
            mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
    
    
            //stop location updates
            if (mGoogleApiClient != null) {
                LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
            }
    
    
        }
    
    
    
    
    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    
    }
    
    public boolean checkLocationPermission(){
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
    
            // Asking user if explanation is needed
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)) {
    
                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
    
                //Prompt the user once explanation has been shown
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST_LOCATION);
    
    
            } else {
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST_LOCATION);
            }
            return false;
        } else {
            return true;
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    // Permission was granted.
                    if (ContextCompat.checkSelfPermission(this,
                            Manifest.permission.ACCESS_FINE_LOCATION)
                            == PackageManager.PERMISSION_GRANTED) {
    
                        if (mGoogleApiClient == null) {
                            buildGoogleApiClient();
                        }
                        mMap.setMyLocationEnabled(true);
                    }
    
                } else {
    
                    // Permission denied, Disable the functionality that depends on this permission.
                    Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
                }
                return;
            }
    
            // other 'case' lines to check for other permissions this app might  request.
            //You can add here other case statements according to your requirement.
        }
    }
    

    不要忘记COMMIT这个操作!