1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
| select * from orders
order by 2,3
show engines
show variables like '%innodb%'
show variables like '%storage_engine%' --查看mysql当前默认的存储引擎:
show table status from test where name ='test'--查看test库的test表的存储引擎信息
----创建表,并指定其存储引擎为Innodb
USE TEST
create table test_engine (id int not null auto_increment,
temp varchar(10),--varchar类型需要指定长度,否则会报错无法创建表
primary key (id)
) engine = innodb
--更改表的存储引擎
alter table engine= myisam--报错,不知道为何
--小练习1
create table example0(id int not null auto_increment primary key,
name varchar(20),
sex boolean --等同与tinyint(1)
)
--小练习2--组合主键
use test
create table sc(sNo int not null,
cNo int not null,
/*突然想起了select 2 from table的问题了,试验一下
use world;
select 2 from world.city --结果为选出一列n行的2,n为city表的行数
*/
grade int default '0',--不能少逗号,即使下面没有属性声明只有主键定义。。。
primary key (sNo,cNo)
)
use test;
create table sc(sNo int ,--这里之前有not null 然后创建的时候就一直出错
cNo int ,--同上(上面一个例子是不是也是这个原因?不明白为啥,实验一下)
grade int default '0',
primary key (sNo,cNo)
)
create table example1(id int not null auto_increment primary key,
name varchar(20),--果然不加(20)就会报错,为什么?
sex boolean
)
/*子表的外键必须是父表的主键,且数据类型需一致*/
create table example3(id int primary key,
stu_id int,
course_id int,
constraint c_fk foreign key(stu_id,course_id) references example2(stu_id,course_id) --报错,无法创建example3表。查询书籍,可能是被依赖的example2表及其属性不存在
--先回来创建表example2
)
create table example2(stu_id int,
course_id int,
grade float,
primary key(stu_id,course_id)
)
create table example3(id int primary key,
stu_id int,
course_id int,
constraint c_fk foreign key(stu_id,course_id) references example2(stu_id,course_id)
) --Command(s) completed successfully.
/*字段唯一性约束,字段自增属性,字段默认值(之前的例子中,int默认值0不加‘’报错,再预先实验一下)*/
--把小练习2复制过来,以后的例子都要编号,方便调用。。。
create table sc1(sNo int not null,
cNo int not null,
grade int default 0,
primary key (sNo,cNo))--Command(s) completed successfully.看来是我自己弄错了
--小练习3
create table example7(id int primary key auto_increment,
stu_id int unique,
name varchar(20) not null,
English varchar(20) default 'zero',
Math float default 0,
Computer Float default 0
) --Command(s) completed successfully.关于表的练习结束
|