赞
踩
1.表的设计和创建
2.在navicate运行这些代码
- create table user(
- id int not null auto_increment primary key,
- name varchar(50) not null unique,
- password varchar(50) not null,
- state enum('online','offline') default 'offline'
- );
- create table friend(
- userid int not null,
- friendid int not null
- );
- alter table friend
- add constraint pk_friend primary key(userid,friendid);
-
- create table allgroup(
- id int not null auto_increment primary key,
- groupname varchar(50) not null,
- groupdesc varchar(200) default ''
- );
-
- create table groupuser(
- groupid int not null primary key,
- userid int not null,
- grouprole enum('creator','normal') default 'normal'
- );
-
- create table offlinemessage(
- userid int not null primary key,
- message varchar(500) not null
- );
参考和推荐文章:
mysql 设置默认为空字符串
mysql 创建表格 枚举类型:
mysql 创建联合主键
在我们后面的项目中, 有用到这些表,在ubutun linux中,将我们上面写好的sql代码粘贴进去按下
- mysql> mysql -u root -p
- mysql> show databases;
- mysql> create database chat;
-
- mysql> use chat;
- Database changed
- mysql> create table user(
- -> id int not null auto_increment primary key,
- -> name varchar(50) not null unique,
- -> password varchar(50) not null,
- -> state enum('online','offline') default 'offline'
- -> );
- Query OK, 0 rows affected (0.02 sec)
-
- mysql> create table friend(
- -> userid int not null,
- -> friendid int not null
- -> );
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> alter table friend
- -> add constraint pk_friend primary key(userid,friendid);
- Query OK, 0 rows affected (0.01 sec)
- Records: 0 Duplicates: 0 Warnings: 0
-
- mysql> create table allgroup(
- -> id int not null auto_increment primary key,
- -> groupname varchar(50) not null,
- -> groupdesc varchar(200) default ''
- -> );
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> create table groupuser(
- -> groupid int not null primary key,
- -> userid int not null,
- -> grouprole enum('creator','normal') default 'normal'
- -> );
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> create table offlinemessage(
- -> userid int not null primary key,
- -> message varchar(500) not null
- -> );
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> show tables;
- +----------------+
- | Tables_in_chat |
- +----------------+
- | allgroup |
- | friend |
- | groupuser |
- | offlinemessage |
- | user |
- +----------------+
- 5 rows in set (0.00 sec)
-
- mysql>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。