centos7安装MySQL后创建表、字段,设置字段与字段之间的关系
时间: 2023-12-10 09:37:54 浏览: 65
安装MySQL:
1. 安装MySQL:
```bash
sudo yum install mysql-server
```
2. 启动MySQL服务:
```bash
sudo service mysqld start
```
3. 修改MySQL的root用户密码:
```bash
sudo mysql_secure_installation
```
创建表:
1. 登录MySQL:
```bash
mysql -u root -p
```
2. 创建数据库:
```mysql
CREATE DATABASE mydatabase;
```
3. 选择数据库:
```mysql
USE mydatabase;
```
4. 创建表:
```mysql
CREATE TABLE users (
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
```
设置字段与字段之间的关系:
1. 添加外键:
```mysql
ALTER TABLE orders
ADD CONSTRAINT fk_orders_users
FOREIGN KEY (user_id) REFERENCES users(id);
```
2. 删除外键:
```mysql
ALTER TABLE orders
DROP FOREIGN KEY fk_orders_users;
```
阅读全文