- 论坛徽章:
- 0
|
linux数据库的练习
create table staff(
id int not null auto_increment,
name varchar(30) not null,
depart varchar(30) not null,
post varchar(20),
primary key(id)
)engine=innoDB charset=utf8;
insert into staff(id,name,depart,post) values(1,'zhang','教学部','讲师');
create table pers_info(
id int not null,
age int,
tel int,
email varchar(30),
wage int,
foreign key(id) references staff(id)
)engine=innoDB charset=utf8;
insert into pers_info(id,age,tel,email,wage) values(1,20,62760823,'zhang@sxkj.com',3000);
select * from pers_info order by age desc,wage desc;
insert into pers_info(id,age,tel,email,wage) values(2,23,62760303,'yang@sina.com',2890);
insert into pers_info(id,age,tel,email,wage) values(4,null,62760998,'xiejinghui@sina.com',3100);
create database sxkjtest;
create table t1(
id int not null,
name char(40),
city char(40),country char(40),
email char(40),
primary key(id))engine=innoDB charset=utf8;
create table t2(
id int not null,
name char(40) not null,
age int null,
tel int null)engine=innoDB charset=utf8;
create table t3(
id int not null,
name char(30) not null,
tel int,
addr char(40) not null,
primary key(id))engine=innoDB charset=utf8;
create table t5(
id int not null primary key auto_increment,
name char(40) not null
)engine=innoDB charset=utf8;
create table t4(
id int not null,
addr char(40) null,
foreign key(id) references t1(id))engine=innoDB charset=utf8;用foreign key 指定id列为外键,关键字references 决定来该列外键的值引用自表t1的主键id列。表t4的id列的所有取值必须是在表t1的id列中出现的值。
show create table t4;
alter table t4 engine=innoDB;
alter table t4 charset=utf8;
alter table t5 rename t6; 将表名T5改为表T6.
alter table t5 add (city char(40) not null); 使用alter table 在定义完成之后对表进行添加或删除列一些。
alter table t5 drop city;使用 alter table 修改表时,关键字 drop 表示删除之前添加的列。
modify 更新
alter table t4 modify addr char(50) not null default 'changsha';
修改表t4中的addr列数据类型为长度为50的字符类型,并且不为null,默认值为'changsha'.
alter table 修改表时,使用change关键字可以对列进行重命令。
alter table t4 change addr address char(40) not null default 'changsha';
drop table t5;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bs |
| ds |
| mysql |
| sxkjtest |
| test |
+--------------------+
drop database ds;
insert into t1(id,name,city,country,email) values(2,'yangbei','hainan','china','yangbei@qq.com'),(3,'wangguanwen','hainan','china','wangguanwen@sina.com');使用inser into语句一次插入多条数据时,每行数据放在一对圆括号内,多行数据之间使用逗号分隔。
|
|