数据库语句

oracle:

 1 //在cmd模式下登录数据库
 2 sqlplus
 3 sys as sysdba
 4 //建立表空间
 5 create tablespace zjl datafile 'd:\zjl.dbf' size 20m;
 6 //创建用户
 7 create user zjl identified by zjl default tablespace zjl;
 8 //授权
 9 grant connect,resource to zjl;
10 //切换用户
11 conn zjl/zjl;
12 //创建userinfos表
13 create table userinfos(
14     userid number(10) primary key not null,
15       username varchar(20) not null,
16       birthday date not null
17 );
18 //创建cards表
19 create table cards(
20     cardid number(10) primary key not null,
21       userid number(10) references userinfos(userid) not null,
22       balance number(10,2) not null
23 );
24 //创建序列 25 create sequence seq_userid; 26 create sequence seq_cardid; 27 //插入第一张表数据 28 begin 29 for rn in 1..22 loop 30 insert into userinfos values(seq_userid.nextval,dbms_random.string('a',6),sysdate+rn); 31 end loop; 32 commit; 33 end; 34 / 35 36 //使用游标给第二张表插入数据 37 declare 38 cursor cs is select userid from userinfos; 39 begin 40 for rn in cs loop 41 insert into cards values(seq_cardid.nextval,rn.userid,trunc(dbms_random.value(100,1000),2)); 42 end loop; 43 commit; 44 end; 45 /

1 //创建表之后再创建主键及外键的写法如下:
2 //创建userinfos表的主键
3 alter table userinfos add constraint PK_userid primary key (userid);
4 //创建cards表的主键及外键
5 alter table cards add constraint PK_cardid primary key (cardid);
6 alter table cards add constraint FK_user_card foreign key (userid) references userinfos(userid); 
1 //删除表空间
2 drop tablespace userinfos including contents and datafiles;
3 //删除用户
4 drop user zjx cascade;

mysql:

 1 mysql -u root -p
 2 create database goods;
 3 use goods;
 4 create table customs(
 5         custid int primary key auto_increment not null,
 6         account varchar(20) not null,
 7         password varchar(40) not null,
 8         address varchar(50) not null,
 9         telephone varchar(11) not null
10         );
11 create table goods(
12         goodid int primary key auto_increment not null,
13         title varchar(50) not null,
14         pirce numeric(10,2) not null,
15         picture varchar(40) not null,
16         shop varchar(50) not null,
17         store int not null
18         );
19 create table buycar(
20         carid int primary key auto_increment not null,
21         buydate date not null,
22         custid int not null
23         );
24         
25 create table carandgood(
26         cagid int primary key auto_increment not null,
27         carid int not null,
28         goodid int not null,
29         buynum int not null
30     );
31 create table orders(
32         orderid int primary key auto_increment not null,
33         orderdate date not null,
34         custid int not null
35         );
36 create table orderandgood(
37         oagid int primary key auto_increment not null,
38         goodid int not null,
39         orderid int not null,
40         ordernum int not null
41         );
42 alter table buycar add constraint FK_cust_car foreign key (custid) references customs(custid);
43 alter table carandgood add constraint FK_car_cag foreign key (carid) references buycar(carid);
44 alter table carandgood add constraint FK_good_cag foreign key (goodid) references goods(goodid);
45 alter table orderandgood add constraint FK_good_oag foreign key (goodid) references goods(goodid);
46 alter table orderandgood add constraint FK_order_oag foreign key (orderid) references orders (orderid);
47 alter table orders add constraint FK_cust_order foreign key (custid) references customs (custid);
48 
49 insert into customs(account,password,address,telephone) values('admin','123456','南京晓航路','18652');
50 
51 insert into goods(title,pirce,picture,shop,store) values('物美价廉,货品有限',1000,'good.jpg','Themis',200);
52 insert into goods(title,pirce,picture,shop,store) values('物美价廉,货品有限',456,'good.jpg','句句句',123);
53 insert into goods(title,pirce,picture,shop,store) values('物美价廉,货品有限',7889,'good.jpg','谷歌',134);
54 insert into goods(title,pirce,picture,shop,store) values('物美价廉,货品有限',567,'good.jpg','订单',156);
55 insert into goods(title,pirce,picture,shop,store) values('物美价廉,货品有限',89,'good.jpg','搜索',167);
56 insert into goods(title,pirce,picture,shop,store) values('物美价廉,货品有限',45,'good.jpg','广告',234);
57 insert into goods(title,pirce,picture,shop,store) values('物美价廉,货品有限',45,'good.jpg','解决',234);
58 insert into goods(title,pirce,picture,shop,store) values('物美价廉,货品有限',345,'good.jpg','UI哦',3435);
59 insert into goods(title,pirce,picture,shop,store) values('物美价廉,货品有限',678,'good.jpg','儿童',23);
60 insert into goods(title,pirce,picture,shop,store) values('物美价廉,货品有限',3456,'good.jpg','好几款',56);

 

posted @ 2017-05-31 14:08  CoderMap  阅读(201)  评论(0)    收藏  举报