Examples of database and table building in MySQL

Source: Internet
Author: User
Tags create database

An instance of building a library and building a table

The code is as follows Copy Code


Drop database if exists school; Delete if there is school
Create Database School; Build a library School
Use school; Open Library School
CREATE TABLE teacher//Create tables Teacher
(
ID int (3) auto_increment NOT null primary key,
Name Char (a) NOT NULL,
Address varchar default ' Shenzhen ',
Year Date
); Build Table End

The following is the Insert field
Insert into teacher values (", ' Allen ', ' Dalian One ', ' 1976-10-10′ ');
Insert into teacher values (", ' Jack ', ' Dalian II ', ' 1975-12-23′");

If you type the above command at the MySQL prompt, it's not easy to debug.
1, you can write the above command as is written in a text file, assume that the school.sql, and then copy to C:\, and in the DOS state into the directory [Url=file://\mysql\bin]\mysql\bin[/url], and then type the following command:
Mysql-uroot-p Password < C:\school.sql
If successful, there is no display on a single line, and if there is an error, there is a hint. (The above command has been debugged and you can use it only if you remove the comment.)

2, or enter the command line after the use of mysql> source c:\school.sql; You can also import the School.sql file into the database.

  code is as follows copy code

Drop database if exists school; If a school is present, delete the
Create DATABASE school//build library school
Use School;//Open Library school
CREATE TABLE teacher//CREATE TABLE teacher
(
    ID int (3) auto_increment NOT NULL primary key,
    name Char (TEN) NOT NULL ,
    address varchar ($) Default ' Shenzhen ',
    year date
);//table End

//below Insert Into the field
insert into teacher values (', ' ' Glchengang ', ' ', ' ' Shenzhen ', ' 1976-10-10 ');
insert into teacher values (' ", ' ' Jack ', ' Shenzhen One ', ' 1975-12-23 ';

Note: In the construction of the table
1. Set the ID to a number field of length 3: Int (3), and let it automatically add one to each record: auto_increment; not null; and let him be the main field primary key.

2, set name to a length of 10 character field

3, the address set to the length of 50 character field, and the default value is Shenzhen.

4. Set year as 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.