And index management table, the foreign key role:
Create database
Create Schema IF Not EXISTS Students. Character SET 'GBK' COLLATE 'gbk_chinese_ci';
CD Students.
LS
File the db.opt
CAT the db.opt
default Character-SET = GBK-
default-collation = gbk_chinese_ci
modify the database
Help the ALTER database;
Help drop databases;
Help the create the table;
3 ways to create tables:
1) directly define an empty table;
2) Creating new tables from query results
3) template creates an empty table like a key in another table word
single field:
primary key primary key
unique key unique key
mono- or fields:
primary key (COL, ...)
unique key (COL, ...)
index (COL, ...)
Create Table IF Not EXISTS tb_name (col_name col_definition, constraint)
Create Table TB1 (ID int unsigned Not null AUTO_INCREMENT Primary Key, name char (20 is) Not null, Age tinyint Not null)
ID field as the primary key to
create table tb2 (id int unsigned not null auto_increment, name char (20) not null, age tinyint not null, primary key ( id))
master key, the only key index field
create table tb3 (id int unsigned not null auto_increment, name char (20) not null, age tinyint not null, primary key (id), unique key (name), index (age) ) engine = engine_name
primary key unique key index keys but do not:
key also known constraints, can be used as the index, it is a special index (special Limited): B + Tree
Create index
Create Table courses (unsigned tinyint Not null AUTO_INCREMENT CID Primary Key, Couse VARCHAR (50) Not null) = Engine InnoDB;
Show Status Table like 'courses';
Show Status Table like 'courses' \ G
drop Table courses;
INSERT INTO courses (Couse) values ( 'Python'), ( 'c ++'), ( 'MySQL');
the SELECT * from courses;
Show the Indexes from courses;
displays the index specified table
show indexes from tb_name;
query results to create a new table
Create table testcourses SELECT * from courses WHERE CID <= 2;
Show tables;
SELECT * from testcourses;
display list structure
desc courses;
desc testcourses;
other tables template to create an empty table
Create table Test like courses;
desc Test;
modified table structure : ALTER table
Help ALTER table
CRUD field
CRUD index
modifying table attributes
Show indexes from Test;
ALTER table Test the Add uNIQUE key (Couse); add a unique key index
alter table change couse course varchar (50 ) not null test; modify the field name
alter table test add startdate date default ' 2020-01-01'; increased field
Help the ALTER the Table
the ALTER the Table the rename the Test AS | to courses2; modify the table name
rename table courses2 to test;
delete the table
Help drop the Table
drop the Table IF EXISTS tb_name;
the Create Table Student (SID int unsigned Not null AUTO_INCREMENT Primary Key, name VARCHAR (30), CID int Not null);
INSERT INTO Student (name, CID) values ( 'WQD', 2), ( 'ZQD',. 1);
SELECT from Student *;
SELECT * from courses;
SELECT name, from Student Couse, WHERE student.cid = courses.cid courses;
INSERT INTO Student (name, CID) values ( 'Chenchen',. 5);
Delete from Student WHERE CID =. 5 ;
add foreign key
role: cid cid student table and curriculum references foreign key constraints, the students will complain when inserted into the curriculum table does not exist
ALTER Table Student Modify CID tinyint unsigned Not null;
ALTER Table Student the Add Foreign Key foreign_cid (CID) References courses (CID);
Show Indexes from Student;
modified table Engine:
ALTER Table courses Engine = InnoDB;
ALTER Table courses Engine = mysiam;
INSERT INTO Student (name, cid) values ( 'Chenchen', 3);
the delete from courses the WHERE cid = 3; will complain
only innodb engine is supported foreign key
foreign key constraints to prevent the table from accidental deletion
index: create and modify
help create index;
Help index drop;
Create index index_name ON tb_name;
drop index index_name ON tb_name;
Create index name_on_student ON Student (name) the using btree;
Create index name_on_student ON Student (name (five) desc) the using btree; descending character length 5
drop index name_on_student on student;