I was given two databases, and am supposed to create a table in a new database that stores information about the given databases.
So far I created a table in a new database. I also defined its attributes, but I am stuck on how to populate this table.
The attributes are things like 'original_db, 'original_field' etc , but I don't know how to access this information? Especially since I would need to connect jdbc to 3 databases (the new one, and the 2 old ones) at the same time. Is ths even possible?
I am new to working on databases and SQLite, so sorry if this is a stupid problem.
I would be so grateful for any advice!
What I can grasp from your question is you don't know how to insert data into it?
You could do this in your schema.sql when creating the db e.g the schema would look something like.
DROP TABLE users;
CREATE TABLE users(id integer primary key, username varchar(30), password varchar(30), email varchar(50), receive_email blob, gender varchar(6));
DROP TABLE lists;
CREATE TABLE lists(id integer primary key, user_id integer, list_id integer, name varchar(50), creation_date datetime, importance integer, completed blob);
DROP TABLE list_item;
CREATE TABLE list_item(id integer primary key, list_id integer, name varchar(50), creation_date datetime, completed blob);
then you could make a data.sql file or something with
INESRT into users VALUES(NULL, "name", ...);
and then do in the terminal
sqlite3 database.db <- data.sql
or you could start a sqlite interactive session in the same working directory as your db and manually type in the code
e.g type in
sqlite3 databasename.db
and then type in commands from there
Related
I am creating a program which is a Car Management app in Java using Swing in Netbeans, and as the database I am using Mysql, which will do all the CRUD operation. I have created a database with 3 different tables.
DATABASE SCHEMA
CREATE TABLE car_make(
make_id INT NOT NULL AUTO_INCREMENT
make VARCHAR(50)
PRIMARY KEY(make_id)
)
CREATE TABLE car_model(
model_id INT NOT NULL AUTO_INCREMENT
model VARCHAR(50)
fk_make_id INT
PRIMARY KEY(model_id)
FOREIGN KEY(fk_make_id) REFERENCES car_make(make_id)
)
CREATE TABLE car_attributes(
car_attr_id INT NOT NULL AUTO_INCREMENT
fk_make_id INT
fk_model_id INT
year INT
body_type VARCHAR(50)
mileage INT
price DECIMAL(6,2)
PRIMARY KEY(car_attr_id )
FOREIGN KEY(fk_make_id) REFERENCES car_make(make_id)
FOREIGN KEY(fk_model_id) REFERENCES car_model(model_id )
);
The car_make table is already populated with all the car makes which I inserted manually and their ID's were auto generated. And the car_model table is also populated with different models for each make.
I have also created the user interface in netbeans using Java Swing, where the user selects the make of the car which will automatically generate the corresponding model, the year, mileage and so on and insert all that data into the car_attribute tables. My question is, what is the query to insert the car make and model into the car_attribute table as foreign keys. For example if the user chooses as Make-BMW and Model-5 Series, I want to insert both of those IDS into the third table.
this scenario came up when you want to add new car, so the GUI show you the CAR_MAKEs
when you choose, it will show you CAR_MODELs, then you need to STORE the ID of fetched data (car_makes OR car_models) in arrayList and use it in the query when the USER submit
Just pseudo code
int carMakesSelectedIndex = comboCarMakes.getSelectedIndex();
int carModelsSelectedIndex = comboCarModels.getSelectedIndex();
PreparedStatement stmt=con.prepareStatement("insert into car_attributes values(?,?,?,?,?,?,?,?,?,?)");
stmt.setInt(1,carAttribute);
stmt.setInt(2,carMakesIdList.get( carMakesSelectedIndex ) );
stmt.setInt(3,carMakesIdList.get( carModelsSelectedIndex ) );
stmt.setString(4,carYear );
// the same with other attribute
I am very new to sql, and I am trying to delete a row of person data from a BANK_PERSON table along with its FK constraints. I am using JDBC to declare a callable statement, which takes in a username, then calls a stored procedure in sql and deletes a person from the database.I know the basic process for creating a stored procedure, but I'm not sure how to do it in my case . Here is my BANK_PERSON table and MY BANK_ACCOUNT table with an FK constraint.
CREATE TABLE BANK_PERSON (
ID_NUMBER INTEGER PRIMARY KEY,
FIRSTNAME VARCHAR2(30),
LASTNAME VARCHAR2(30),
USERNAME VARCHAR2(30),
PASS VARCHAR2(30),
RANK INTEGER
);
CREATE TABLE BANK_ACCOUNT (
ACCOUNT_NUMBER INTEGER PRIMARY KEY,
ACCOUNT_BALANCE NUMBER(10,2),
FOREIGN KEY(ACCOUNT_NUMBER) REFERENCES BANK_PERSON(ID_NUMBER));
First of all I suggest to use ON DELETE CASCADE option for your FK which allows you delete all child entries automatically. In other words use following script for creation of BANK_ACCOUNT table:
CREATE TABLE BANK_ACCOUNT (
ACCOUNT_NUMBER INTEGER PRIMARY KEY,
ACCOUNT_BALANCE NUMBER(10,2),
FOREIGN KEY(ACCOUNT_NUMBER) REFERENCES A1(ID_NUMBER) ON DELETE CASCADE);
Then use following script for procedure creation:
CREATE OR REPLACE
PROCEDURE DELETE_PERSON
(USERNAME IN VARCHAR2) AS
BEGIN
delete from BANK_PERSON where BANK_PERSON.USERNAME like DELETE_PERSON.USERNAME;
END DELETE_PERSON;
And finally call of newly created procedure:
EXECUTE DELETE_PERSON('Marcus');
Hope this is what you would like to get.
I have Cassandra Table as below
create table user(
id UUID PRIMARY KEY,
firstname varchar,
secondname varchar,
emailid varchar,
);
From Java - Spring Boot Im trying to access data
Optional<User> findByEmailid(String emailId);
I get error stating
It asks me to use "FILTERING ALLOWED" part of query. Is there anyway to enable this globally or I should change query/db structure?
That error is telling you that emailid is not a valid column that can be filtered on in your WHERE clause. Enabling ALLOW FILTERING or creating a secondary index on that column is one way to do this. But both of those are pretty terrible solutions (because of how Cassandra is works under-the-hood).
With Cassandra you need to take a query-based modeling approach. This means that sometimes (often) queries and tables share a 1:1 ratio. If you really need to query users by email address, then you will need to create a table to serve that query.
CREATE TABLE user_by_email(
id UUID,
firstname varchar,
secondname varchar,
emailid varchar PRIMARY KEY,
);
Then something like this will work:
Optional<UserByEmail> findByEmailid(String emailId);
And if you don't ever plan on querying the user table by id, then there really isn't a reason to use that column as your sole primary key.
Using MySQL, I have the following SQL Table definition:
CREATE TABLE books (
author INT,
book INT,
name VARCHAR(128),
PRIMARY KEY(author, book)
);
What I want is that I have an Id for author that I set manually and an Id for book that is incremented for each author id. Therefore I created a trigger like so:
CREATE TRIGGER trBooks
BEFORE INSERT ON books
FOR EACH ROW SET NEW.book = (
SELECT COALESCE(MAX(book), -1) + 1 FROM books
WHERE author = NEW.author
);
This works fine for me. But now I need to know the book id that was set for my inserted entry that I inserted in Java. Something like the Insert with Output as in MSSQL or a Statement.executeQuery("INSERT ..."). The solution has to be thread safe, so a separate INSERT and SELECT is no good solution, since there might have been another INSERT in the meantime.
Thanks for your help!
Your data model just doesn't make sense. You have two entities, "books" and "authors". These should each be represented as a table. Because a book can have multiple authors and an author can write multiple books, you want a junction table.
This looks like this:
CREATE TABLE Books (
BookId INT auto_increment primary key,
Title VARCHAR(255)
);
CREATE TABLE Authors (
AuthorId INT auto_increment primary key,
Name VARCHAR(255)
);
CREATE TABLE BookAuthors (
BookAuthorId INT auto_increment primary key,
AuthorId INT,
BookId INT,
CONSTRAINT fk_BookAuthor_BookId FOREIGN KEY (BookId) REFERENCES Books(BookId),
CONSTRAINT fk_BookAuthor_AuthorId FOREIGN KEY (BookId) REFERENCES Authors(AuthorId),
UNIQUE (AuthorId, BookId)
);
As for your question about inserts. You don't need a trigger to set auto-incremented ids. You can use LAST_INSERT_ID() to fetch the most recent inserted value.
i have two tables where in the first one i have 14 millions and in the second one i have 1.5 million of data.
So i wonder how could i transfer this data to another table to be normalized ?
And how do i convert some type to another, for example: i have a field called 'year' but its type is varchar, but i want it an integer instead, how do i do that ?
I thought about do this using JDBC in a loop while from java, but i think this is not effeciently.
// 1.5 million of data
CREATE TABLE dbo.directorsmovies
(
movieid INT NULL,
directorid INT NULL,
dname VARCHAR (500) NULL,
addition VARCHAR (1000) NULL
)
//14 million of data
CREATE TABLE dbo.movies
(
movieid VARCHAR (20) NULL,
title VARCHAR (400) NULL,
mvyear VARCHAR (100) NULL,
actorid VARCHAR (20) NULL,
actorname VARCHAR (250) NULL,
sex CHAR (1) NULL,
as_character VARCHAR (1500) NULL,
languages VARCHAR (1500) NULL,
genres VARCHAR (100) NULL
)
And this is my new tables:
DROP TABLE actor
CREATE TABLE actor (
id INT PRIMARY KEY IDENTITY,
name VARCHAR(200) NOT NULL,
sex VARCHAR(1) NOT NULL
)
DROP TABLE actor_character
CREATE TABLE actor_character(
id INT PRIMARY KEY IDENTITY,
character VARCHAR(100)
)
DROP TABLE director
CREATE TABLE director(
id INT PRIMARY KEY IDENTITY,
name VARCHAR(200) NOT NULL,
addition VARCHAR(150)
)
DROP TABLE movie
CREATE TABLE movie(
id INT PRIMARY KEY IDENTITY,
title VARCHAR(200) NOT NULL,
year INT
)
DROP TABLE language
CREATE TABLE language(
id INT PRIMARY KEY IDENTITY,
language VARCHAR (100) NOT NULL
)
DROP TABLE genre
CREATE TABLE genre(
id INT PRIMARY KEY IDENTITY,
genre VARCHAR(100) NOT NULL
)
DROP TABLE director_movie
CREATE TABLE director_movie(
idDirector INT,
idMovie INT,
CONSTRAINT fk_director_movie_1 FOREIGN KEY (idDirector) REFERENCES director(id),
CONSTRAINT fk_director_movie_2 FOREIGN KEY (idMovie) REFERENCES movie(id),
CONSTRAINT pk_director_movie PRIMARY KEY(idDirector,idMovie)
)
DROP TABLE genre_movie
CREATE TABLE genre_movie(
idGenre INT,
idMovie INT,
CONSTRAINT fk_genre_movie_1 FOREIGN KEY (idMovie) REFERENCES movie(id),
CONSTRAINT fk_genre_movie_2 FOREIGN KEY (idGenre) REFERENCES genre(id),
CONSTRAINT pk_genre_movie PRIMARY KEY (idMovie, idGenre)
)
DROP TABLE language_movie
CREATE TABLE language_movie(
idLanguage INT,
idMovie INT,
CONSTRAINT fk_language_movie_1 FOREIGN KEY (idLanguage) REFERENCES language(id),
CONSTRAINT fk_language_movie_2 FOREIGN KEY (idMovie) REFERENCES movie(id),
CONSTRAINT pk_language_movie PRIMARY KEY (idLanguage, idMovie)
)
DROP TABLE movie_actor
CREATE TABLE movie_actor(
idMovie INT,
idActor INT,
CONSTRAINT fk_movie_actor_1 FOREIGN KEY (idMovie) REFERENCES movie(id),
CONSTRAINT fk_movie_actor_2 FOREIGN KEY (idActor) REFERENCES actor(id),
CONSTRAINT pk_movie_actor PRIMARY KEY (idMovie,idActor)
)
UPDATE:
I'm using SQL Server 2008.
Sorry guys i forgot to mention that are different databases :
The not normalized is call disciplinedb and the my normalized call imdb.
Best regards,
Valter Henrique.
If both tables are in the same database, then the most efficient transfer is to do it all within the database, preferably by sending a SQL statement to be executed there.
Any movement of data from the d/b server to somewhere else and then back to the d/b server is to be avoided unless there is a reason it can only be transformed off-server. If the destination is different server, then this is much less of an issue.
Though my tables were dwarfs compared to yours, I got over this kind of problem once with stored procedures. For MySQL, below is a simplified (and untested) essence of my script, but something similar should work with all major SQL bases.
First you should just add a new integer year column (int_year in example) and then iterate over all rows using the procedure below:
DROP PROCEDURE IF EXISTS move_data;
CREATE PROCEDURE move_data()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE orig_id INT DEFAULT 0;
DECLARE orig_year VARCHAR DEFAULT "";
DECLARE cur1 CURSOR FOR SELECT id, year FROM table1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
PREPARE stmt FROM "UPDATE table1 SET int_year = ? WHERE id = ?";
read_loop: LOOP
FETCH cur1 INTO orig_id, orig_year;
IF done THEN
LEAVE read_loop;
END IF;
SET #year= orig_year;
SET #id = orig_id;
EXECUTE stmt USING #orig_year, #id;
END LOOP;
CLOSE cur1;
END;
And to start the procedure, just CALL move_data().
The above SQL has two major ideas to speed it up:
Use CURSORS to iterate over a large table
Use PREPARED statement to quickly execute pre-known commands
PS. for my case this speeded things up from ages to seconds, though in your case it can still take a considerable amount of time. So it would be probably best to execute from command line, not some web interface (e.g. PhpMyAdmin).
I just recently did this for ~150 Gb of data. I used a pair of merge statements for each table. The first merge statement said "if it's not in the destination table, copy it there" and the second said "if it's in the destination table, delete it from the source". I put both in a while loop and only did 10000 rows in each operation at a time. Keeping it on the server (and not transferring it through a client) is going to be a huge boon for performance. Give it a shot!