centos7安装MySQL后创建表、字段,设置字段与字段之间的关系
时间: 2024-05-26 08:14:00 浏览: 12
安装MySQL:
1. 安装MySQL:sudo yum install mysql-server
2. 启动MySQL服务:sudo systemctl start mysqld
3. 设置MySQL自启动:sudo systemctl enable mysqld
4. 确认MySQL能否正常工作:sudo systemctl status mysqld
创建表:
1. 登录MySQL:mysql -u root -p
2. 创建数据库:CREATE DATABASE database_name;
3. 选择数据库:USE database_name;
4. 创建表:CREATE TABLE table_name (column_name1 data_type1, column_name2 data_type2, ...);
设置字段与字段之间的关系:
1. 主键:PRIMARY KEY
ALTER TABLE table_name ADD PRIMARY KEY (column_name);
2. 外键:FOREIGN KEY
ALTER TABLE table_name ADD FOREIGN KEY (column_name) REFERENCES reference_table (reference_column);
示例:
1. 创建数据库:CREATE DATABASE mydb;
2. 选择数据库:USE mydb;
3. 创建表:
CREATE TABLE users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL
);
CREATE TABLE orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
order_date DATE NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)