Python mysql-table creation, deletion and update

Source: Internet
Author: User
Tags python mysql

2017-09-06 20:59:56

    • Creation of a database

CREATE datebase < database name >

    • Creation of tables

CREATE table < table name >

(< column name 1> < data type > < constraints required for the column;

< column name 2> < data type > < constraints required for this column;

.

< constraints on the table 1>,< the table constraints 2>.);

ImportPymysqldb= Pymysql.connect ("localhost","Root","hy1102","TESTDB") Cursor=db.cursor () SQL="""CREATE TABLE Shohin (shohin_id CHAR (4) NOT NULL, Shohin_mei varchar (+) NOT NULL, Shohin_bunrui varchar (+) not NUL L, Hanbai_tanka Integer, Shiire_tanka integer, Torrokubi DATE, PRIMARY KEY (shohin_id))"""cursor.execute (SQL) db.close ()
    • Deletion of tables

DROP table < table name >;

Drop table cannot be recovered, so be sure to pay special attention before deleting it.

    • Updates to the table

ALTER table < table name > ADD < column definition >;

Import= Pymysql.connect ("localhost","root", " hy1102 ","TESTDB"==""ALTER table Shohin Add (Test VARCHAR) "" "cursor.execute (SQL) db.close ()

ALTER table < table name > DROP < column name >

Import= Pymysql.connect ("localhost","root", " hy1102 ","TESTDB"==""ALTER table Shohin drop test"" "cursor.execute (SQL)
Db.close ()

The updated statement of the table is also non-modifiable, so be sure to think clearly before changing it.

Python mysql-table creation, deletion and update

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.