Common basic database operations

First, the data definition statements (DDL)

1. Create a database

 1) Syntax: create database if not exists database name

 2) database name naming requirements:

  • The first character must be uppercase or lowercase letters, or special characters underscore _, @, #;
  • Subsequent characters can be letters, numbers, @, $, symbols or numbers underlined;
  • Identifier can not do RDBMS reserved words;
  • Do not allow embedded spaces or other special characters.

2, modifying the database character set: alter database table name character set = UTF-8

3, delete the database: drop database database name

4, create a table: create table table name (
    Column Name Data Type Constraints,
   Column Name Data Type Constraints
)

5, the operation of common data table

  • Deleting a table // drop ---> DROP TABLE tongxun;
  • Rename the table // rename to | as: ALTER TABLE tongxun RENAME TO tongxun1;
    or ALTER TABLE tongxun1 RENAME AS tongxun;
  • Copy a table structure // like: CREATE TABLE tongxun LIKE tongxunlu;
  • Copy a table structure and data // as (select * from the old table): CREATE TABLE tongxun AS (SELECT * FROM tongxunlu);
  • Add a new column to the table // add column a new column name data type: ALTER TABLE tongxunlu ADD COLUMN scode1 INT auto_increment PRIMARY KEY;
  • To delete a table // alter table drop column column: ALTER TABLE tongxunlu DROP COLUMN scode1;
  • Modify the column names and types // change column column column names old new column name data type: ALTER TABLE tongxunlu CHANGE COLUMN scode school VARCHAR (10);

Second, the data manipulation statements (DML)

1, insert data

  • Insert data insert into the table (a column name, a column name 2 ...) values ​​(value 1, value 2 ...): INSERT INTO Students (sname, saddress, sgrade, semail, ssex) VALUES ( 'John Doe', 'Chengdu', 6, '123 @ qq.com', 0);
  • Selecting students from the table insert new table corresponding to the content in TongXunLu: INSERT INTO TongXunLu (name, address, email) SELECT sname, saddress, semail FROM Students;
  • Use multiple insert statement batch execution: insert into table (1 column names, column names, 2 ...) values ​​(1 values ​​list, a list of values ​​... 2)

2, modify the data

  • update table set where the initial content of the modified content: UPDATE students SET sname = "John Doe" WHERE sname = 'John Doe';

3, delete data

  • DELETE FROM table name WHERE deletion condition: DELETE FROM students WHERE sname = 'John Doe';
  • To delete all the data in the table: DELETE FROM students;

 

Guess you like

Origin www.cnblogs.com/zhufeng123/p/11795517.html