manipulating databases and tables

Source: Internet
Author: User

Mysql–u User name –p password


1. Operational database
Create a database
CREATE DATABASE [IF not EXISTS] db_name [create_specification [, Create_specification] ...]
Create_specification:
[DEFAULT] CHARACTER SET Charset_name | [DEFAULT] COLLATE collation_name

Create DATABASE mydb1;//
Create DATABASE mydb2 character Set GBK; GBK Character Set
Create database mydb3 character Set UTF8 collate utf8_bin;//specify character set and proofing rules


View Database
show databases; Querying the database
Show CREATE Database mydb1; Create statement used when MYDB1 is created before querying

Modify Database
ALTER DATABASE MYDB1 character set UTF8; Change the character set of mydb1 to UTF8

Deleting a database
Drop database mydb1;

Select Database
Use MYDB1;
Select Database (); View the currently selected database

2. Operation table
Create a table
CREATE TABLE table_name
{
Field1 datatype,
Field2 datatype,
FIELD3 datatype,
} [Character set character set] [collate proofing rules]

Field: Specify column name datatype: Specify column type

string: VarChar Char
Big Data: Blob (4G spatial binary data) text (large text 4G space such as fiction)
Value: tinyint ( -128~127) samllint (2 bytes -32768 32767) int (4) bigint (8) Float double
Logic: Bit
Date: Date Time datetime timestamp

CREATE TABLE Employee (
ID int primary KEY auto_increment,
Name varchar (unique),
Gender bit NOT NULL,
Birthday date,
Entry_date date,
Job varchar (40),
Salary Double,
Resume text
);
Define primary key Primary key
Define the primary key auto-grow: auto_increment,
Defining UNIQUE constraints: Unique
Define a non-null constraint: NOT NULL

View Table
View all tables in the current database show tables;
View table structure desc table name;
View Build Table statement show create TABLE employee;

To modify a table:
Add a column: ALTER TABLE employee add image blob;
Modify a column length of 60:alter table employee Modify job varchar (60);
Delete a column: ALTER TABLE employee drop gender;
Modify table Name: Rename table employee to user;
Modify the table's character set to Utf8:alter table user character set UTF8;
Modify the name of the column: ALTER TABLE user change name username varchar (20);

Delete tables: drop table employee;

manipulating databases and tables

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.