How to Insert data in MySQL
-- Assuming the users table has only three columns: first_name, last_name, and email, and in that order
INSERT INTO users VALUES ('John', 'Doe', 'john@doe.com');
INSERT INTO users (first_name, last_name, email, birth_date, city, state)
VALUES ('John', 'Doe', 'john@doe.com','2000-01-01','Los Angeles','CA');
Inserting Multiple Rows
You can insert multiple rows in one INSERT
statement by having multiple sets of values enclosed in parentheses:
INSERT INTO users (first_name, last_name)
VALUES
('John','Lennon'),
('Paul','McCartney'),
('George','Harrison'),
('Ringo','Starr');
If inserting a row would violate a unique constraint, there are different ways to handle it, depending on your requirements.
Using INSERT IGNORE
will quietly discard the row to be inserted:
INSERT IGNORE INTO products (product_id, product_name, stocks)
VALUES (1, 'VPN Product 1', 50);