Kang Changyuan's Personal Notes

website·Indexed 30 days ago

https://kangchangyuan.github.io/

A collection of personal technical notes covering backend development with MySQL and SQL, frontend design patterns like Factory mode, and system administration topics including Ubuntu 20.04, Git, and SSH keys.

Tokens
5K
Snippets
41
Records
42
Agent score
75%

What's inside Kang Changyuan Notes

  1. Retrieve system information using SQL system functions

    Use these functions to get metadata about the current database environment:

    • version(): Returns the current database version.
    • database(): Returns the name of the current database.
    • user(): Returns the current authenticated user.
    select version(), database(), user();
  2. Create and manage data tables

    Instructions for creating tables with specific constraints, deleting tables safely, and duplicating table structures or data.
    -- Create a table with primary key, auto-increment, and constraints
    create table tableName (
      id int primary key auto_increment,
      nickName varchar(36) not null comment '昵称',
      status int default 0 comment '状态',
      address varchar(120) null comment '地址',
      age int not null comment '年龄'
    );
    
    -- Delete a table
    drop table tableName;
    
    -- Delete a table only if it exists to prevent errors
    drop table if exists tableName;
    
    -- Copy table structure only
    create table newTable like oldTable;
    
    -- Copy both table structure and data
    create table newTable select * from oldTable;
  3. Stash and restore temporary changes

    Use the stash feature to temporarily save uncommitted changes to a stack, allowing you to work on something else and return to those changes later.
    # Stash current changes
    git stash
    
    # Restore the most recent stash
    git stash pop
    
    # List all stashes
    git stash list
  4. Initialize and clone Git repositories

    Create a new local repository in the current directory, create a new directory as a repository, or clone an existing remote repository including its full commit history.
    # Initialize repository in current directory
    git init
    
    # Initialize a new directory as a repository
    git init [projectName]
    
    # Clone a remote project
    git clone [url]
  5. Change Ubuntu 20.04 software sources to Aliyun mirror

    To improve download speeds in certain regions, you can replace the default Ubuntu software sources with the Aliyun mirror (https://mirrors.aliyun.com/ubuntu/). It is recommended to back up the original sources list before making changes.
    # Back up the original sources list
    sudo cp /etc/sources.list /etc/sources.list.backup
    
    # Edit the sources list to replace URLs with https://mirrors.aliyun.com/ubuntu/
    sudo vim /etc/sources.list
    
    # Update the package list and upgrade installed packages
    sudo apt update
    sudo apt upgrade
  6. Manage Git branches and tracking

    Create, delete, rename, and list branches. Use remote prune origin to remove references to remote branches that have been deleted.
    # List local branches
    git branch
    
    # List all local and remote branches
    git branch -a
    
    # Create and switch to a new branch
    git checkout -b [newBranchName]
    
    # Delete a branch (use -D for forced deletion)
    git branch -d [branchName]
    git branch -D [branchName]
    
    # Rename current branch
    git checkout [branch]
    git branch -m [newName]
    
    # Rename remote branch: delete old and push new
    git push -d origin branch
    git push origin newName
    git branch --set-upstream-to origin/newName
    
    # Associate local branch with a remote branch
    git branch --set-upstream-to localBranch origin/remoteBranch
    
    # Clean up deleted remote branches from local tracking
    git remote prune origin
  7. Commit and push changes to remote

    Stage changes and commit them using standard conventions (feat, fix, docs, style, refactor, test, chore). Push local commits to the remote server.
    # Stage all changes
    git add .
    
    # Commit changes
    git commit -m 'message'
    
    # Standard push
    git push
    
    # Force push to overwrite remote
    git push -f
    
    # Push a new branch and set upstream tracking
    git push --set-upstream origin [branchName]
  8. View the SSH public key

    SSH public keys (ending in .pub) are typically stored in the ~/.ssh/ directory. Use the cat command to display the contents of the public key file so it can be copied to a remote server or git provider.
    # Navigate to the SSH directory
    cd ~/.ssh/
    
    # View the public key content
    cat xxx.pub
  9. Generate a new SSH key pair

    Use the ssh-keygen command to create a new private and public key pair. The -t ed25519 flag specifies the algorithm, and -C allows you to add a label (typically an email address) to the key. When prompted for a file location, pressing Enter accepts the default path (usually ~/.ssh/). If a passphrase is set, it will be required every time the SSH key is used to push code.
    # Create a new SSH key pair with an email label
    ssh-keygen -m PEM -t ed25519 -C "your.email@example.com"
    
    # When prompted:
    # Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
    # Enter passphrase (empty for no passphrase): [Press enter for no password]
  10. Query data from tables

    Basic data retrieval techniques including selecting specific columns, aliasing, filtering with WHERE, and limiting results.
    -- Select all columns
    select * from tableName;
    
    -- Select specific columns and rename them using AS
    select nickname as cname, status as '状态' from tableName;
    
    -- Filter by multiple conditions
    select * from tableName where status = 0 and nickName = '小米';
    
    -- Filter using IN (matches any value in the list)
    select * from tableName where status in (1,2);
    
    -- Filter using NOT IN
    select * from tableName where status not in (1,2);
    
    -- Filter within a range (inclusive [a, b])
    select * from tableName where status between 1 and 3;
    
    -- Limit results: limit start, count (e.g., start at 0, return 5 records)
    select * from tableName limit 0,5;
  11. Insert, update, and delete data

    Standard CRUD operations for modifying data within a table.
    -- Insert a single record using SET syntax
    insert into tableName set nickName = '昵称小胖', status = 1, age = 60, address = '长白山';
    
    -- Insert multiple records using VALUES syntax
    insert into tableName (nickName, address, status, age) values 
    ('小米', '长白山', 1, 20),
    ('小明', '太行山', 2, 22);
    
    -- Update specific records
    update tableName set nickName = '昵称大胖', status = 2 where id = 1;
    
    -- Delete specific records
    delete from tableName where id = 1;