create table SM_student( lt Line 2 id int(10)primary key auto_increment, name varchar(30) not null. age int(10), sex enum('w,'m')default'w' phone int(11), teacher_id int(10)not null );
时间: 2024-04-25 18:26:33 浏览: 84
这是一段 SQL 语句,用于创建一个名为 SM_student 的表,其中包含以下列:
- id:整数类型,主键,自增长;
- name:字符串类型,长度为 30,不允许为空;
- age:整数类型,长度为 10;
- sex:枚举类型,只能取值为 'w' 或 'm',默认值为 'w';
- phone:整数类型,长度为 11;
- teacher_id:整数类型,不允许为空。
这个表可能用于存储学生的信息,包括姓名、年龄、性别、电话等,同时还关联了一个教师的 ID。
相关问题
CREATE TABLE customer ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, gender ENUM('男', '女') NOT NULL, phone VARCHAR(20) NOT NULL, address VARCHAR(100) NOT NULL ); CREATE TABLE car_model( id INT PRIMARY KEY AUTO_INCREMENT, brand VARCHAR(50) NOT NULL, model VARCHAR(50) NOT NULL, price DECIMAL(10,2) NOT NULL ); CREATE TABLE car( model_id INT PRIMARY KEY AUTO_INCREMENT, color VARCHAR(50) NOT NULL, description VARCHAR(50) NOT NULL ); CREATE TABLE order1( id INT PRIMARY KEY AUTO_INCREMENT, customer_id INT NOT NULL, car_id INT NOT NULL, order_date DATETIME NOT NULL , price DECIMAL(10,2) NOT NULL, ); CREATE TABLE employee( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, gender ENUM('男', '女') NOT NULL, phone VARCHAR(20) NOT NULL, address VARCHAR(100) NOT NULL, salary DECIMAL(10,2) NOT NULL给出五个存储函数以及调用语句 );
存储函数1:
DELIMITER //
CREATE FUNCTION get_total_price(order_id INT)
RETURNS DECIMAL(10,2)
BEGIN
DECLARE total_price DECIMAL(10,2);
SELECT SUM(price) INTO total_price FROM order1 WHERE id = order_id;
RETURN total_price;
END//
DELIMITER ;
调用语句:SELECT get_total_price(1);
存储函数2:
DELIMITER //
CREATE FUNCTION get_customer_orders(customer_name VARCHAR(50))
RETURNS INT
BEGIN
DECLARE order_count INT;
SELECT COUNT(*) INTO order_count FROM order1 o JOIN customer c ON o.customer_id = c.id WHERE c.name = customer_name;
RETURN order_count;
END//
DELIMITER ;
调用语句:SELECT get_customer_orders('John Doe');
存储函数3:
DELIMITER //
CREATE FUNCTION get_employee_salary_increase(employee_id INT, percent DECIMAL(10,2))
RETURNS DECIMAL(10,2)
BEGIN
UPDATE employee SET salary = salary * (1 + percent/100) WHERE id = employee_id;
RETURN salary;
END//
DELIMITER ;
调用语句:SELECT get_employee_salary_increase(1, 10);
存储函数4:
DELIMITER //
CREATE FUNCTION get_model_count(brand_name VARCHAR(50))
RETURNS INT
BEGIN
DECLARE model_count INT;
SELECT COUNT(*) INTO model_count FROM car_model WHERE brand = brand_name;
RETURN model_count;
END//
DELIMITER ;
调用语句:SELECT get_model_count('Toyota');
存储函数5:
DELIMITER //
CREATE FUNCTION get_car_info(car_id INT)
RETURNS VARCHAR(100)
BEGIN
DECLARE car_info VARCHAR(100);
SELECT CONCAT(c.color, ' ', cm.brand, ' ', cm.model) INTO car_info FROM car c JOIN car_model cm ON c.model_id = cm.id WHERE c.id = car_id;
RETURN car_info;
END//
DELIMITER ;
调用语句:SELECT get_car_info(1);
test_sql=""" CREATE TABLE IF NOT EXISTS user_students( id INT auto_increment PRIMARY KEY, name varchar(20) not null, sex char(2) not null, age int(2) not null, banji varchar(20) not null, phonenumber char(11) not null, password varchar(20) not null )ENGINE=innodb DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS user_teacher( id INT auto_increment PRIMARY KEY, name varchar(20) not null, sex char(2) not null, age int(2) not null, salary int(11) not null, teach_course varchar(11) not null, qualification varchar(10) not null, phonenumber char(11) not null, password varchar(20) not null )ENGINE=innodb DEFAULT CHARSET=utf8; CREATE TABLE courses ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, course_teacher VARCHAR(50) NOT NULL, credit INT NOT NULL, FOREIGN KEY (name) REFERENCES user_teacher(teach_course), FOREIGN KEY (course_teacher) REFERENCES user_teacher(name) ); CREATE TABLE enrollments ( id INT PRIMARY KEY AUTO_INCREMENT, student_id INT NOT NULL, course_id INT NOT NULL, FOREIGN KEY (student_id) REFERENCES user_students(id), FOREIGN KEY (course_id) REFERENCES courses(id) ); CREATE TABLE grades ( id INT PRIMARY KEY AUTO_INCREMENT, enrollment_id INT NOT NULL, grade FLOAT NOT NULL, FOREIGN KEY (enrollment_id) REFERENCES enrollments(id) ); """curs.execute(test_sql)哪错了,为什么数据库没有这些表
根据提供的 SQL 语句,可以看出这是创建数据库表的语句。但是,如果数据库中没有这些表,可能有以下原因:
1. 数据库连接失败:在执行这些 SQL 语句之前,需要先连接到数据库。如果连接失败,则无法执行 SQL 语句。
2. 数据库名不匹配:在执行 SQL 语句时,需要指定正确的数据库名。如果数据库名不匹配,则无法创建表。
3. 数据表名不匹配:如果数据库中已经存在同名的数据表,则无法再创建同名的数据表。
4. 数据库权限问题:如果当前用户没有创建表的权限,则无法执行创建表的 SQL 语句。
你可以检查以上这些原因,看看是否能够解决问题。
阅读全文