This article briefly introduces the mysql database creation and table creation instances.
A database creation and table creation instance
The Code is as follows: |
Copy code |
Drop database if exists school; // Delete if SCHOOL exists Create database school; // create a database SCHOOL Use school; // open the SCHOOL library Create table teacher // create table TEACHER ( Id int (3) auto_increment not null primary key, Name char (10) not null, Address varchar (50) default 'shenzhen ', Year date ); // Table creation ends
// Insert fields as follows Insert into teacher values (", 'allen ', 'dalian Zhongyi', '2017-10-10 ′); Insert into teacher values (", 'jack', 'dalian No. 2 middle school ', '2017-12-23 ′); |
If you type the preceding command at the mysql prompt, debugging is not convenient.
1. You can write the above commands into a text file as they are, for example, school. SQL, then copy it to c: \, and enter the directory [url = file: // \ mysql \ bin] \ mysql \ bin [/url] in DOS status. run the following command:
Mysql-uroot-p password <c: \ school. SQL
If it succeeds, no display is displayed for a blank row. If there is an error, a prompt is displayed. (The preceding command has been debugged. You only need to remove the // annotation to use it ).
2. You can use mysql> source c: \ school. SQL after entering the command line. You can also import the school. SQL file to the database.
The Code is as follows: |
Copy code |
Drop database if exists school; // Delete if SCHOOL exists Create database school; // create a database SCHOOL Use school; // open the SCHOOL library Create table teacher // create table TEACHER ( Id int (3) auto_increment not null primary key, Name char (10) not null, Address varchar (50) default ''shenzhen '', Year date ); // Table creation ends // Insert fields as follows Insert into teacher values (''', ''glengang '', ''shenzhen Zhongyi ''', ''2017-10-10 ''); Insert into teacher values (''', ''jack'', ''shenzhen No. 1 Middle School ''', ''2017-12-23 ''); |
Note: Table Creation
1. Set the ID to a numeric field with a length of 3: int (3), and enable it to automatically add auto_increment for each record; it cannot be blank: not null; in addition, set the primary key as the primary key.
2. Set NAME to a 10-character field
3. Set ADDRESS to a 50-character field, and the default value is Shenzhen.
4. Set YEAR as the date field.