Create databases and tables in mysql

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.