-- 新建數據庫 CREATE DATABASE `school`; -- 使用數據庫 USE `SCHOOL`; -- 建立字段 -- id:學生的ID -- name:學生名稱 -- nickname:學生暱稱 -- sex:性別 -- in_time:入學時間 CREATE TABLE `student`( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(20) NOT NULL, `nickname` VARCHAR(20) NULL, `sex` SMALLINT NOT NULL, `in_time` DATETIME NULL ); -- 插入語句 INSERT INTO `student` VALUES(1,'張三','三哥','1',now()) -- 插入自增語句 INSERT INTO `student`(`name`,`sex`) VALUES ('張三1','1'), ('張三2','2'), ('張三3','1') ; -- 查詢語句 SELECT `name`,`nickname` FROM `student`; SELECT `id`,`name`,`nickname` FROM `student` WHERE `sex`='1'; SELECT `id`,`name`,`nickname` FROM `student` WHERE `sex`='1' ORDER BY `id` DESC; SELECT `id`,`name`,`nickname` FROM `student` WHERE `sex`='1' ORDER BY `id` DESC LIMIT 0,2; -- 修改語句 UPDATE `student` SET `sex`='2' WHERE `sex`='1'; UPDATE `student` SET `sex`='2',`nickname`='沒有暱稱'; -- 刪除語句 DELETE FROM `student` WHERE `sex`='1';
安裝clientmysql
pip install mysqlclient