帮我创建一个mysql表,并插入1000条数据
时间: 2023-08-12 08:13:01 浏览: 245
好的,以下是创建一个名为`my_table`的表,并插入1000条数据的MySQL语句:
```SQL
CREATE TABLE my_table (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT(3) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO my_table (name, age) VALUES
('Alice', 25),
('Bob', 30),
('Charlie', 35),
('David', 40),
('Emily', 45),
('Frank', 50),
('Grace', 55),
('Henry', 60),
('Isaac', 65),
('Jane', 70);
-- 插入900条随机数据
INSERT INTO my_table (name, age)
SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 8), FLOOR(RAND() * 100)
FROM information_schema.tables
LIMIT 900;
```
这个表包含3个列:`id`、`name`和`age`。其中`id`是自增长主键,`name`和`age`是必填字段。前10条数据是手动插入的,剩下的900条是随机生成的。
相关问题
帮我写一个mysql语句,创建一个数据库,并创建一个表,插入1000条数据
可以使用以下的 SQL 语句来创建一个名为 mydatabase 的数据库,并在其中创建名为 mytable 的表,并随机插入 1000 条数据:
```
CREATE DATABASE mydatabase;
USE mydatabase;
CREATE TABLE mytable (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
age INT(3) NOT NULL
);
INSERT INTO mytable (name, age)
SELECT
CONCAT('Name', FLOOR(RAND() * 1000)) AS name,
FLOOR(RAND() * 100) AS age
FROM
information_schema.TABLES
LIMIT 1000;
```
这将创建一个包含两个列(name 和 age)的表,每个条目都有一个自动递增的 ID,并且随机生成 1000 个名字和年龄。
c语言mysql同时创建多个表并插入数据
要使用 C 语言操作 MySQL 数据库,需要使用 MySQL C API 库。下面是一个简单的示例代码,可以同时创建多个表并插入数据。
```c
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "password"; // 替换为你自己的 MySQL root 用户密码
char *database = "test";
conn = mysql_init(NULL);
// 连接 MySQL 数据库
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
// 创建多个表并插入数据
if (mysql_query(conn, "CREATE TABLE table1 (id INT, name VARCHAR(20))") != 0) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "INSERT INTO table1 VALUES (1, 'John')") != 0) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "CREATE TABLE table2 (id INT, name VARCHAR(20))") != 0) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "INSERT INTO table2 VALUES (2, 'Mary')") != 0) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
// 查询数据
if (mysql_query(conn, "SELECT * FROM table1")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
printf("table1:\n");
while ((row = mysql_fetch_row(res)) != NULL) {
printf("%s %s\n", row[0], row[1]);
}
mysql_free_result(res);
if (mysql_query(conn, "SELECT * FROM table2")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
printf("table2:\n");
while ((row = mysql_fetch_row(res)) != NULL) {
printf("%s %s\n", row[0], row[1]);
}
mysql_free_result(res);
mysql_close(conn);
return 0;
}
```
这个例子中创建了两个表,分别为 `table1` 和 `table2`,并向每个表中插入了一条数据。然后查询了这两张表中的数据。在实际使用中,需要根据自己的需求进行修改。