|
|
MySQL training course - Creating tablesIn this section of the mysql training course we will explore the MySQL commands to create database tables and selecting the database. Databases store data in tables. So what are these tables?
The table above contains four columns that store the name, age, country and email. Each row contains data for one individual. This is called a record. To find the country and email of Alexander, you'd first pick the name from the first column and and then look in the third and fourth columns of the same row. A database can have many tables; it is tables, that contain the actual data. Hence, we can segregate related (or unrelated) data in different tables. For our employees database we'll have one table that stores company details of the employees. The other table would contain personal information. Let's make the first table. The SQL command for creating tables looks complex when you view it for the first time. Don't worry if you get confused, we'll be discussing this in more detail in later sessions. CREATE TABLE employee_data ( emp_id int unsigned not null auto_increment primary key, f_name varchar(20), l_name varchar(20), title varchar(30), age int, yos int, salary int, perks int, email varchar(60) ); Note: In MySQL, commands and column names are not case-sensitive; however, table and database names might be sensitive to case depending on the platform (as in Linux). You can thus, use create table instead of CREATE TABLE. Why have a column with unique values? Using a database SELECT DATABASE(); The system responds with mysql> SELECT DATABASE(); +------------+ | DATABASE() | +------------+ | | +------------+ 1 row in set (0.01 sec) The above shows that no database has been selected. Actually, everytime we work with mysql client, we have to specify which database we plan to use. There are several ways of doing it. It's necessary to specify the database we plan to use, else MySQL will throw an error. Creating tablesOnce you've selected the employees database, issue the CREATE TABLE command at the mysql prompt. CREATE TABLE employee_data ( emp_id int unsigned not null auto_increment primary key, f_name varchar(20), l_name varchar(20), title varchar(30), age int, yos int, salary int, perks int, email varchar(60) ); Note: When you press the enter key after typing the first line, the mysql prompt changes to a ->. This means that mysql understands that the command is not complete and prompts you for additional statements. Remember, each mysql command ends with a semi-colon and each column declaration is separated by a comma. Also, you can type the entire command on one line if you so want.
mysql> CREATE TABLE employee_data
-> (
-> emp_id int unsigned not null auto_increment primary key,
-> f_name varchar(20),
-> l_name varchar(20),
-> title varchar(30),
-> age int,
-> yos int,
-> salary int,
-> perks int,
-> email varchar(60)
-> );
Query OK, 0 rows affected (0.01 sec)
Okay, we just made our first table.
Page contents: Mysql training course - creating tables in mysql - mysql create table command - mysql select database
Page URL: http://www.webdevelopersnotes.com/tutorials/sql/ mysql_training_course_creating_tables.php3
|
|