Download presentation
Presentation is loading. Please wait.
Published byAdelia Bradley Modified over 9 years ago
1
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data
2
Open Source Server Side Scripting 2 ECA 236 sitename table users user_idMEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY first_nameVARCHAR(15) NOT NULL last_nameVARCHAR(30) NOT NULL emailVARCHAR(40) passwordCHAR(16) NOT NULL registration_dateDATETIME NOT NULL
3
Open Source Server Side Scripting 3 ECA 236 INSERT 1 st way to INSERT data: specify the columns to be used syntax: using this way, you can add rows of records, but populate only the fields you want unspecified columns will be treated as NULL or given a default value INSERT INTO tablename (column1, column2, column5 ) VALUES( ‘value1’, ‘value2’, ‘value5’ );
4
Open Source Server Side Scripting 4 ECA 236 INSERT cont … 2 nd way to INSERT data: do not specify columns syntax: using this way, you include a value for every column, even if NULL failure to match number of values to number of columns will generate an error INSERT INTO tablename VALUES ( ‘value1’, NULL, ‘value3’ );
5
Open Source Server Side Scripting 5 ECA 236 INSERT cont … 2 nd way to INSERT data: to insert multiple rows, separate each record with a comma syntax: INSERT INTO tablename VALUES ( ‘value1’, NULL, ‘value3’ ), ( ‘value4’, NULL, ‘value6’ ), ( ‘value7’, NULL, ‘value9’ ) ;
6
Open Source Server Side Scripting 6 ECA 236 NOW( ) NOW( ) is a MySQL function use NOW( ) with date data types NOW( ) inserts the current date and time into a column do not put a space between the function name and the parentheses
7
Open Source Server Side Scripting 7 ECA 236 inserting data into users to insert a new row into the users table: use the NOW( ) function to insert the current date information notice that NOW( ) is not enclosed in quotes INSERT INTO users (first_name, last_name, email, password, registration_date) VALUES ('John', 'Lennon', 'john@beatles.com', PASSWORD('Happin3ss'), NOW( ));
8
Open Source Server Side Scripting 8 ECA 236 inserting data into users cont … we left out the user_id column user_id will be set to NULL because user_id is AUTO_INCREMENT, MySQL will set the value to the next logical number INSERT INTO users (first_name, last_name, email, password, registration_date) VALUES ('John', 'Lennon', 'john@beatles.com', PASSWORD('Happin3ss'), NOW( ));
9
Open Source Server Side Scripting 9 ECA 236 inserting data into users to INSERT several more records: INSERT INTO users (first_name, last_name, email, password, registration_date) VALUES ('Paul', 'McCartney', 'paul@beatles.com', PASSWORD('letITbe'), NOW( )), ('George', 'Harrison', 'george@beatles.com ', PASSWORD('something'), NOW( )), ('Ringo', 'Starr', 'ringo@beatles.com', PASSWORD('thisboy'), NOW( ));
10
Open Source Server Side Scripting 10 ECA 236 inserting data into users when using INSERT enclose strings with single quotes do not quote numbers or function calls to INSERT a value that contains a single quote, escape the quote with a backslash
11
Open Source Server Side Scripting 11 ECA 236 loading text files into tables in some situations you may have to change the configuration of the my.ini file modify my.ini file, usually located in Windows or WINNT directory add the following code to the [mysqld] section [mysqld] set-variable=local-infile=0
12
Open Source Server Side Scripting 12 ECA 236 loading text files into tables cont … text files should contain: one row for each record to be loaded each column separated with a delimiter you can specify delimiter and end of line marker by default, MySQL uses a tab and linefeed NULL values may be represented with \N SpikeIvypoison@ivy.comscraTchY tabs first_name last_name email password
13
Open Source Server Side Scripting 13 ECA 236 loading text files into tables cont … LOAD DATA INFILE loads a text file into a table for more information visit the MySQL Manual: 3.3.3 Loading Data into a Table 4.2.4 Security issues with LOAD DATA LOCAL 6.4.8 LOAD DATA INFILE Syntax LOAD DATA INFILE “path_to/file_name.txt” INTO TABLE table_name;
14
Open Source Server Side Scripting 14 ECA 236 loading text files into tables cont … EXERCISE download users_data.txt from web site create a folder one level down from C: named data save users_data.txt in mysql monitor: view table after data has loaded LOAD DATA INFILE “c:/data/users_data.txt” INTO TABLE users; SELECT * FROM users;
15
Open Source Server Side Scripting 15 ECA 236 SELECT … INTO OUTFILE complements LOAD DATA INFILE write data from a database to a file defaults: >writes tabs between fields >writes newlines at end of lines when working on Windows, escape backslash SELECT * INTO OUTFILE ‘c:\\data\\into_outfile.txt’ FROM users;
16
Open Source Server Side Scripting 16 ECA 236 UPDATE UPDATE allows you to modify existing records syntax: UPDATE multiple columns by separating with comma use a WHERE clause to identify rows to UPDATE UPDATE table_name SET column = ‘value’; UPDATE table_name SET column1 = ‘value1’, column2 = ‘value2’; UPDATE table_name SET column3 = ‘value’ WHERE column1 = ‘value1’;
17
Open Source Server Side Scripting 17 ECA 236 UPDATE cont … the users table contains unencrypted passwords use PASSWORD( ) function to encrypt current unencrypted values in password column notice there are not quotes around password being passed to PASSWORD( ) UPDATE users SET password = PASSWORD(password) WHERE user_id >= 5;
18
Open Source Server Side Scripting 18 ECA 236 UPDATE cont … registration_date has been set to zero for all new entries use the WHERE clause to affect only those rows with a registration_date set to zero UPDATE users SET registration_date = NOW( ) WHERE registration_date = 0;
19
Open Source Server Side Scripting 19 ECA 236 LOCAL LOAD DATA LOCAL INFILE if the LOCAL keyword is included the file is read by the client program on the client machine, and sent to the server if the LOCAL keyword is not included the file must be located on the server files to be loaded must be readable by all
20
Open Source Server Side Scripting 20 ECA 236 backing up a database mysqldump will back up tables and their structure run directly from command line dump file to the screen: mysqldump -u root -p sitename
21
Open Source Server Side Scripting 21 ECA 236 backing up a database cont … mysqldump create an output file contains SQL commands to create the table contains data to populate the table mysqldump -u root -p sitename > c:/data/mydump.sql
22
Open Source Server Side Scripting 22 ECA 236 backing up a database cont … mysqldump read the file back into MySQL using the syntax: review MySQL Manual for a long list of options to use with mysqldump mysql -u root -p sitename < c:/data/mydump.sql
23
Open Source Server Side Scripting 23 ECA 236 source to run MySQL statements stored in a text file use source or \. takes the filename as an argument do not use quotes or semicolon \. C:/data/insert_record.sql source path_to/file_name.txt \. path_to/file_name.txt
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.