Constraints:
auto_increment --- self-increment
not null --- cannot be empty
default 'x' --- default value
unique --- unique constraint
charset --- specified character set
primary key --- primary key (unique, cannot Empty)
foreign key --- used to represent the association between two tables (too many associations, affecting performance)
View tables:
View all tables: show tables;
View table structure: desc bhz;
View table creation statements: show create table tablename;
Create table:
###Grade table###
create table score(
id int auto_increment primary key,
s_id int not null,
grade float not null
);
## Student table ###
create table bhz (
id int auto_increment primary key, --- primary key, self-increasing
name varchar(10) not null, --- cannot be empty
sex varchar(2) default 'male' --- Default value
addr varchar(50),
phone int unique --- unique constraint
);
Note: first use the corresponding database
Drop table:
drop table bhz;
Modify the table:
Modify the table name: alter table bhz rename ads;
Modify field data type:
1. alter table ads modify addr varchar(100);(modify + field name + new data type)
2. alter table ads change name stu_name varchar(30); (change + old field name + new field name + new data type)
Add fields:
1. alter table ads add money float; (added at the end by default)
2. alter table ads add money float after sex; (added after the sex field)
3. alter table ads add money float first; (added at the end Front)
Delete field: alter table ads drop money;