Entering many Values into a sql database at one - sql

Question:
I have a table with 100 rows called user
I am after creating a new table called village and I would like to have it automatically fill in 100 rows in the new table

You can use a select to full records of your new table like this
insert into village (user_id, owner)
select id, username
from `user`

Related

Insert into new table with userId column for all users from existing users table

I have user_settings table with two columns, userId and value which is currently empty but have another users table and now I need to add migration with query which should popuplate user_settings table for all users with some default value for value column.
I began with INSERT INTO "user_settings" ("userId", "value") VALUES ($1, $2) and got stuck. How to loop all id's from users table and fill this user_settings table with some default value...
There is really no need to 'loop' through the rows in users, as you can easily achieve this using an INSERT INTO SELECT statement (https://www.w3schools.com/sql/sql_insert_into_select.asp) like this:
SET #default = 'your_settings';
INSERT INTO `user_settings` (
`userId`,
`value`
)
SELECT
id,
#default
FROM
users
Suppose you are using a DBMS that supports triggers, you could also add a trigger to insert default settings upon inserting new records in users.
In MySQL it might look something like this:
DELIMITER $$
CREATE TRIGGER after_user_insert
AFTER INSERT
ON
users
FOR EACH ROW
BEGIN
INSERT INTO user_settings (
userId,
value
)
VALUES (
NEW.id,
'your_settings'
);
END$$
DELIMITER ;
(Assuming the primary key on users table is called id.)
Assuming some syntax elements here because you didn't say which database system you are using.
INSERT INTO "user_settings" ("userId", "value")
SELECT DISTINCT "userId", 'defaultvalue'
FROM "users"
To learn more, read training materials such as those found at:
https://www.w3schools.com/sql/default.asp

How to copy some records of table and change some columns before insert into this table again in sql server?

In my SQL Server table, I have a table whose PK is GUID with lots of records already.
Now I want to add records which only needs to change the COMMON_ID and COMMON_ASSET_TYPE column of some existing records.
select * from My_Table where COMMON_ASSET_TYPE = "ASSET"
I am writing sql to copy above query result, changing COMMON_ID value to new GUID value and COMMON_ASSET_TYPE value from "ASSET" to "USER", then insert the new result into My_Table.
I do not know how to write it since now I feel it is a trouble to insert records manually.
Update:
I have far more columns in table and most of them are not nullable, I want to keep all these columns' data for new records except above two columns.Is there any way if I do not have to write all these column names in sql?
Try to use NEWID if you want to create new guid:
INSERT INTO dbo.YourTable
(
COMMON_ID,
COMMON_ASSET_TYPE
)
select NEWID(), 'User' as Common_Asset_Type
from My_Table
where COMMON_ASSET_TYPE = "ASSET"
UPDATE:
As a good practice I would suggest to write all column names explicitly to have a clean and clear insert statement. However, you can use the following construction, but it is not advisable in my opinion:
insert into table_One
select
id
, isnull(name,'Jon')
from table_Two
INSERT INTO My_Table (COMMON_ID,COMMON_LIMIT_IDENTITY, COMMON_CLASS_ID,COMMON_ASSET_TYPE)
SELECT NEWID(), COMMON_LIMIT_IDENTITY, COMMON_CLASS_ID,'USER'
FROM My_Table
WHERE COMMON_ASSET_TYPE = 'ASSET'
If I've understood correctly you want to take existing records in your table, modify them, and insert them as new records in the same table.
I'll assume ID column contains the the GUID?
I'd first create a temporary table
CREATE TABLE #myTempTable(
ID UNIQUEIDENTIFIER,
Name varchar(max),
... etc
);
Fill this temp table with the records to change with your SELECT statement.
Change the records in the temp table using UPDATE statement.
Finally, Insert those "new" records back into the primary table. with INSERT INTO SELECT statement.
You will probably have to sandwitch the INSERT INTO SELECT with IDENTITY_INSERT (on/off):
SET IDENTITY_INSERT schema_name.table_name ON
SET IDENTITY_INSERT schema_name.table_name OFF
IDENTITY_INSERT "Allows explicit values to be inserted into the identity column of a table."

How to insert new row with id from other table in loop with Postgressql

I would like to insert new row in profit table for each user that exist in db: select id * from users. The number of users is dynamic, so I need to fetch them all. I need some loop in postgres sql. I have problem to figure it out on my own. It would be something like that:
select id * from users as user_ids
for (each userId in user_ids) {
insert into profit (user_id, value) values (userId, 23);
}
Can I ask You for help? I've went for many questions already:
Insert new row with data computed from other rows
postgresSQL insert multiple rows, of id returned from select queries
No luck so far
Thinking in "loops" is almost always wrong when working with SQL. You need to think in terms of sets and how you operate on them. SQL statement describe exactly that: how to retrieve a set and what to do with that set of rows.
In this case, you can use a SELECT statement as the source for an INSERT:
insert into profit (user_id, value)
select id, 23
from users;
Note that you don't have a values clause in this case.

How to fire two triggers for two tables at different time to insert some values from both the tables to third table?

I have 3 tables in SQL server with following fields.
Table 1- id, name, age.
Table 2- id,email, Address.
Table 3- id, name, email.
I wish to use two triggers like, when I insert values on Table 1, id and name should insert in Table 3. When I insert values in Table 2, Email should insert in Table 3 and it should insert at id and name position means it should not show NULL values. Name,id and email should insert in one row.
You should make the View for inserting data from two tables to a single table
You should fix the data model so that it is normalized. You wouldn't need a trigger to replicate data into Table 3 if the name and email columns weren't in 2 tables.

Automatically create record when another is created

I have two tables in my database: Users, Roles and Membership. The Membership table assigns users to specific Roles.
How could I automatically create the Membership record for anytime a new record is inserted in Users.
Example: When a user is created and assigned an ID number (# 562), The database would automatically add them to the Membership table with a specific role ID.
How could I do this?
Write an AFTER INSERT TRIGGER on Users TABLE, that will INSERT the new Row in the Membership table.
http://msdn.microsoft.com/en-us/magazine/cc164047.aspx
Assuming you have a Default RoleID for your new Membership row, when a new User is inserted in Users table, something like this should work.
CREATE TRIGGER TRI_USERS_INSERT on Users
AFTER INSERT
AS
SET NOCOUNT ON
-- If you have a Default RoleID, select that into a variable and use it in the INSERT below.
-- For this example, I am using just the number 1
-- Also assumes that the ID for Memberships table is AUTO GENERATED, so it's not in INSERT list.
INSERT INTO Memberships (UserID, RoleID)
SELECT ID, 1 FROM INSERTED
GO