Single-row inserts

To insert a single row into a table in MySQL, use the following syntax: 

INSERT INTO lahmansbaseballdb.collegeplaying
(`playerID`,
`schoolID`,
`yearID`)
VALUES
(<{playerID: }>,
<{schoolID: }>,
<{yearID: }>);

You can get this syntax by taking the following steps:

  1. Right-click on Tables in MySQL Workbench.
  2. Select Send to SQL Editor.
  3. Choose Insert Statement.

This places the INSERT syntax for that table into a SQL query window: 

The INSERT statement allows you to add values to the collegeplaying table. To make the query insert proper values into a row in the collegeplaying table, you need to add values to the VALUES statement, as in the following query:

USE lahmansbaseballdb;
INSERT INTO collegeplaying
(playerID,
schoolID,
yearID)
VALUES
('blaloha01',
'sandiegost',
1999);

You will see the following results from the previous query. It shows that one row is inserted in the collegeplaying table. It is seen that the player blaloha01 attended sandiegost State University in the year 1999:

To make the query more compact, you can place the columns and values on the same line, as in the following query:

USE lahmansbaseballdb;
INSERT INTO collegeplaying
(playerID, schoolID, yearID)
VALUES ('blaloha01','sandiegost',1999);

The previous query runs in the same way as the INSERT query before it. The previous query is just more compact and takes up less screen space. This is particularly helpful if you have a lot of queries in one SQL file.

You can also insert values into a table without specifying the columns if you make sure to place your values in the correct order. The following query inserts the same values into the table as the previous INSERT statements:

USE lahmansbaseballdb;
INSERT INTO collegeplaying
VALUES ('blaloha01','sandiegost',1999);

If you want to insert values into some fields in the table but not others, you need to specify the columns you want to add them to to ensure the values go into the correct columns.

For instance, if you want to place a playerID value into the collegeplaying table without a value for schoolID, you can execute the following query:

USE lahmansbaseballdb;
INSERT INTO collegeplaying
(playerID, yearID)
VALUES ('blaloha01', 1999);

The previous query inserts a row into the collegeplaying table and the schoolID value is NULL, in this case, since we didn't insert a value into it, which gives the results shown in the following screenshot:

It doesn't make a lot of sense to have a row in the collegeplaying table without a schoolID value specified, but this just gives you an example of how it would work.

If you execute the following query, you get an error:

USE lahmansbaseballdb;
INSERT INTO collegeplaying
VALUES ('blaloha01', 1999);

The error you receive is Error Code: 1136. Column count doesn't match value count at row 1. MySQL fails to insert rows if your value count doesn't match the column count in the table. Since you know that the collegeplaying table has three columns, the INSERT query fails since you've only specified two values. You need to specify the columns you want those values inserted into.

Let's say you put the values in the wrong order for the columns in the table, as in the following query:

USE lahmansbaseballdb;
INSERT INTO collegeplaying
VALUES('blaloha01',1999, 'sandiegost');

The previous query gives you an error, Error Code: 1366. Incorrect integer value: 'sandiegost' for column 'yearID' at row 1. This error occurs since MySQL can't implicitly convert the 'sandiegost' value into a SMALLINT value, which is what the yearID column data type is. Since you placed 'sandiegost' in the yearID column on the INSERT line, MySQL assumed you wanted to insert 'sandiegost' into yearID.

Alternatively, if you execute the following query, you won't get an error: 

USE lahmansbaseballdb;
INSERT INTO collegeplaying
(playerID,schoolID)
VALUES ('blaloha01', 1999);

Even though 1999 isn't a schoolID value, MySQL doesn't know this and implicitly converts 1999 into a VARCHAR value and inserts it into the collegeplaying table without showing an error. You will also notice that the yearID column is NULL since you didn't insert anything into that column. The following screenshot shows you the results of the previous code: 

If the table you are trying to insert data into defines a default constraint, then you can leave that column out of the listing of columns and not specify a value when inserting.

If you try to insert data into a table with a CHECK constraint and the data doesn't conform to the CHECK constraint, you will get an error. For example, if you have a CHECK constraint on yearID in the collegeplaying table ((yearID >= 1871) and (yearID <= 2155)) and you try to insert 1870 for yearID, the insert action will fail because of the CHECK constraint. 

If you try to insert a NULL value into a column with a NOT NULL constraint, then it will fail because you aren't allowed to add NULL values to that column. 

As for DEFAULT constraints, you can place a value into a column with an INSERT statement, but if you don't specify a value, the default value will automatically be placed into the column.

If you are inserting a value into a column that is in a primary or foreign key relationship, you must ensure that the value doesn't violate those relationships; otherwise, the INSERT statement will fail.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.196.175