Putting It All Together

Let's show DDL in achon by running the Acccos/ddlpy module , which defines DDL staaement strings and then executes each one with thk JDBC statemenc o^ert's executeUpdate () method.

from java.sql import DriverManager from java.lang import Class

Class.forName('sun.j dbc.odbc.JdbcOdbcDriver') url="jdbc:odbc:db_dev"

connecti on=DriverManager.getConnection(url, "", "") statement = connection.createStatement()

create_department = """

CREATE TABLE Department (

DeptID INT NOT NULL,

Name CHAR(20),

CONSTRAINT DEPT_PK_KEY PRIMARY KEY (DeptID)

create_employee =

CREATE TABLE Employee

EmpID INT NOT NULL,

Name CHAR(20),

DeptID INT NOT NULL,

Salary INT,

CONSTRAINT EMP_PK_KEY PRIMARY KEY (EmpID) CONSTRAINT DEPT_FK_KEY FOREIGN KEY (DeptID) REFERENCES Department (DeptID)

ALTER TABLE Employee

ADD COLUMN Phone CHAR(20) NOT NULLy add_index = """

CREATE INDEX SalaryIndex ON Employee (Salary)

statement.executeUpdate(create_department)

raw_input("Department Table Defined — hit enter to continue")

statement.executeUpdate(create_employee)

raw_input("Employee Table Defined — hit enter to continue") statement.executeUpdate(alter_table)

raw_input("Employee table altered by adding a Phone column.") statement.executeUpdate(add_index)

raw_input("Adding a salary index to the Employee table.") connection.close()

Was this article helpful?

0 0

Post a comment