WEB工程数据库相关安装脚本写作

1. 数据库oracle安装

2. 数据库用户创建,表空间创建,表创建

#!/bin/bash

current_path=`pwd`
create_tablespace=${current_path}/create_tablespace.sql
create_tables=${current_path}/create_tables.sql
install_log=${current_path}/installlog.log
sqllog=${current_path}/sql.log


function create_table_space
{
    if [ ! -f ${create_tablespace} ]; then
        echo "${create_tablespace} is not exist "
        return 1
    fi
    logger "info" "executing ${create_tablespace}..."

    sqlplus / as sysdba >${sqllog}<<EOF
@${create_tablespace}
EOF
    if [ $? != 0 ]; then
        logger "error" "create table space failed"
        return 1
    fi

    error_info=`grep "ERROR" ${sqllog}`
    if [ "${error_info}X" != "X" ]; then
        logger "error" "create table space failed"    
        cat ${sqllog}
        return 1
    fi
    logger "info" "create table space success"
    return 0
}


function create_tables
{
    if [ ! -f ${create_tables} ]; then
        logger "error" "${create_tables} is not exist.."
        return 1    
    fi
    logger "info" "executing create tables..."    
    sqlplus rocky/rocky12345@ora11g  >>${sqllog}<<EOF
@${create_tables}
EOF
    if [ $? != 0 ]; then
        logger "error" "create tables failed..."
        return 1
    fi

    error_info=`grep ERROR ${sqllog}`
    if [ "${error_info}X" != "X" ]; then
        logger "error" "create tables failed..."
        return 1
    fi
    logger "info" "create tables success..."
    return 0
}


function main
{
    echo "start install rocky" >${install_log}
    echo `date`>>${install_log}
    echo "start install rocky"
    echo "[step 1] create table space"
    create_table_space    
    if [ $? != 0 ]; then    
        return 1
    fi
    
    echo "[step 2] create tables"
    create_tables
    
    if [ $? != 0 ]; then    
        return 1
    fi
    
}

function logger
{
    if [ $# == 2 ]; then
        echo "[$1]:[$2]"
        echo "[$1]:[$2]" >>${install_log}
        return
    fi
}


main $*

主安装脚本

create or replace  procedure clean_user
as
v_count integer;
begin
          select count(*) into v_count from user_tablespaces where upper(tablespace_name)=upper('rocky_default');
          If v_count > 0 Then
          Execute immediate 'drop tablespace rocky_default including contents and datafiles cascade constraints';
          end if;
          
          Execute immediate 'create tablespace rocky_default datafile ''/home/oracle/test/rocky.dbf'' size 100M autoextend on next 10M maxsize 500M';
          Execute immediate 'create user rocky identified by rocky12345 default tablespace rocky_default';
          Execute immediate 'GRANT CONNECT,RESOURCE,CREATE ANY TABLE,DROP ANY TABLE,CREATE SEQUENCE,DROP ANY SEQUENCE, CREATE DATABASE LINK, CREATE VIEW to rocky';
    
          
end;
/
execute  clean_user;
/
exit;

create_tablespace.sql

create or replace  procedure clean_tables
as
v_count integer;
begin
          select count(*) into v_count from user_tables where upper(table_name)=upper('T_MEMBER_INFO');
          If v_count > 0 Then
          Execute immediate 'drop table T_MEMBER_INFO';
          end if; 
end;
/
execute  clean_tables;
/

create table T_MEMBER_INFO
(
    username    varchar(200) not null,
    password    varchar(100) not null,
    groupid        number(9)    not null
);
alter table T_MEMBER_INFO add constraint PK_MEMBER_USERNAME primary key (username);
exit;
/

create_tables.sql

原文地址:https://www.cnblogs.com/unixshell/p/3361286.html