Summary of MySQL statement table Creation

Source: Internet
Author: User
Before writing code, you need to note that you can use the command line mode to write MySQL statements. If it involves Chinese characters, you 'd better use mysqlgt; charsetGBK;

Before writing code, you need to note that you can use the command line mode to write MySQL statements. If it involves Chinese characters, you 'd better use mysqlgt; charset GBK;

Before writing code, you need to note that you can use the command line mode to write MySQL statements. If it involves Chinese characters, you 'd better use mysql> charset GBK. The command sets the client character set to GBK, this ensures that Chinese characters are correctly inserted and read (database character sets can be gb2312, GBK, utf8, and other Chinese character sets ).

If you are using the Mysql Query Browser tool provided by MySQL to write scripts, we recommend that you first use text tools such as ultra edit (not to use Notepad, because there may be character encoding problems, we recommend ultra edit) write the code, paste it into the Script Tab of QueryBrowser, and execute it. Because QueryBrowser has a bug in Chinese support and cannot enter Chinese correctly, write it elsewhere.

Several tables are written, including the settings of the foreign key primary key. The table creation statement is as follows:
USE testdatabase;
Create table class (
Class_id integer unsigned AUTO_INCREMENT primary key,
Class_name VARCHAR (40)
);


Create table student (
Student_id integer unsigned not null AUTO_INCREMENT,
Primary key (student_id ),
Student_name varchar (40 ),
Class_id integer unsigned,
CONSTRAINT FK_student_class foreign key (class_id) REFERENCES class (class_id) ON DELETE CASCADE ON UPDATE CASCADE
);

Create table lession (
Lession_id integer unsigned not null AUTO_INCREMENT,
Primary key (lession_id ),
Lession_name varchar (40)
);

Create table core (
Core_id integer unsigned AUTO_INCREMENT,
Lession_id integer unsigned not null,
Student_id integer unsigned not null,
Core FLOAT,
Close_status integer default 0,
CONSTRAINT PK_core primary key (
Core_id
),
CONSTRAINT FK_core_lession foreign key (lession_id) REFERENCES lession (lession_id ),
CONSTRAINT FK_core_student foreign key (student_id) REFERENCES student (student_id)
ON DELETE CASCADE
);

Create table teacher (
Teacher_id integer unsigned not null AUTO_INCREMENT,
Primary key (teacher_id ),
Teacher_name varchar (40)
);

Create table teachlession (
Teachlession_id integer primary key AUTO_INCREMENT,
Lession_id integer unsigned,
Teacher_id integer unsigned,
CONSTRAINT FK_teachlession_lession foreign key (lession_id) REFERENCES lession (lession_id)
On delete cascade,
CONSTRAINT FK_teachlession_teacher foreign key (teacher_id) REFERENCES teacher (teacher_id)
ON DELETE CASCADE
);

Create table sySUSEr (
Sysuser_id integer unsigned not null primary key AUTO_INCREMENT,
Sysuser_name VARCHAR (40 ),
Sysuser_password VARCHAR (40 ),
Sysuser_role INTEGER,
Foreign_id INTEGER
);


/* This is used to create a table by specifying the character set of the table and column */
Use testdatabase;
Drop table if exists chartable;
Create table chartable (
Name varchar (30) character set utf8 collate utf8_general_ci
) Character set latin1 collate latin1_danish_ci;

Insert into chartable values ('Our World ');

Summary:
1. The order of table creation statements must conform to the relationship between tables. For example, the primary table should be created before the table with a foreign key relationship.
2. For foreign key declaration, the two fields must be of the same type.
3. Note that the encoding format of the Code is consistent with that of the database character set.

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.